Slide 1

Slide 1 text

Multipeer Networking ----------------- Rob Stearn

Slide 2

Slide 2 text

History Overview Code ! Future

Slide 3

Slide 3 text

History

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Overview

Slide 6

Slide 6 text

Multipeer networking sends data from one device to many

Slide 7

Slide 7 text

Multipeer networking sends data from one device to many requires no server infrastructure

Slide 8

Slide 8 text

Multipeer networking sends data from one device to many requires no server infrastructure sends over Bluetooth, *Ad-Hoc or Infrastructure WiFi

Slide 9

Slide 9 text

Multipeer networking sends data from one device to many requires no server infrastructure sends over Bluetooth, *Ad-Hoc or Infrastructure WiFi bridges across interfaces

Slide 10

Slide 10 text

Consider 3 iOS devices...

Slide 11

Slide 11 text

Consider 3 iOS devices...

Slide 12

Slide 12 text

Consider 3 iOS devices...

Slide 13

Slide 13 text

Consider 3 iOS devices... 1 connects to 2 via Bluetooth 3 connects to 2 via WiFi 1, 2 and 3 can all see each other no direct connection between 1 & 3 required

Slide 14

Slide 14 text

Multipeer networking sends data from one device to many requires no server infrastructure sends over Bluetooth, *Ad-Hoc or Infrastructure WiFi bridges across interfaces can send data as Message, File or Stream

Slide 15

Slide 15 text

Multipeer networking sends data from one device to many requires no server infrastructure sends over Bluetooth, *Ad-Hoc or Infrastructure WiFi bridges across interfaces can send data as Message, File or Stream can ensure order and delivery of data

Slide 16

Slide 16 text

Multipeer networking sends data from one device to many requires no server infrastructure sends over Bluetooth, *Ad-Hoc or Infrastructure WiFi bridges across interfaces can send data as Message, File or Stream can ensure order and delivery of data API requires iOS7+

Slide 17

Slide 17 text

Discovery

Slide 18

Slide 18 text

Discovery Zero-configuration networking over IP

Slide 19

Slide 19 text

Discovery Zero-configuration networking over IP Every peer acts as a DNS for itself

Slide 20

Slide 20 text

Discovery Zero-configuration networking over IP Every peer acts as a DNS for itself Uses URLs with with the .local domain

Slide 21

Slide 21 text

Discovery Zero-configuration networking over IP Every peer acts as a DNS for itself Uses URLs with with the .local domain Uses multicast DNS to discover services

Slide 22

Slide 22 text

Discovery Zero-configuration networking over IP Every peer acts as a DNS for itself Uses URLs with with the .local domain Uses multicast DNS to discover services Peers self-assign an IP in the 169.254.xxx.xxx range

Slide 23

Slide 23 text

Discovery APIs

Slide 24

Slide 24 text

Code

Slide 25

Slide 25 text

3 Objects: Session Browser Advertiser

Slide 26

Slide 26 text

Session, Browser and Advertiser

Slide 27

Slide 27 text

Session, Browser and Advertiser

Slide 28

Slide 28 text

Session, Browser and Advertiser

Slide 29

Slide 29 text

Session, Browser and Advertiser

Slide 30

Slide 30 text

Session, Browser and Advertiser

Slide 31

Slide 31 text

Session, Browser and Advertiser

Slide 32

Slide 32 text

kMCSessionMaximumNumberOfPeers

Slide 33

Slide 33 text

kMCSessionMaximumNumberOfPeers Bluetooth: 8

Slide 34

Slide 34 text

kMCSessionMaximumNumberOfPeers Bluetooth: 8 Wi-fi: 8?

Slide 35

Slide 35 text

Session Browser Advertiser Sending data

Slide 36

Slide 36 text

Session / MCPeerID MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"cocoad elica"];

Slide 37

Slide 37 text

Session / Initialize self.session = [[MCSession alloc] initWithPeer:peerID securityIdentity:nil* encryptionPreference: MCEncryptionRequired**];

Slide 38

Slide 38 text

Session / Initialize self.session = [[MCSession alloc] initWithPeer:peerID securityIdentity:nil* encryptionPreference: MCEncryptionRequired**]; * optional Array with SecIdentityRef and SecCertificateRef items

Slide 39

Slide 39 text

Session / Initialize self.session = [[MCSession alloc] initWithPeer:peerID securityIdentity:nil* encryptionPreference: MCEncryptionRequired**]; * optional Array with SecIdentityRef and SecCertificateRef items ** choose from MCEncryptionRequired, MCEncryptionOptional or MCEncryptionNone

Slide 40

Slide 40 text

Session / Delegate didChangeState: didReceiveCertificate: didReceiveData: didStartReceivingResourceWithName: didFinishReceivingResourceWithName: didReceiveStream:

Slide 41

Slide 41 text

Session / Details Foreground operation only

Slide 42

Slide 42 text

Session / Details Foreground operation only Disconnects on breakpoints

Slide 43

Slide 43 text

Session / Details Foreground operation only Disconnects on breakpoints Good candidate for a singleton*

Slide 44

Slide 44 text

Browser / Initialize MCBrowserViewController *bvc = [[MCBrowserViewController alloc] initWithServiceType:(NSString *)serviceType session:(MCSession *)session];

Slide 45

Slide 45 text

Browser / Initialize MCBrowserViewController *bvc = [[MCBrowserViewController alloc] initWithServiceType:(NSString *)serviceType session:(MCSession *)session]; Use a 1-15 character string for service type Check IANA domain naming conventions

Slide 46

Slide 46 text

Browser "Easy mode"

Slide 47

Slide 47 text

Browser "Easy mode" boxed solution

Slide 48

Slide 48 text

Browser "Easy mode" boxed solution minimal styling

Slide 49

Slide 49 text

Browser "Easy mode" boxed solution minimal styling awesome!

Slide 50

Slide 50 text

Browser "Easy mode" boxed solution minimal styling awesome! a bit dull

Slide 51

Slide 51 text

Browser / Delegate MCBrowserViewControllerDelegate browserViewController:shouldPresentNearbyPeer :withDiscoveryInfo: browserViewControllerDidFinish: browserViewControllerWasCancelled:

Slide 52

Slide 52 text

Browser / Details "Hard Mode" Build a custom UI Use MCNearbyServiceBrowser for data and callbacks

Slide 53

Slide 53 text

Browser / 'Hard Mode' MCNearbyServiceBrowserDelegate didNotStartBrowsingForPeers: foundPeer:withDiscoveryInfo: lostPeer:

Slide 54

Slide 54 text

Advertiser / Initialize [[MCAdvertiserAssistant alloc] initWithServiceType:service discoveryInfo:discoveryDict session:mySession];

Slide 55

Slide 55 text

Advertiser / Initialize [[MCAdvertiserAssistant alloc] initWithServiceType:service discoveryInfo:discoveryDict session:mySession]; discoveryInfo is your opportunity to add additional info and context about your user.

Slide 56

Slide 56 text

Advertiser / discoveryInfo NSDictionary Keys and Values must be of type NSString Max size per key/value pair of 256 bytes

Slide 57

Slide 57 text

Advertiser / Details "Hard Mode" Build a custom UI for invitations Use MCNearbyServiceAdvertiser advertising & callbacks

Slide 58

Slide 58 text

Advertiser / 'Hard Mode' MCNearbyServiceAdvertiserDelegate advertiser: didNotStartAdvertisingPeer: advertiser: didReceiveInvitationFromPeer: withContext: invitationHandler:

Slide 59

Slide 59 text

Sending data Messages Resources Streams

Slide 60

Slide 60 text

Messages Data with known bounds, serialized into NSData objects, sent atomically sendData: toPeers: withMode: error: session: didReceiveData: fromPeer:

Slide 61

Slide 61 text

Messages can be sent as either: MCSessionSendDataReliable which guarantees delivery and order

Slide 62

Slide 62 text

Messages can be sent as either: MCSessionSendDataReliable which guarantees delivery and order MCSessionSendDataUnreliable which does not

Slide 63

Slide 63 text

Messages can be sent as either: MCSessionSendDataReliable which guarantees delivery and order MCSessionSendDataUnreliable which does not Analogy to TCP/UDP

Slide 64

Slide 64 text

Resources Text files, File URLs or Web URLs sendResourceAtURL: withName: toPeer: withCompletionHandler: Callbacks to start and finish transfer, uses NSProgress

Slide 65

Slide 65 text

Streams Unbounded data, uses NSStreams startStreamWithName: toPeer: error: returns an NSOutputStream session: didReceiveStream: withName: fromPeer: gives an NSInputStream

Slide 66

Slide 66 text

Streams For both input and output streams: > Add stream to a run loop > Open the stream > Set a delegate and respond to stream: handleEvent: callback

Slide 67

Slide 67 text

!

Slide 68

Slide 68 text

Duplicates in the browser

Slide 69

Slide 69 text

Duplicates in the browser Issue: MCPeerID created each time MCSession is instantiated

Slide 70

Slide 70 text

Duplicates in the browser Issue: MCPeerID created each time MCSession is instantiated Solution: Each peer should serialize its own MCPeerID with NSKeyedArchiver, save it to disk and reuse for each new session.

Slide 71

Slide 71 text

Invitation response times

Slide 72

Slide 72 text

Invitation response times Issue: Once an invitation is accepted there's a delay before connecting

Slide 73

Slide 73 text

Invitation response times Issue: Once an invitation is accepted there's a delay before connecting Solution: Encryption is on by default which adds overhead to initial connections. Consider using MCEncryptionNone

Slide 74

Slide 74 text

Comparing MCPeerIDs

Slide 75

Slide 75 text

Comparing MCPeerIDs Issue: MCPeerID hash method returns it's unique ID number, as an NSUInteger.

Slide 76

Slide 76 text

Comparing MCPeerIDs Issue: MCPeerID hash method returns it's unique ID number, as an NSUInteger. Solution: Truncate the returned hash to 32-bit for comparison.

Slide 77

Slide 77 text

Demo

Slide 78

Slide 78 text

LocalTalk on the app store update in progress for iOS 8 OS X version to follow free!

Slide 79

Slide 79 text

Future

Slide 80

Slide 80 text

Multipeer for Mac

Slide 81

Slide 81 text

Multipeer for Mac Common API with iOS

Slide 82

Slide 82 text

Multipeer for Mac Common API with iOS But! Supports background operation

Slide 83

Slide 83 text

Multipeer for Mac Common API with iOS But! Supports background operation However! Not Bluetooth

Slide 84

Slide 84 text

Multipeer for Mac Common API with iOS But! Supports background operation However! Not Bluetooth AirDrop rebuilt to use the new API

Slide 85

Slide 85 text

Multipeer for Mac Common API with iOS But! Supports background operation However! Not Bluetooth AirDrop rebuilt to use the new API OS X <-> iOS AirDrop. Finally.

Slide 86

Slide 86 text

End created with Deckset http://www.decksetapp.com icons made with Sketch http://bohemiancoding.com/sketch/

Slide 87

Slide 87 text

Questions?

Slide 88

Slide 88 text

Rob Stearn @cocoadelica www.cocoadelica.co.uk [email protected]

Slide 89

Slide 89 text

WWDC 2013 Session 708 WWDC 2014 Session 709 IANA service type list Bonjour Overview Multipeer Networking Reference NSNetServices & CFNetServices Programming Guide MultipeerGroupChat sample code Forums