Ok, that’s strange, I have it still in 5.2.1p4.
If I understand right it always happens after trying to connect a few times to a server which is not available. In my project the game tries to connect to the (unet-) master server. If the server is not reachable or offline, this fails with a timeout error. Then the game tries to connect again after a short break. If I leave the game running while the server is offline, after a few (probably 16) times this error occurs and I can’t join or start a server or host a game anymore.
I’ll try to create a smaller repro and will report this as a new issue.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System.Collections;
public class Connector : MonoBehaviour {
int connectionAttemptCount;
NetworkClient client;
bool errorHappened;
void Update () {
if (errorHappened) {
errorHappened = false;
StopClient ();
}
if (client == null )
StartClient ();
}
void StartClient()
{
client = new NetworkClient ();
client.RegisterHandler (MsgType.Connect, OnConnected);
client.RegisterHandler (MsgType.Disconnect, OnDisconnected);
client.RegisterHandler (MsgType.Error, OnError);
// short timeouts
var config = new ConnectionConfig();
config.ConnectTimeout = 100;
client.Configure (config, 1);
client.Connect ("2.2.2.2", 1234); // some non-connectable server to create a connection timeout
connectionAttemptCount++;
Debug.LogFormat ("New connection attempt {0}...",connectionAttemptCount);
}
void StopClient()
{
print ("Stopping client: hostid=" +client.connection.hostId);
// can't do it here, would spam this error: host id {0} has been already deleted
//NetworkTransport.RemoveHost (client.connection.hostId);
//client.Disconnect ();
client.UnregisterHandler (MsgType.Connect);
client.UnregisterHandler (MsgType.Disconnect);
client.UnregisterHandler (MsgType.Error);
client.Shutdown ();
client = null;
}
void OnConnected(NetworkMessage netMsg)
{
print ("connected");
}
void OnDisconnected(NetworkMessage netMsg)
{
print ("disconnected");
}
void OnError(NetworkMessage netMsg)
{
// we just assume it's a timeout error
errorHappened = true;
}
}
This tries to connect to a server that doesn’t exist and fails with a timeout. After failing, it repeats the same thing over and over again. After 16 times it fails with said error.
Maybe I’m doing something wrong when cleaning up the NetworkClient instance, but I have no idea what it is. I also tried to call NetworkClient.Disconnect() or NetworkTransport.RemoveHost() before shutting down, but that just spammed the console with “host id {0} has been already deleted” errors and breaks the loop in this example. The host id of the client keeps increasing until it reaches 15, which causes the “maximum hosts cannot exceed {16}” error.
I think the original bug is more of how it is designed, rather than letting the developers use this workaround. Either these workarounds should be listed in the documentation, or it needs to be revised, I feel.
Also, are you sure your bug is related to clicking 17 times on the LAN client in the NetworkManagerHUD?
I’ve stumbled across this bug also. I have a server supervisor that checks if server is alive. If it hangs for some reason - it should stop it, restart and connect to new instance of server. All worked well until I’ve introduced reconnect to new instance. After 16 retries of connecting I get same error as above.
How I’ve solved it?
private void ClearHostIDs()
{
if(hostIDs.Count != 0)
{
var clearList = new List<int>();
foreach(int id in hostIDs)
{
if(client.connection != null)
{
if(id != client.connection.hostId)
{
NetworkTransport.RemoveHost(id);
clearList.Add(id);
}
} else
{
NetworkTransport.RemoveHost(id);
clearList.Add(id);
}
}
foreach(int id in clearList)
{
hostIDs.Remove(id);
}
}
}
Where I add hostIDs to list after I TRY to connect (not on OnConnect network event). I run this method every time I try to reconnect.