GKPeerStateAvailable for non existing peers? - iphone

used this website for the long time to find answers, now need to ask on myself! :)
I have implemented multi player for my game using GKSession and client/server approach, not using peer picker (up to 4 player allowed).
I have one big problem which I can't find solution for.
If you disconnect/reconnect server peer quickly enough, client peer will "remember" that host and will show it in the list of all available peers permanently, only turning wifi/bluetooth off resets this.
So if I do this 5 times in a row, my client will show 5 available hosts with the same name!
Anyone had experience with this? Any idea of workarounds?
I should note that requirements for my game is iPhone SDK 3.0 for Bluetooth and 3.1 for Wifi as GKSession supports both now.

I have a similar problem. In fact I event get peers listed who are not even currently playing the game. Even more so, peers who are not acting as server are listed. I am wondering if there is some behind the scenes caching going on.
I'm thinking about implementing some kind of timer with visual feedback to show the disconnect progress, ideally to prevent this situation. It seems like in general there is a bit of time delay involved with connecting/available and disconnecting/unavailable. It might be better to code the interface to take these time delays into consideration, though I'm not sure exactly how.

Related

How can I index nearby devices into a UITableView and pair with the selected devices?

How can I find nearby iPhones and iPods running the same application and index them into a UITableView? Then how would I go about pairing with the selected devices over bluetooth to carry out an undisclosed process?
GKSession is the easiest I've seen, although it took me about 4-5 hrs to do and that was not messing around just straight typing you will need a client model
A server model hostTableView and a joingameTableView and not to mention quite a few delegates to transfer the info back and forth between the server model and client model and back and forth between the host and the client(joingameTableView) they all talk back and forth to make that happen. You can use bonjour and other methods but I haven't used any other. I will try to post an example that compiles to git sometime soon, there is just way to much logic involved for me to explain in a post! Hope I helped at least guided you in the right direction.

Is there a way to kill a hanging thread?

I am using a third party framework, which in rare cases does not respond anymore, if the internet connection is lost. My tests showed that when I am connected via WiFi on my home network and I start using the app and when the network changes from WiFi to the carrier network, the thread does not respond, if it was exchanging data at that very moment.
Of course I got in touch with the vendor of the framework to fix this, however, it will take some time, until I will get the patch.
In the meantime I would like to implement a work-around, so the user does not have to deal with a non-responding app, especially as this is a no-go for the app approval.
My question: Is there a way I can monitor such a thread and terminate it after a timeout?
Or would anyone have another solution approach for this issue? I know about the reachability framework of Apple, but as far as I understand the framework, it is a detection tool only, which in my case would not solve my issue, right?
Thanks, René

Roll your own Multiplayer iPhone setup?

I've been looking into the various options for implementing multiplayer (internet) in iPhone games. Game Center, OpenFeint etc are obviously very good, stable and feature-rich. But I can't help thinking my needs are a lot simpler than what they provide.
If I wanted to put together a simple 2d tank game, could I not just..
setup a mysql server on the web, with some get/set php scripts to actas 'web services'.
when you open the game, a call is made to the server, to log that you're 'available'.
when you select Play, a call is made to retrieve a list of 'available' IP's.
you select an opponent, remote IP is stored locally.
game starts, UDP handles the data between your IP and your opponent's IP.
..or would that never work? apologies, I'm very much a n00b at gaming/networking dev!
thanks in advance!
In addition to the complexities that dtuckernet points out, there are few other things to consider:
You need to think about the cost of running and supporting the web server. Can you maintain the same or better uptime than Apple and OpenFeint?
GameCentre promotes games that support it. The App Store adds an icon, and Game Centre itself lists games that your friends own. Don't underestimate the effect of free advertising!
Even if the code it easy (which I suspect isn't true), why take the risk of developing your own when there is well-tested code available for free?
Incidentally, depending on your requirements, you may want to use a hybrid approach. For example, Game Centre has a maximum of four players per game when Apple hosts it. You can find players using GC and then run the games itself on your own server.
That would certainly work, but implementing this can be a bit harder than it seems initially. You have to handle things like NAT/Firewall traversal and if you are using UDP you need to implement some type of a heartbeat to ensure you know when devices have disconnected (and handle it accordingly).
In most all cases, you would be better off working with a solution that already has these problems solved.

iOS Online Game w/o Server?

I've made an iOS game that works over both Bluetooth and bonjour. It works great both ways, but I'd really like it if players could play with eachother with any Internet connection, be it edge, 3G, or different wi-fi addresses.
The catch: I don't have the resources or knowledge to set up dedicated servers or anything like that. It seems that surely GameKit has something in it that will let me do this, but despite spending dozens of hours over the last week figuring out how to use it, I've found nothing.
So, does anyone have ideas for solutions? My game is just one on one, if that makes it any easier.
Why don't go through the Game Center with GameKit?
Apple provides all the infrastructure to play all around the world via their servers (in 3G or Wifi). With the Game Center you can play up to 4 players (so in your case there is no problem), it's free, you don't have to manage any server, there is a mechanism of invitation between friends, and many more.
The logic of your game stay in the client side, so it'll be same design as your actual game (expect for the API I think).
You can find all information on the official documentation here.
If you want, there is a very tuto on the ray wenderlich's blog (but it uses the cocos2d framework instead of the CocoaTouch).
Yep, try GameCenter with GKMatch - works really well if game doesnt need to send tons of data. If you make additional layer between your app and GKMatch and GKSession, you can use both transparently (so your game supports local AND global multiplayer). Tested, it works

GameKit bluetooth

I'm trying to understand the basic steps required for a game I'm trying to make using GameKit for bluetooth connections. I only have 1 device, so I can't test out my ideas very easily.
The game involves a maximum of 2 players over bluetooth via GameKit and the only data to send/receive is a 'shot' which includes position and direction data only.
This is what I think should happen, I really just want someone to point out any mistakes I've made:
The game starts by presenting a GKPeerPickerController with a connectionTypeMask of 'GKPeerPickerConnectionTypeNearby' and setting it's delegate to 'self'.
The GKPeerPickerController delegate method 'peerPickerController:didConnectPeer:toSession:' is implemented and sets the sessions dataReceiveHandler to self. The peerID is added to an NSMutableArray which holds all peer IDs connected. (There should only be 1 peer connected at any time).
To enforce only 1 peer connection per game the method 'session:didReceiveConnectionRequestFromPeer:' checks the 'count' property of the NSMutableArray which holds the connected peerIDs. If 'count'>0 then we respond by calling: 'denyConnectionFromPeer:' else, we call: 'acceptConnectionFromPeer:error:'
To send a shot, the game calls: 'sendData:toPeers:withDataMode:error:'
To receive shots, the game responds to: 'receiveData:fromPeer:inSession:context:'
Are there any steps missing? For example, if a user tries to connect to me before I connect to them, what will happen with my GKPeerPickerController? How do I know to dismiss it?
And without another iPhone/iPod I can't test.
First off, if you want to test Bluetooth in Gamekit you really are going to need a 2nd device. If you're serious about your app, try and buy a cheap/refurbished iPhone or iPod Touch that will support GameKit Bluetooth (the oldest devices don't do it). I actually think you do even better with two Macs for testing, but if you don't already have a second one that's an expensive proposition.
What you've posted above is pretty much correct - actually using the PeerPicker is pretty straightforward and you should be able to get through it easily using Apple's docs. The problem is that in my experience, it's a bit janky. It might be because I'm using a 3GS and a 3G to test and they have different Bluetooth inside, but a lot of times I see weird things when the scenario you outlined occurred - "if a user tries to connect to me before I connect to them." In the end it typically still works, but not without a little overhead of waiting and perhaps the user having to repeat the process a time or two.