hengineer / CaptainsMess

A local multiplayer networking library for making games like Spaceteam in Unity
MIT License
184 stars 20 forks source link

CaptainsMess attempts to connect to another server after creating its own #6

Closed Nestorferrando closed 8 years ago

Nestorferrando commented 8 years ago

With the right timing (2 devices connecting at the same time), it is possible to make CaptainsMess to throw an exception.

It seems that, after creating a server, a new server discovery arrives and then it (also) tries to join to that server. This is the full log:

CaptainsMess# Initialized peer 4614f236, 'CM2', 1-4 players

OnStartConnecting

CaptainsMess# Restarting server with data: 1:4614f236:0:0:999::

CaptainsMess# Discovered 1:9d9c3e7d:0:0:999::

CaptainsMess# Should NOT join.

CaptainsMess# Discovered 1:4614f236:0:0:999::

CaptainsMess# Should NOT join.

CaptainsMess# Hosting candidates: 1:9d9c3e7d:0:0:999::,1:4614f236:0:0:999::

CaptainsMess# MaybeStartHosting? Found 2/1 candidates

CaptainsMess# Hosting candidates: 1:9d9c3e7d:0:0:999::,1:4614f236:0:0:999::

CaptainsMess# Picked 4614f236 as best candidate

#CaptainsMess# StartHosting

CaptainsMess# Discovered 1:9d9c3e7d:1:1:999::

#CaptainsMess# Should join!

CaptainsMess# Joining ::ffff:213.201.97.205 : 7777

CaptainsMess# OnLobbyClientExit (num players = 0)

CaptainsMess# OnLobbyServerConnect (num players = 0)

CaptainsMess# OnLobbyServerConnect (num players = 0)

CaptainsMess# OnLobbyServerCreateLobbyPlayer (num players 0)

CaptainsMess# OnLobbyClientEnter MyCMListener

CaptainsMess# Restarting server with data: 1:4614f236:1:1:999::

CaptainsMess# OnLobbyServerConnect (num players = 1)

(Exception) A connection has already been set as ready. There can only be one. (Exception) ClientScene::AddPlayer: playerControllerId of 0 already in use.

CaptainsMess# Restarting server with data: 1:4614f236:1:2:999::

I solved it myself adding a internal state variable "hostingServer" and using it as a guard, ignoring any server discovery if CM already hosting a server:

public void OnDiscoveredServer(DiscoveredServer aServer) { if (verboseLogging) { Debug.Log("#CaptainsMess# Discovered " + aServer.rawData); } if (hostingServer) { if (verboseLogging) { Debug.Log("#CaptainsMess# Already hosting a server, ignoring " + aServer.rawData); } return; } ...

Don't know if it is the best solution, but it avoids the exception and the app can still run correctly :-) . If you like it I can do a pull request.

Cheers, Néstor.

hengineer commented 8 years ago

Thanks a lot Néstor! I will test this and integrate it into the next version. Now that Spaceteam 2.0 is finalized I'm going to focus more on improving CaptainsMess to make things more stable.

Nestorferrando commented 8 years ago

I'm glad to hear that you are still working hard on it :D :+1: :+1: :+1:

hengineer commented 8 years ago

Should be fixed in 0.5