public void Subscribe(ServerConnectionDelegate onConnectionHandler) => OnEntiredConnection += onConnectionHandler;
public void StartServer(NetworkMode _NetworkMode, int _ConnectionCountMax, int _ConnectPort, string _ServerName, ServerEventDelegate _ServerStartedDelegate, ServerEventDelegate _ServerStoppedDelegate, ServerConnectionDelegate _ClientConnectedDelegate, ServerConnectionDelegate _ClientDisconnectedDelegate) { Debug.Log("Starting Server..."); System.Diagnostics.Debug.Assert(Network.peerType == NetworkPeerType.Disconnected); System.Diagnostics.Debug.Assert(CanStartServer()); bool useNatPunchThrough = (_NetworkMode == NetworkMode.E_NetworkOnlineOnly) && !Network.HavePublicAddress(); m_PublicServerName = _ServerName; m_NetworkMode = _NetworkMode; m_ConnectionCountMax = _ConnectionCountMax; m_ConnectPort = _ConnectPort; m_IsUsingNatPunchThrough = useNatPunchThrough; m_ServerStartedDelegate = _ServerStartedDelegate; m_ServerStoppedDelegate = _ServerStoppedDelegate; m_ClientConnectedDelegate = _ClientConnectedDelegate; m_ClientDisconnectedDelegate = _ClientDisconnectedDelegate; ChangeServerState(ServerState.E_ServerPendingStart); NetworkConnectionError initServerResult = Network.InitializeServer(_ConnectionCountMax, _ConnectPort, useNatPunchThrough); if (initServerResult != NetworkConnectionError.NoError) { Debug.Log("Init Server failed:" + initServerResult + " (port: " + _ConnectPort + ")" ); //@TODO reset member variables } }
void OnPlayerDisconnected(NetworkPlayer _NetPlayer) { Debug.Log("Client disconnected: " + _NetPlayer.ipAddress + ":" + _NetPlayer.port); if (m_ClientDisconnectedDelegate != null) { m_ClientDisconnectedDelegate(_NetPlayer); m_ClientDisconnectedDelegate = null; } Network.RemoveRPCs(_NetPlayer); Network.DestroyPlayerObjects(_NetPlayer); }
void OnPlayerConnected(NetworkPlayer _NetPlayer) { Debug.Log("New client connected: " + _NetPlayer.ipAddress + ":" + _NetPlayer.port); if (m_ClientConnectedDelegate != null) { m_ClientConnectedDelegate(_NetPlayer); m_ClientConnectedDelegate = null; } }
void OnDisconnectedFromServer(NetworkDisconnection _NetDisconnectionInfo) { Debug.Log("Server connection disconnected: " + _NetDisconnectionInfo.ToString()); Debug.Log("Current network peer type is " + Network.peerType.ToString()); m_ServerStartedDelegate = null; m_ServerStoppedDelegate = null; m_ClientConnectedDelegate = null; m_ClientDisconnectedDelegate = null; StopServerAdvertising(); ChangeServerState(ServerState.E_ServerStopped); if (m_ServerStoppedDelegate != null) { m_ServerStoppedDelegate(); m_ServerStoppedDelegate = null; } }
public void StopServer(int _DisconnectTimeOutInMilliseconds) { Debug.Log("Stopping Server..."); ChangeServerState(ServerState.E_ServerPendingStop); m_ServerStartedDelegate = null; m_ServerStoppedDelegate = null; m_ClientConnectedDelegate = null; m_ClientDisconnectedDelegate = null; StopServerAdvertising(); //@FIXME: nothing provided in Unity3d Network API to specifically stop the server Network.Disconnect(_DisconnectTimeOutInMilliseconds); }