Search
Mobile

iPhone SDK 3 : Making Connections with GameKit and Bonjour - iPhone to iPhone Gaming Via BonjourHelper

Published 7 min readBy Programming4us Editorial Team

If you’re willing to forgo GameKit’s Bluetooth and work with WiFi, you can duplicate many of GameKit’s features on all iPhones including the older first generation units. Recipe 1 introduces BonjourHelper. 

[GameKitHelper sharedInstance].sessionID = @"Typing Together";
[GameKitHelper sharedInstance].dataDelegate = self;
[GameKitHelper assignViewController:self];

Substituting BonjourHelper for GameKitHelper requires very few programming changes. It uses the same initialization steps, and the data delegate receives an identical set of callbacks. You do need to omit the space in the session ID, a step that isn’t needed in GameKit. GameKit encrypts its session IDs to produce a guaranteed no-space proper Bonjour identifier. BonjourHelper’s plain-text approach means spaces are off-limits. Limit your session ID names to simple alphanumeric text with 14 characters or fewer. Refer to RFC 2782 service types (http://www.dns-sd.org/ServiceTypes.html) for details. The BonjourHelper code transforms the session ID into a standard Bonjour identifier (i.e., _typingtogether._tcp.).

[BonjourHelper sharedInstance].sessionID = @"TypingTogether";
[BonjourHelper sharedInstance].dataDelegate = self;
[BonjourHelper assignViewController:self];

That’s not to say that the functionality and implementation are identical. With BonjourHelper, both units must be on the same network. You lose the pretty GameKit peer connection controller sequence . Instead, BonjourHelper provides a simple alert, as shown in Figure 12-8. Beyond that, BonjourHelper basically provides the same peer-to-peer connectivity and data flow as GameKit.

Figure 1. The custom BonjourHelper class provides a simpler connection interface than GameKit.


Registering Bonjour Names and Ports

You should register any Bonjour names you plan to use for commercial release with the DNS Service Discovery organization. Registration ensures that your service names and protocols will not overlap or conflict with any other vendor. A list of currently registered services is maintained at http://www.dns-sd.org/ServiceTypes.html.

These names must conform to the RFC 2782 standard. Submit your protocol name to srv_type_request@dns-sd.org. Include the up-to-14-character name of the Bonjour service, a longer descriptive name, the contact information (name and e-mail address) of the person registering the service, and an information page URL. Specify the transportation protocol (i.e., _tcp or _udp) and a list of any TXT record keys used.

It may take some time for the volunteers at the dns-sd.org site to process and respond to your query. Delays on the order of weeks are not uncommon. You may need to resubmit, so keep a copy of all your information.

If you plan to use a fixed port (most Bonjour implementations randomly pick a port at runtime to use), you’ll want to submit an application for a registered port number with IANA, the Internet Assigned Numbers Authority, as well. IANA provides a central repository for port registrations and will, at some time, be merged with the dns-sd registry. IANA often takes a year or longer to finish registering new protocol port numbers.

Note

Apple maintains a list of official OS X Bonjour service types in its Technical Q&A QA1312 document, which you can find at http://developer.apple.com/mac/library/qa/qa2001/qa1312.html.


Duplex Connection

For simplicity, BonjourHelper works by establishing a duplex connection. Each device provides both a client and a host. This avoids any issues about trying to get two peers to negotiate with each other and assume the proper server and client roles without both of them ending up as client or server at the same time.

When resolving addresses, the helper ensures that the unit will not connect to itself. It demands a unique IP address that doesn’t match the local one. If the incoming address does match, it just continues looking. The host needs no such checks; outgoing client connections are limited to foreign addresses.

When the helper has established an outgoing connection and accepted an incoming one, it stops looking for any further peers and considers itself fully connected. The helper updates the Connect/Disconnect button if a view controller has been set.

Reading Data

Recipe 1 cannot use a simple read loop, that is, request data, read it, and repeat. Reading data is blocking. A read loop prevents an application from handling its server duties at the same time as its client duties.

Instead, this class uses the nonblocking hasDataAvailable check before asking for new data. A delayed selector adds a natural interval into the poll allowing each host time to update and prepare new data before being barraged by a new request.

Closing Connections

Connections can break in several ways. Users can quit an application, they can press the Disconnect button in the sample, or they can move out of range of the connection. BonjourHelper checks for disconnects exclusively from the server point of view. This simplifies its implementation, assuming that a lost client equates to a lost host and avoids the issue of multiple user notifications, i.e., “Lost connection to server” and “Lost connection to client” for both ends of the duplex connection.

Note

For space considerations, this listing of Recipe 1 omits a number of basic IP utilities, including stringFromAddress:, addressFromString:address:, and localIPAddress. 


Recipe 1. BonjourHelper Provides GameKit-like Connectivity over WiFi