By Peter Bakhirev
Support for Bluetooth networking Support for Wi-Fi networking (since 3.1) Peer discovery "Reliable but slower" data delivery "Unreliable but faster" data delivery "Chat room" client-server model Connected iPhone Apps: GameKit vs CFNetwork presented by Peter Bakhirev ByteClub.com GameKit Peer-to-peer* Connectivity Voice Chat Ability to establish a voice chat session between two devices It's interesting, but out of scope of this talk. Sorry... Peer Discovery in GameKit 2 ways: Built-in peer discovery, using Peer Picker Custom peer discovery, based on event handling UI already implemented Turns on Bluetooth(if necessary) Only 2 peers can connect Can implement own UI No control over Bluetooth (user must turn it on manually) More than 2 peers possible * both of these are based on NetServices and Bonjour "Reliable but slower" "Unreliable but faster" On top of UDP Each message is acknowledged Delivery is guaranteed Order is guaranteed Happens behind the scenes On top of UDP It's fire-and-forget Might get lost Might arrive out of order Personal Area Network Full support for TCP/IP networking over Bluetooth Apple's proprietary implementation Available to CFNetwork, too *Data transfer is flakey when both Bluetooth and Wi-Fi are ON For a simple "echo" round-trip message, latency differs by anywhere between 5% and 20% "Chat room" client-server model Server and clients are called "peers" Server hosts and advertises a session Clients discover and join the session Each peer that joins or leaves is announced to others A peer can send messages to all or some peers of the session Server relays messages between peers Connect to server Session join/leave events are propagated to all peers Peer 3 Server relays messages between peers This is not true peer-to-peer. If server goes away, the structure breaks. 1. Overview of networking APIs 2. Overview of GameKit 3. When to use GameKit 4. GameKit code patterns 5. GameKit-based app demo NetServices Implementation of the Bonjour protocol Allows devices and apps to find each other on the network Uses multicasting, works on local networks Can also be used online, but requires extra infrastructure Easy-to-use, high-level framework CFNetwork BSD sockets UDP Bonjour Available in the App Store Online Multiplayer Gaming Technology @byteclub Twitter Published by Apress Quartz, OpenGL ES, positional audio, networking for iPhone games Scheduled for December 2009 Peer 2 Peer 3 Peer 4 Peer 1 (server) Message from Peer 2 Peer 4 Peer 1 (server) "Peer 4 connected" "Peer 4 connected" Peer 2 Connected iPhone Apps GameKit Good for: Not good for: Real-time and turn based games Data exchange apps (contacts, pictures, etc) Apps that require interactions between devices located nearby Interactions with non-iPhone OS devices Interactions beyond local Wi-Fi network or out of Bluetooth range Wi-Fi support only in firmware 3.1! Message to Peer 4 Socket API: Integrates BSD sockets with run loop, making it easier to write asynchronous network code Stream API: Makes it easier to exchange data over network Support for higher-level protocols such as HTTP and FTP From the TCP/IP family Connection-less Fast Simple No guarantees "User Datagram Protocol" GameKit's Peer-to-Peer Connectivity glossary class GKSession Encapsulates the session used by peers to exchange data sessionID property Allows you to distinguish between different GameKit apps. Clients connect only to servers that advertise matching sessionID Type: NSString. Default value is based on app's Bundle ID. peerID property Unique identification of each peer in the session. Type: NSString, but looks like a long number. protocol GKSessionDelegate Allows you to interact with GKSession by suppying a delegate that implements this protocol. data receive handler Object supplied by you that handles data received from other peers connected to the session. class GKPeerPickerController "Provides a standard UI to allow iPhone to discover and connect to another iPhone" protocol GKPeerPickerControllerDelegate Allows you to interact with GKPeerPickerController by supplying a delegate that implements this protocol 1 and 2 3 4 GameKit pattern: Using peer picker User can dismiss the dialog, but you still have to deallocate it This is your chance to do something great But don't forget to clean up after yourself Optionally, you can customize the session GameKit pattern: Creating server session, no peer picker Session creation with sensible defaults Set it in motion Up to you whether to accept or deny connection GameKit pattern: Choosing a server and connecting, no peer picker This will contain "readable" peer names This will contain "unreadable" peer IDs Set it in motion Initial fill of the server list Create a "client" session Using "peer state changed" callback, keep track of available servers Let user pick a server Try connecting to the chosen server Using callbacks, figure out whether your connection attempt was successful peer's display name Peers can have human-readable names. If you don't specify anything, GameKit uses name of the device by default. GameKit pattern: Handling disconnects Common GameKit coding patterns Using peer picker Creating server session, no peer picker Choosing a server and connecting without peer picker Detecting and handling disconnects Exchanging data "efficiently" Exchanging data "flexibly" GKSession operates on NSData It's up to you to come up with your own data format You need to account for multiple types of messages Conversion to and from NSData should be fairly efficient It's a hypothetical shooting game with 2 kinds of network messages Each message is a structure Turn it into NSData Fill in the details Send it reliably Fill in the details Turn it into NSData Send it UNreliably Figure out message type Handle different messages appropriately You might want to verify that message is legitimate by checking it's size (or some magic value) before attempting to process it GameKit pattern: Exchanging data flexibly You can also use a less rigid data format Example: Pack your keys and values into a dictionary Serialize and de-serialize dictionary using "archivers" Tradeoffs: messages are bigger, more CPU used GameKit pattern: Exchanging data efficiently Time for a demo! 5 Thank you! Questions? Peter Bakhirev Twitter: @byteclub Email: peter@byteclub.com
360iDev Denver 2009, Presentation "Connected iPhone Apps: GameKit vs CFNetwork"
gabriella luna
pat Peter Bakhirev
on the back
gabriella luna