Skip to content

Commit

Permalink
fix: Don't create a socket if not listening (should fix #1054)
Browse files Browse the repository at this point in the history
This prevents Mirage creating a socket that'll never be used in a non-listening environment (ie. Single player campaign mode using Mirage).
  • Loading branch information
SoftwareGuy committed Mar 17, 2022
1 parent 03bf457 commit f33c6eb
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -217,14 +216,22 @@ public void StartServer(NetworkClient localClient = null)
// Only create peer if listening
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;

peer.Bind(SocketFactory.GetBindEndPoint());
}

if (logger.LogEnabled()) logger.Log("Server started listening");
if (logger.LogEnabled()) logger.Log("Server started, listening for connections");
}
else
{
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 f33c6eb

Please sign in to comment.