Skip to content

Commit

Permalink
fix: networkserver shouldn't create socket if we're not listening (ad…
Browse files Browse the repository at this point in the history
…dresses ticket #1054)

Should fix #1054.
  • Loading branch information
SoftwareGuy committed Mar 17, 2022
2 parents 03bf457 + e08a917 commit 5117f49
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void StartServer(NetworkClient localClient = null)
ThrowIfSocketIsMissing();

Application.quitting += Stop;
if (logger.LogEnabled()) logger.Log($"NetworkServer Created, Mirage version: {Version.Current}");
if (logger.LogEnabled()) logger.Log($"NetworkServer created, Mirage version: {Version.Current}");

logger.Assert(Players.Count == 0, "Player should have been reset since previous session");
logger.Assert(connections.Count == 0, "Connections should have been reset since previous session");
Expand All @@ -198,7 +198,6 @@ public void StartServer(NetworkClient localClient = null)
MessageHandler = new MessageHandler(World, DisconnectOnException);
MessageHandler.RegisterHandler<NetworkPingMessage>(World.Time.OnServerPing);

ISocket socket = SocketFactory.CreateServerSocket();
var dataHandler = new DataHandler(MessageHandler, connections);
Metrics = EnablePeerMetrics ? new Metrics(MetricsSize) : null;

Expand All @@ -214,17 +213,28 @@ public void StartServer(NetworkClient localClient = null)

NetworkWriterPool.Configure(config.MaxPacketSize);

// Only create peer if listening
// Are we listening for incoming connections?
// If yes, set up a socket for incoming connections (we're a multiplayer game).
// If not, that's okay. Some games use a non-listening server for their single player game mode (Battlefield, Call of Duty...)
if (Listening)
{
// Create a server specific socket.
ISocket socket = SocketFactory.CreateServerSocket();

// Tell the peer to use that newly created socket.
peer = new Peer(socket, dataHandler, config, LogFactory.GetLogger<Peer>(), Metrics);
peer.OnConnected += Peer_OnConnected;
peer.OnDisconnected += Peer_OnDisconnected;

// Bind it to the endpoint.
peer.Bind(SocketFactory.GetBindEndPoint());
}

if (logger.LogEnabled()) logger.Log("Server started listening");
if (logger.LogEnabled()) logger.Log("Server started, listening for connections");
}
else
{
// Nicely mention that we're going live, but not listening for connections.
if (logger.LogEnabled()) logger.Log("Server started, but not listening for connections: Attempts to connect to this instance will fail!");
}

InitializeAuthEvents();
Active = true;
Expand Down

0 comments on commit 5117f49

Please sign in to comment.