Skip to content

Commit

Permalink
fix(NetworkServer): making listening disable server peer (#959)
Browse files Browse the repository at this point in the history
previously would just ignore new connections
  • Loading branch information
James-Frowen committed Oct 6, 2021
1 parent 7cbfba2 commit 528698b
Showing 1 changed file with 26 additions and 41 deletions.
67 changes: 26 additions & 41 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public class NetworkServer : MonoBehaviour, INetworkServer

public bool DisconnectOnException = true;

/// <summary>
/// <para>If you disable this, the server will not listen for incoming connections on the regular network port.</para>
/// <para>This can be used if the game is running in host mode and does not want external players to be able to connect - making it like a single-player game.</para>
/// </summary>
[Tooltip("If disabled the server will not create a Network Peer to listen. This can be used to run server single player mode")]
public bool Listening = true;

[Tooltip("Creates Socket for Peer to use")]
Expand Down Expand Up @@ -200,18 +197,28 @@ public void StartServer(NetworkClient localClient = null)
ISocket socket = SocketFactory.CreateServerSocket();
var dataHandler = new DataHandler(MessageHandler, connections);
Metrics = EnablePeerMetrics ? new Metrics(MetricsSize) : null;
Config config = PeerConfig ?? new Config

Config config = PeerConfig;
if (config == null)
{
MaxConnections = MaxConnections,
};
config = new Config
{
// only use MaxConnections if config was null
MaxConnections = MaxConnections,
};
}

NetworkWriterPool.Configure(config.MaxPacketSize);

peer = new Peer(socket, dataHandler, config, LogFactory.GetLogger<Peer>(), Metrics);
peer.OnConnected += Peer_OnConnected;
peer.OnDisconnected += Peer_OnDisconnected;
// Only create peer if listening
if (Listening)
{
peer = new Peer(socket, dataHandler, config, LogFactory.GetLogger<Peer>(), Metrics);
peer.OnConnected += Peer_OnConnected;
peer.OnDisconnected += Peer_OnDisconnected;

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

if (logger.LogEnabled()) logger.Log("Server started listening");

Expand Down Expand Up @@ -268,7 +275,14 @@ internal void Update()
private void Peer_OnConnected(IConnection conn)
{
var player = new NetworkPlayer(conn);
ConnectionAccepted(player);

if (logger.LogEnabled()) logger.Log("Server accepted client:" + player);

// add connection
AddConnection(player);

// let everyone know we just accepted a connection
Connected?.Invoke(player);
}

private void Peer_OnDisconnected(IConnection conn, DisconnectReason reason)
Expand Down Expand Up @@ -450,35 +464,6 @@ public static void SendToMany<T>(IReadOnlyList<INetworkPlayer> players, T msg, i
}
}

void ConnectionAccepted(INetworkPlayer player)
{
if (logger.LogEnabled()) logger.Log("Server accepted client:" + player);

//Only allow host client to connect when not Listening for new connections
if (!Listening && player != LocalPlayer)
{
return;
}

// are more connections allowed? if not, kick
// (it's easier to handle this in Mirage, so Transports can have
// less code and third party transport might not do that anyway)
// (this way we could also send a custom 'tooFull' message later,
// Transport can't do that)
if (Players.Count >= MaxConnections)
{
player.Connection?.Disconnect();
if (logger.WarnEnabled()) logger.LogWarning("Server full, kicked client:" + player);
return;
}

// add connection
AddConnection(player);

// let everyone know we just accepted a connection
Connected?.Invoke(player);
}

//called once a client disconnects from the server
void OnDisconnected(INetworkPlayer player)
{
Expand Down

0 comments on commit 528698b

Please sign in to comment.