1-08 session 107 - iphone os networking

47
At the convention center, a movie will be placed here These are confidential sessions—please refrain from streaming, blogging, or taking pictures. Session 107 iPhone OS Networking 1

Upload: dario-black

Post on 29-Nov-2014

280 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1-08 Session 107 - iPhone OS Networking

At the convention center, a movie will be placed here

These are confidential sessions—please refrain from streaming, blogging, or taking pictures.

Session 107

iPhone OS Networking

1

Page 2: 1-08 Session 107 - iPhone OS Networking

iPhone OS Networking

Joshua GraessleySenior Software Engineer

2

Page 3: 1-08 Session 107 - iPhone OS Networking

What You’ll Learn

• Networking API overview

• Major differences from Mac OS X

• Device to device communication

• Mobility techniques

• Performance tips

3

Page 4: 1-08 Session 107 - iPhone OS Networking

Introduction

• Full networking stack■ Power■ Mobility■ Performance

4

Page 5: 1-08 Session 107 - iPhone OS Networking

iPhone Environment

• Interfaces■ WiFi■ Cellular

■ Except iPods■ Bluetooth PAN—device to device■ VPN

• IPv4

• Bonjour

5

Page 6: 1-08 Session 107 - iPhone OS Networking

Network APIs

CocoaURL Loading System NSNetServices

CFSocket CFNetServicesCFStream

Socket DarwinBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams

Core Foundation

6

Page 7: 1-08 Session 107 - iPhone OS Networking

Socket APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

7

Page 8: 1-08 Session 107 - iPhone OS Networking

Sockets

• Darwin■ BSD sockets■ getaddrinfo/getnameinfo, etc.■ kqueue/select

• Core Foundation■ Run loop integration■ CFSocket■ CFHost

8

Page 9: 1-08 Session 107 - iPhone OS Networking

Stream APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

9

Page 10: 1-08 Session 107 - iPhone OS Networking

Stream Abstraction

• SSL/TLS support

• Proxy support

• Cocoa■ NSStream

• Core Foundation■ CFStream

10

Page 11: 1-08 Session 107 - iPhone OS Networking

Common Protocol APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

11

Page 12: 1-08 Session 107 - iPhone OS Networking

Common Protocols

• FTP, HTTP, HTTPS

• Cocoa■ NSURLConnection■ NSURLResponse■ NSURLRequest

• Core Foundation■ CFStream■ CFHTTPMessage

12

Page 13: 1-08 Session 107 - iPhone OS Networking

Service Discovery APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

13

Page 14: 1-08 Session 107 - iPhone OS Networking

Service Discovery

• Session 508—Zero Configuration Networking Using Bonjour

• Darwin■ Bonjour

• Core Foundation■ Bonjour■ CFNetService

• Cocoa■ NSNetService

14

Page 15: 1-08 Session 107 - iPhone OS Networking

Miscellaneous APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

15

Page 16: 1-08 Session 107 - iPhone OS Networking

Game Kit

• Session 318—Peer to Peer Networking with Game Kit

• User interface

• Voice chat—Bluetooth and WiFi

• Data sessions—Bluetooth

16

Page 17: 1-08 Session 107 - iPhone OS Networking

Unique on iPhone OS

• No root access■ Read only routing sockets■ No IPSec policy/association access (PF_KEY)■ No Berkeley Packet Filter (BPF)■ Limited Raw IP■ Low numbered ports

■ No root requirement on iPhone OS 3.0

• Subset of System Configuration Framework■ Network reachability■ Use CFProxySupport

17

Page 18: 1-08 Session 107 - iPhone OS Networking

New in iPhone OS 3.0

• Captive network support

• Scoped routing

• Bluetooth device to device

18

Page 19: 1-08 Session 107 - iPhone OS Networking

Captive Network Support

• Avoid broken network state

• Probes network on associate

• Wireless ISP roaming (WISPr) authentication

• Displays web sheet or authenticates

• Scrapes credentials from web sheet

• Disassociate if authentication fails

19

Page 20: 1-08 Session 107 - iPhone OS Networking

Scoped Routing

• Source address based routing

• Flexibility—bind to interface■ Visual voicemail■ Push notifications■ Exchange support■ Internet tethering■ Game Kit

• New in SnowLeopard

20

Page 21: 1-08 Session 107 - iPhone OS Networking

How It Works TodayApplication connects over cellular

iPhone

WiFi

ServerInternet

Cellular

Application

Network Stack

Cellular Network10.0.0.0/8

21

Page 22: 1-08 Session 107 - iPhone OS Networking

How It Works TodaySocket bound to cellular IP

iPhone

WiFi

ServerInternet

Cellular

Application10.1.2.3:1234

Network Stack

Cellular Network10.0.0.0/8

22

Page 23: 1-08 Session 107 - iPhone OS Networking

How It Works TodayDefault route changes to WiFi

iPhone

ServerInternet

Cellular

Application10.1.2.3:1234

Network Stack

WiFi

Cellular Network10.0.0.0/8

WiFi Network192.168.2.0/24

23

Page 24: 1-08 Session 107 - iPhone OS Networking

iPhone OS 3.0 Scoped RoutingExisting connections go over cellular

iPhone

Server

Cellular Network10.0.0.0/8

Internet

Cellular

Application10.1.2.3:1234

Network Stack

WiFi Network192.168.2.0/24

WiFi

24

Page 25: 1-08 Session 107 - iPhone OS Networking

Coping with Routing

• Monitor reachability

• Reconnect on changes

• Scoped routing covers up mistakes■ Mistakes result in slower connections

25

Page 26: 1-08 Session 107 - iPhone OS Networking

Reachability

• Networking hard to get right

• Not a pre-flight check

• Connect first

• Reachability second■ Change notification■ Avoid polling■ React faster

26

Page 27: 1-08 Session 107 - iPhone OS Networking

Reachability

• Create SCNetworkReachabilityRef■ Watch for remote reachability

■ Watch reachability for established connection

• Use asynchronous API

• Evaluate options on reachability callback

SCNetworkReachabilityCreateWithName

SCNetworkReachabilityCreateWithAddressPair

SCNetworkReachabilitySetCallbackSCNetworkReachabilityScheduleWithRunLoopSCNetworkReachabilityGetFlags

27

Page 28: 1-08 Session 107 - iPhone OS Networking

Reachability Flags—Cheat Sheet

■ Safe to attempt connection■ No guarantee connection will succeed

■ Interface not currently connected■ CoreFoundation/Cocoa APIs will trigger “dial”

■ Over cellular■ Over VPN over cellular

kSCNetworkReachabilityFlagsReachable

kSCNetworkReachabilityFlagsConnectionRequired

kSCNetworkReachabilityFlagsIsWWAN

28

Page 29: 1-08 Session 107 - iPhone OS Networking

UIRequiresPersistentWiFi

• Info.plist key■ True means “Network Application”

• Disables WiFi disassociation timer

• Allows WiFi UI■ Password dialog■ Ask to join dialog■ Captive web sheet

29

Page 30: 1-08 Session 107 - iPhone OS Networking

Bluetooth Device to Device

• Bonjour over Bluetooth discovery■ Advertise, browse, resolve■ Expensive

■ Moderately—browse■ Extremely—resolve

• IP over Bluetooth PAN (Personal Area Network)■ Idle disconnect■ One outgoing PAN connection■ Maximum three PAN connections

• WiFi/Bluetooth coexistence

30

Page 31: 1-08 Session 107 - iPhone OS Networking

Performance

• Simulator

• Latency

• Non-blocking IO

• Multiple threads

• CFSocket

31

Page 32: 1-08 Session 107 - iPhone OS Networking

Simulator

• Simulator for rapid application development

• Contains no network simulator

• Test, test, and test again on iPhone■ WiFi■ 3G/EDGE■ VPN

• Differences■ CPU■ Memory and network buffers■ Latency and bandwidth

32

Page 33: 1-08 Session 107 - iPhone OS Networking

Latency

• Cellular round trip time is long■ ~ 1/3 second EDGE■ ~ 1/6 second 3G

• Latency kills■ DNS (1/3 second)■ TCP three-way handshake (1/3 second)■ Request (1/3 second)

• HTTP is latency bound■ Safari over 3G about 2x faster than EDGE

33

Page 34: 1-08 Session 107 - iPhone OS Networking

Latency DiagramDNS

TCP Connect

HTTP GET/

HTTP GET x

HTTP GET y

DNS

TCP Connect

HTTP GET/

HTTP GET x and y

HTTP HTTP with Pipelining

34

Page 35: 1-08 Session 107 - iPhone OS Networking

HTTP GET—NYTimes

0 5s 10s 15s 20s

Time

Server SYN Client FIN

35

Page 36: 1-08 Session 107 - iPhone OS Networking

HTTP Get NYTimes Pipelined

0 1s 2s 3s 4s 5s

Time

Server SYN Client FIN ServerSYN

36

Page 37: 1-08 Session 107 - iPhone OS Networking

HTTP Receive

10s 15s 20s

Time0 5s

37

Page 38: 1-08 Session 107 - iPhone OS Networking

HTTP Receive—First Five Seconds

1s 2s 3s 4s 5s

Time

38

Page 39: 1-08 Session 107 - iPhone OS Networking

HTTP Pipelined Receive

3s 4s 5s

Time

39

Page 40: 1-08 Session 107 - iPhone OS Networking

Latency Notes

• Protocol design■ Allow multiple requests■ Bad: Request A, wait, get response, request B■ Good: Request A, request B, request C, request D■ HTTP Pipelining busted

■ Proxies, Apache plug-Ins

• Multiple connections bad■ Congestion window/slow start problem■ Hard to tune

• Investigate—don’t settle for slow

40

Page 41: 1-08 Session 107 - iPhone OS Networking

Non-Blocking IO

• Never block main thread■ DNS—30 seconds■ Blocking socket read/write/connect

• Indeterminate call duration

• Watchdog—20 seconds

• Avoid modal “connecting” dialogs

• Remain responsive during network failure

41

Page 42: 1-08 Session 107 - iPhone OS Networking

Threads vs. Asynchronous State Machine

• Threads■ Takes more memory■ Thread synchronization overhead■ Easier to write■ Better for CPU bound workloads

• Asynchronous state machine■ Minimal resources■ Better scalability■ Better for I/O bound workloads

• iPhone/iPod Touch have one core

42

Page 43: 1-08 Session 107 - iPhone OS Networking

CFSocket Performance

• Run loop integration cost■ Run loop mach port based■ Sockets file descriptor based

• CFSocket thread■ Watches file descriptors■ Signal via mach port

• Use non-blocking socket

• Read and write until EWOULDBLOCK

• Not applicable to CFStream

• Better on SnowLeopard

43

Page 44: 1-08 Session 107 - iPhone OS Networking

Miscellaneous Tips

• WiFi vs. 3G vs. EDGE■ Don’t assume performance advantage

• WiFi powers off■ 30 minute timer

■ UIRequiresPersistentWiFi for network apps■ Screen lock timer

• Cellular and VPN “Dial” triggers■ Core Foundation■ Cocoa

• Connect UDP sockets

44

Page 45: 1-08 Session 107 - iPhone OS Networking

Summary

• Use reachability

• Non-blocking IO

• Be aware of latency

• Test on iPhone hardware

• Assume dynamic network environment

• Connect UDP socket

• Read all data when using CFSocket

45

Page 46: 1-08 Session 107 - iPhone OS Networking

WWDC Session SurveyYou can rate your sessions on the WWDC Attendee Site.Survey forms are located on each session description page at: 

Q&A

developer.apple.com/wwdc/attendee

46

Page 47: 1-08 Session 107 - iPhone OS Networking

47