开发者

iPhone GameKit asynchronous server pattern

What is the 开发者_JS百科best way to implement a server piece of application using iOS GameKit? Should I create a separate server thread that somehow responds to all delegated events (data received etc)? Or maybe use NSRunLoops?


Don't use threads unless you know you need to.

Nearly everything uses the main thread's run loop by default. You can't (easily) use another run loop without using another thread — each thread has at most one run loop.

(Strictly, most things use the "current" run loop, which is the run loop of the currently executing thread, which is generally going to be the main thread.)

In the past, most PC games didn't run the networking code in a different thread, meaning that a slow graphics card reduced the performance of the networking code. These days, FPSes often run the network code in a separate thread by default or have an option to do so (Quake 3 has a "pure server" mode); this makes game physics decoupled from the host's render performance, which is generally a good thing.

(Somehow, doing anything which causes a texture/etc reload in Quake 3 still lags the server thread.)

If you are going to run the server in a separate thread, I suggest communicating with the local "client" over the "network". You'll have to write some sort of network code anyway, so just use something like socketpair(AF_UNIX, SOCK_DGRAM, 0, &sockets) (if that doesn't work, try AF_INET) and treating them like UDP sockets.

Note that you don't need to use GameKit to communicate over Bluetooth. If you publish a Bonjour service (see NSNetService), it will automatically be advertised over the same Bluetooth PAN that's used for GameKit (under the hood, GameKit runs over Bonjour over IP over Bluetooth). If you just use Bonjour and UDP, it's easier to make it cross-platform and to support old devices which don't do GameKit over Bluetooth.

(I think GameKit has additional mesh networking features, so A and B can communicate by using C and a relay. I haven't tested this. It's not terribly important.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜