Slide 1

Slide 1 text

@S_Shimotori - try! Swift Tokyo 2024 Parallel Socket Communication in Swift

Slide 2

Slide 2 text

S_Shimotori iOS Engineer @ LY Corporation Eventer 2

Slide 3

Slide 3 text

3 at try! Swift Tokyo Meetup 2023/01/21 Sat.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

From Application to Hardware Kernel Hardware Standard Libraries iOS application, macOS application, … URL, URLSession Socket Network interface 5 Application

Slide 6

Slide 6 text

Parallel Socket Communication in Swift 6

Slide 7

Slide 7 text

Inter-Process Communication Process A Process B 7

Slide 8

Slide 8 text

Socket Communication Process B Socket Process A Socket 8 Host A Host B

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Interface of Serial Ping in Swift func ping( identifier: UInt16, sequenceNumber: UInt16, to address: String ) async throws -> Result data including elapsed time 12

Slide 13

Slide 13 text

Let’s dive into socket communication! 13

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Datagram Socket Process Datagram Socket 16 Your computer Destination host

Slide 17

Slide 17 text

Socket on Swift let fileDescriptor: Int32 = Foundation.socket(…) 17

Slide 18

Slide 18 text

Socket on Swift let fileDescriptor: Int32 = Foundation.socket(…) sendto(fileDescriptor, …) recvfrom(fileDescriptor, …) 18

Slide 19

Slide 19 text

Socket on Swift let fileDescriptor: Int32 = Foundation.socket(…) sendto(fileDescriptor, …) recvfrom(fileDescriptor, …) close(fileDescriptor) 19

Slide 20

Slide 20 text

Socket on Swift let fileDescriptor: Int32 = Foundation.socket(…) sendto(fileDescriptor, …) recvfrom(fileDescriptor, …) close(fileDescriptor) sendto(fileDescriptor, …) // runtime error 20

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Break Down Goals into Tasks • Create a datagram socket ✅ • Encode/Decode a message • Execute system calls for messaging 22

Slide 23

Slide 23 text

00001000 00000000 11110111 11110110 00000000 00000100 00000000 00000101 …… Binary Message Data for Ping 23

Slide 24

Slide 24 text

Binary Message Data for Ping type code checksum identi fi er sequence number 00001000 00000000 11110111 11110110 00000000 00000100 00000000 00000101 …… 24

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Using Structure to Decode Received Message struct IPv4Header { …… let timeToLive: UInt8 …… let sourceIPAddress: in_addr …… } …… 01000000 …… 01111111 00000000 00000000 00000001 …… 27

Slide 28

Slide 28 text

Break Down Goals into Tasks • Create a datagram socket ✅ • Encode/Decode a message ✅ • Execute system calls for messaging 28

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

System Call with Unsafe Types message.withUnsafeBytes { pointerToMessage in withUnsafePointer(to: address) { pointerToAddress in sendto(…, UnsafeRawPointer, …, UnsafePointer, …) } } 30

Slide 31

Slide 31 text

Poll before Receive // These functions block until receiving _ = poll(…, Int32) _ = recvfrom(…, UnsafeMutableRawPointer, …) timeout 31 mutable Data

Slide 32

Slide 32 text

Break Down Goals into Tasks • Create a datagram socket ✅ • Encode/Decode a message ✅ • Execute system calls for messaging ✅ 32

Slide 33

Slide 33 text

Datagram Socket Data Flow of “Serial” Ping Timer UI Data Binding 33 Your computer Destination host every sec.

Slide 34

Slide 34 text

Advanced: Parallel Requests 34

Slide 35

Slide 35 text

Monitoring Multiple Hosts 35

Slide 36

Slide 36 text

Monitoring Multiple Hosts 36 deadman

Slide 37

Slide 37 text

Multiple Request Messages Datagram Socket Host A Host B Host C 37 ping Your computer

Slide 38

Slide 38 text

Multiple Reply Messages Datagram Socket Host A Host B Host C 38 reply Your computer

Slide 39

Slide 39 text

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.

Slide 40

Slide 40 text

Logic that We Should Code in Swift • Sorting received messages • Handling timeout 40

Slide 41

Slide 41 text

Supplement: About ncurses • Thread-unsafe • Managing screens using OpaquePointer 41

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

Summary • You can experience socket communication using Swift. • Swift ensures safety and helps parallel communication. • Have fun learning socket communication with Swift! 43

Slide 44

Slide 44 text

44 @S_Shimotori_pub @S-Shimotori

Slide 45

Slide 45 text

References 45

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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 — LLVM 19.0.0git documentation • https://llvm.org/docs/LangRef.html#id664 47

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Resources 50

Slide 51

Slide 51 text

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