1-08 session 107 - iphone os networking

Post on 29-Nov-2014

280 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

iPhone OS Networking

Joshua GraessleySenior Software Engineer

2

What You’ll Learn

• Networking API overview

• Major differences from Mac OS X

• Device to device communication

• Mobility techniques

• Performance tips

3

Introduction

• Full networking stack■ Power■ Mobility■ Performance

4

iPhone Environment

• Interfaces■ WiFi■ Cellular

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

• IPv4

• Bonjour

5

Network APIs

CocoaURL Loading System NSNetServices

CFSocket CFNetServicesCFStream

Socket DarwinBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams

Core Foundation

6

Socket APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

7

Sockets

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

• Core Foundation■ Run loop integration■ CFSocket■ CFHost

8

Stream APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

9

Stream Abstraction

• SSL/TLS support

• Proxy support

• Cocoa■ NSStream

• Core Foundation■ CFStream

10

Common Protocol APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

11

Common Protocols

• FTP, HTTP, HTTPS

• Cocoa■ NSURLConnection■ NSURLResponse■ NSURLRequest

• Core Foundation■ CFStream■ CFHTTPMessage

12

Service Discovery APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

13

Service Discovery

• Session 508—Zero Configuration Networking Using Bonjour

• Darwin■ Bonjour

• Core Foundation■ Bonjour■ CFNetService

• Cocoa■ NSNetService

14

Miscellaneous APIs

URL Loading System NSNetServices

CFSocket CFNetServicesCFStream

SocketBonjour

Game Kit

ReachabilityCFNetwork

Socket Streams Cocoa

Darwin

Core Foundation

15

Game Kit

• Session 318—Peer to Peer Networking with Game Kit

• User interface

• Voice chat—Bluetooth and WiFi

• Data sessions—Bluetooth

16

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

New in iPhone OS 3.0

• Captive network support

• Scoped routing

• Bluetooth device to device

18

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

Scoped Routing

• Source address based routing

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

• New in SnowLeopard

20

How It Works TodayApplication connects over cellular

iPhone

WiFi

ServerInternet

Cellular

Application

Network Stack

Cellular Network10.0.0.0/8

21

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

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

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

Coping with Routing

• Monitor reachability

• Reconnect on changes

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

25

Reachability

• Networking hard to get right

• Not a pre-flight check

• Connect first

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

26

Reachability

• Create SCNetworkReachabilityRef■ Watch for remote reachability

■ Watch reachability for established connection

• Use asynchronous API

• Evaluate options on reachability callback

SCNetworkReachabilityCreateWithName

SCNetworkReachabilityCreateWithAddressPair

SCNetworkReachabilitySetCallbackSCNetworkReachabilityScheduleWithRunLoopSCNetworkReachabilityGetFlags

27

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

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

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

Performance

• Simulator

• Latency

• Non-blocking IO

• Multiple threads

• CFSocket

31

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

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

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

HTTP GET—NYTimes

0 5s 10s 15s 20s

Time

Server SYN Client FIN

35

HTTP Get NYTimes Pipelined

0 1s 2s 3s 4s 5s

Time

Server SYN Client FIN ServerSYN

36

HTTP Receive

10s 15s 20s

Time0 5s

37

HTTP Receive—First Five Seconds

1s 2s 3s 4s 5s

Time

38

HTTP Pipelined Receive

3s 4s 5s

Time

39

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

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

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

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

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

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

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

47

top related