public void OnTcpReceived(BitBuffer buffer) { if (buffer.TotalBitsLeft < 8) { return; } byte packet = buffer.ReadByte(); if (packet >= TcpHandlers.Length) { return; } byte[] bytes = buffer.ReadBytes(); // ReSharper disable once ConvertToLocalFunction Action handler = () => { Action <BitBuffer> action = TcpHandlers[packet]; if (action != null) { _handlerBuffer.SetContents(bytes); action(_handlerBuffer); } }; if (!NetworkUtils.SimulateNetworkConditions || NetworkUtils.IsServer) { UnityFixedDispatcher.InvokeNoDelay(handler); } else { UnityFixedDispatcher.InvokeDelayed(NetworkUtils.SimulatedNetDelay, handler); } }
public void OnUdpReceived(IDoubleServerClient client, BitBuffer buffer, ushort packetTimestamp) { NetworkServerClient serverClient = (NetworkServerClient)client.ExtraData; if (serverClient.TakeResetPacketTimestamp()) { serverClient.LastPacketTimestamp = packetTimestamp; } else if (!DoubleProtocol.IsPacketNewest(ref serverClient.LastPacketTimestamp, packetTimestamp)) { return; } byte[] bytes = buffer.ReadBytes(); // ReSharper disable once ConvertToLocalFunction Action handler = () => { if (_server != null) { _handlerBuffer.SetContents(bytes); UdpHandler(serverClient, _handlerBuffer); } }; if (!NetworkUtils.SimulateNetworkConditions || NetworkUtils.IsLocal(serverClient.Id)) { UnityFixedDispatcher.InvokeNoDelay(handler); } else { UnityFixedDispatcher.InvokeDelayed(NetworkUtils.SimulatedNetDelay, handler); } }
public void OnAuthenticationTimeout(DoubleClient.State state) { UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { Stop(); _onConnected(false, SocketError.Success, 0, true, false); } }); }
public void OnTcpAuthenticationFailure(byte errorCode) { UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { Stop(); _onConnected(false, SocketError.Success, errorCode, false, false); } }); }
public void OnConnectionFailure(SocketError error) { UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { Stop(); _onConnected(false, error, 0, false, false); } }); }
public void OnUdpReceived(BitBuffer buffer, ushort packetTimestamp) { bool resetPacketTimestamp; lock (SmallLock) { resetPacketTimestamp = _resetPacketTimestamp; _resetPacketTimestamp = false; } if (resetPacketTimestamp) { _lastPacketTimestamp = packetTimestamp; } else if (!DoubleProtocol.IsPacketNewest(ref _lastPacketTimestamp, packetTimestamp)) { return; } byte[] bytes = buffer.ReadBytes(); // ReSharper disable once ConvertToLocalFunction Action handler = () => { if (_client != null) { _handlerBuffer.SetContents(bytes); UdpHandler(_handlerBuffer); } }; // ReSharper disable once PossibleNullReferenceException int netDelayIncrease = DoubleProtocol.TripTime(_client.ConnectionStartTimestamp, packetTimestamp); if (!NetworkUtils.SimulateNetworkConditions || NetworkUtils.IsServer) { UnityFixedDispatcher.InvokeNoDelay(handler); } else if (!NetworkUtils.SimulateLosingPacket) { int delay = NetworkUtils.SimulatedNetDelay; netDelayIncrease += delay; UnityFixedDispatcher.InvokeDelayed(delay, handler); } else { return; } UdpNetDelay += (netDelayIncrease - UdpNetDelay) * 0.1f; }
public void OnLostConnection(IDoubleServerClient client, DoubleServer.ClientState state) { if (state == DoubleServer.ClientState.Authenticated) { UnityFixedDispatcher.InvokeNoDelay(() => { if (_server != null) { NetworkServerClient serverClient = (NetworkServerClient)client.ExtraData; lock (UdpPayloadLock) { //Don't let the TickingThread send before the client is deinitialized _clients.Remove(serverClient); DisconnectHandler(serverClient); } } }); } }
public Action <BitBuffer> OnFullAuthentication(IDoubleServerClient client) { NetworkServerClient serverClient = (NetworkServerClient)client.ExtraData; serverClient.Initialize(); UnityFixedDispatcher.InvokeNoDelay(() => { if (_server != null) { lock (UdpPayloadLock) { //Don't let the TickingThread send before the client is initialized _clients.Add(serverClient); ConnectHandler(serverClient); } } }); return(buffer => { buffer.Write(serverClient.Id); buffer.Write(NetworkUtils.GlobalBaseTimestamp); }); }
private void DamageApply(KeyValuePair <RealLiveBlock, uint>[] damages) { int status = 0; foreach (KeyValuePair <RealLiveBlock, uint> damage in damages) { Health -= damage.Value; if (Health * 100 < MaxHealth * MinHealthPercentage) { status = 1; break; } RealLiveBlock block = damage.Key; if (block.Health == 0) { if (block.Info.Type == BlockType.Mainframe) { status = 1; break; } else { RemoveBlock(block); status = 2; } } } if (status == 1) { Destroy(gameObject); UnityFixedDispatcher.InvokeDelayed(1000, () => LocalPlayingPlayerInitializer.RespawnPlayerStructure(Id)); } else if (status == 2) { RemoveNotConnectedBlocks(); ApplyMass(true); } }
public void OnTcpReceived(IDoubleServerClient client, BitBuffer buffer) { if (buffer.TotalBitsLeft < 8) { return; } byte packet = buffer.ReadByte(); if (packet >= TcpHandlers.Length) { return; } byte[] bytes = buffer.ReadBytes(); NetworkServerClient serverClient = (NetworkServerClient)client.ExtraData; // ReSharper disable once ConvertToLocalFunction Action handler = () => { if (_server != null) { OnPacketReceived action = TcpHandlers[packet]; if (action != null) { _handlerBuffer.SetContents(bytes); action((NetworkServerClient)client.ExtraData, _handlerBuffer); } } }; if (!NetworkUtils.SimulateNetworkConditions || NetworkUtils.IsLocal(serverClient.Id)) { UnityFixedDispatcher.InvokeNoDelay(handler); } else { UnityFixedDispatcher.InvokeDelayed(NetworkUtils.SimulatedNetDelay, handler); } }
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)); } } }); } }); }
public void OnConnectionLost(DoubleClient.State state) { if (state == DoubleClient.State.Authenticated) { UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { OnDisconnected handler = DisconnectHandler; Stop(); handler(); } }); } else { UnityFixedDispatcher.InvokeNoDelay(() => { if (_client != null) { Stop(); _onConnected(false, SocketError.Success, 0, false, true); } }); } }