Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Parallel Socket Communication in Swift

Parallel Socket Communication in Swift

Shigure Shimotori

March 22, 2024
Tweet

More Decks by Shigure Shimotori

Other Decks in Programming

Transcript

  1. From Application to Hardware Kernel Application Hardware Standard Libraries iOS

    application, macOS application, … URL, URLSession Socket Network interface 4 Lower
  2. From Application to Hardware Kernel Hardware Standard Libraries iOS application,

    macOS application, … URL, URLSession Socket Network interface 5 Application
  3. Sample: Ping $ ping -c 2 example.com PING example.com (93.184.216.34):

    56 data bytes 64 bytes from 93.184.216.34: icmp_seq=0 ttl=52 time=165.910 ms 64 bytes from 93.184.216.34: icmp_seq=1 ttl=52 time=108.211 ms --- example.com ping statistics --- 2 packets transmitted, 2 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 108.211/137.060/165.910/28.850 ms 9
  4. Sample: Ping $ ping -c 2 example.com PING example.com (93.184.216.34):

    56 data bytes 64 bytes from 93.184.216.34: icmp_seq=0 ttl=52 time=165.910 ms 64 bytes from 93.184.216.34: icmp_seq=1 ttl=52 time=108.211 ms --- example.com ping statistics --- 2 packets transmitted, 2 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 108.211/137.060/165.910/28.850 ms 10 byte count IP address sequence No. TTL elapsed time
  5. Communication with Ping 64 bytes from 93.184.216.34: icmp_seq=0 ttl=52 time=165.910

    ms 56 bytes message icmp_seq=0 64 bytes message icmp_seq=0 93.184.216.34 11
  6. Interface of Serial Ping in Swift func ping( identifier: UInt16,

    sequenceNumber: UInt16, to address: String ) async throws -> Result data including elapsed time 12
  7. Break Down Goals into Tasks • Create a datagram socket

    • Encode/Decode a message • Execute system calls for messaging 14
  8. Break Down Goals into Tasks • Create a datagram socket

    • Encode/Decode a message • Execute system calls for messaging 15
  9. Socket on Swift let fileDescriptor: Int32 = Foundation.socket(…) sendto(fileDescriptor, …)

    recvfrom(fileDescriptor, …) close(fileDescriptor) sendto(fileDescriptor, …) // runtime error 20
  10. Socket on Swift ‘ fi leDescriptor' used after consume let

    fileDescriptor = FileDescriptor(fd: socket(…)) fileDescriptor.send(…) fileDescriptor.receive(…) fileDescriptor.close() // consuming func fileDescriptor.send(…) 21 struct FileDescriptor: ~Copyable
  11. Break Down Goals into Tasks • Create a datagram socket

    ✅ • Encode/Decode a message • Execute system calls for messaging 22
  12. Binary Message Data for Ping type code checksum identi fi

    er sequence number 00001000 00000000 11110111 11110110 00000000 00000100 00000000 00000101 …… 24
  13. Using Structure to Encode Message struct ICMPEchoRequest { let type:

    UInt8 let code: UInt8 let checksum: UInt16 let identifier: UInt16 let sequenceNumber: UInt16 …… } 00001000 00000000 11110111 11110110 00000000 00000100 00000000 00000101 …… 25
  14. Byte Order • Big endian, in network 00000000 00000101 =

    5 • Little endian, in Swift 00000101 00000000 = 0b101 = UInt16(5) 00000000 00000101 = 0b101_00000000 = UInt16(1280) 00000000 00000101 = UInt16(5).bigEndian = UInt16(1280) 26
  15. Using Structure to Decode Received Message struct IPv4Header { ……

    let timeToLive: UInt8 …… let sourceIPAddress: in_addr …… } …… 01000000 …… 01111111 00000000 00000000 00000001 …… 27
  16. Break Down Goals into Tasks • Create a datagram socket

    ✅ • Encode/Decode a message ✅ • Execute system calls for messaging 28
  17. System Calls for Datagram Socket • sendto(…, UnsafeRawPointer, …) ->

    length of message • poll(…, Int32) -> result • recvfrom(…, UnsafeMutableRawPointer, …) -> length of message pointer to Data pointer to Data for storing timeout 29
  18. System Call with Unsafe Types message.withUnsafeBytes { pointerToMessage in withUnsafePointer(to:

    address) { pointerToAddress in sendto(…, UnsafeRawPointer, …, UnsafePointer<sockaddr>, …) } } 30
  19. Poll before Receive // These functions block until receiving _

    = poll(…, Int32) _ = recvfrom(…, UnsafeMutableRawPointer, …) timeout 31 mutable Data
  20. Break Down Goals into Tasks • Create a datagram socket

    ✅ • Encode/Decode a message ✅ • Execute system calls for messaging ✅ 32
  21. Datagram Socket Data Flow of “Serial” Ping Timer UI Data

    Binding 33 Your computer Destination host every sec.
  22. Multiple Reply Messages Datagram Socket Host A Host B Host

    C 39 reply Your computer Timers UI Data Binding while true { poll(…) } timeout cancel Timer every sec.
  23. 42

  24. Summary • You can experience socket communication using Swift. •

    Swift ensures safety and helps parallel communication. • Have fun learning socket communication with Swift! 43
  25. Official Pages • API Reference: iOS Manual Pages • https://developer.apple.com/library/archive/documentation/System/Conceptual/

    ManPages_iPhoneOS/index.html • Unsafe Swift - WWDC20 - Videos - Apple Developer • https://developer.apple.com/videos/play/wwdc2020/10648 • Safely manage pointers in Swift - WWDC20 - Videos - Apple Developer • https://developer.apple.com/videos/play/wwdc2020/10167/ • swift-evolution/proposals/0390-noncopyable-structs-and-enums.md at main · apple/swift- evolution • https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and- enums.md 46
  26. apple/swift - Int.bigEndian • swift/stdlib/public/core/Integers.swift at d7b3d3e319d99ee3956dd1af899b73112d7517fd · apple/swift •

    https://github.com/apple/swift/blob/d7b3d3e319d99ee3956dd1af899b73112d7517fd/stdlib/public/core/ Integers.swift#L2296-L2321 • swift/stdlib/public/core/IntegerTypes.swift.gyb at 633d5bc45a7c0a7b0e481701cf3f8fc2025f8f51 · apple/swift • https://github.com/apple/swift/blob/633d5bc45a7c0a7b0e481701cf3f8fc2025f8f51/stdlib/public/core/ IntegerTypes.swift.gyb#L1646-L1654 • llvm-project/llvm/include/llvm/IR/Intrinsics.td at 11fcae69dbea4860e20ab799ecca9b0432d7f19d · llvm/llvm- project • https://github.com/llvm/llvm-project/blob/11fcae69dbea4860e20ab799ecca9b0432d7f19d/llvm/include/ llvm/IR/Intrinsics.td#L638-L641 • LLVM Language Reference Manual &#8212; LLVM 19.0.0git documentation • https://llvm.org/docs/LangRef.html#id664 47
  27. System Programming • Kerrisk, Michael. The Linux Programming Interface. No

    Starch Press. • ઍॅ࣏࿠ ༁. LinuxϓϩάϥϛϯάΠϯλϑΣʔε. ΦϥΠϦʔɾδϟύϯ • UNIXωοτϫʔΫϓϩάϥϛϯάʹొ৔͢Δߏ଄ମͷ঺հͱਖ਼͍͠࢖͍ํ • “Introduction and proper use of structures in UNIX network programming” • http://cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/NETWORK/struct.html 48
  28. Ping • upa/deadman: deadman is a curses-based host status checking

    application using ping • https://github.com/upa/deadman • Ping͕OKͱͳΔ৚݅ʢҟͳΔΞυϨε͔ΒԠ౴͕ฦ͖ͬͯͨ৔߹ʣ • “Conditions under which a ping is fine (when a response is returned from a different address)” • https://infrastructure-engineer.com/tcpip-basic-0005/ • ICMPͷNAPTӽ͑-ϙʔτ൪߸͕ͳ͍ͷʹͳͥʁ | izuminͷඋ๨࿥ • “ICMP through NAT traversal-Why it can pass without a port number” • https://izuminmin.com/network/icmpping-napt/ 49
  29. Image Resources • ͔Θ͍͍ϑϦʔૉࡐू ͍Β͢ͱ΍ • https://www.irasutoya.com/ • About X

    | Our logo, brand guidelines, and tools • https://about.x.com/en/who-we-are/brand-toolkit • GitHub Logos and Usage • https://github.com/logos 51