/// <summary> /// Deinitializes the networking, kicking all connected clients and stopping the server. /// </summary> public static void Stop() { Assert.IsNotNull(_server, "The NetworkServer is not initialized."); UdpPayload = null; _tickingThread?.Stop(); _tickingThread = null; NetworkUtils.GlobalBaseTimestamp = -1; Array.Clear(TcpHandlers, 0, TcpHandlers.Length); _resettingByteBuffer = null; _clients = null; _handler = null; _server.Close(); _server = null; }
/// <summary> /// Deinitializes the networking, disconnecting from the server. /// Always called internally when the connection fails or this client gets disconnected, /// so should only be to make this client disconnect. /// </summary> public static void Stop() { Assert.IsNotNull(_client, "The NetworkClient is not initialized."); UdpPayload = null; _tickingThread?.Stop(); _tickingThread = null; NetworkUtils.GlobalBaseTimestamp = -1; UdpNetDelay = 0; _resetPacketTimestamp = false; DisconnectHandler = null; UdpHandler = null; Array.Clear(TcpHandlers, 0, TcpHandlers.Length); _handler = null; _client.Close(); _client = null; }
public void OnFullAuthentication(BitBuffer buffer) { byte localId = buffer.ReadByte(); long globalBaseTimestamp = buffer.ReadLong(); UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { LocalId = localId; NetworkUtils.GlobalBaseTimestamp = globalBaseTimestamp; _onConnected(true, SocketError.Success, 0, false, false); _tickingThread = new TickingThread(NetworkUtils.UdpSendFrequency, () => { lock (UdpPayloadLock) { if (_udpPayload != null) { _client.SendUdp(buff => buff.Write(_udpPayload)); } } }); } }); }
/// <summary> /// Initializes the networking and starts accepting connections. /// </summary> public static void Start() { Assert.IsNull(_server, "The NetworkServer is already initialized."); NetworkUtils.GlobalBaseTimestamp = DoubleProtocol.TimeMillis; _resettingByteBuffer = new ResettingBitBuffer(DoubleProtocol.TcpBufferArraySize); _clients = new HashSet <NetworkServerClient>(); _handler = new DoubleServerHandler(); _server = new DoubleServer(_handler, NetworkUtils.MaxBotCount, (NetworkUtils.MaxBotCount * 3 + 1) / 2, NetworkUtils.Port); _tickingThread = new TickingThread(NetworkUtils.UdpSendFrequency, () => { lock (UdpPayloadLock) { if (_udpPayload != null && _udpPayload.Length != 0) { foreach (NetworkServerClient serverClient in _clients) { _server.SendUdp(serverClient.DoubleClient, buffer => buffer.Write(UdpPayload)); } } } }); }