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); } }
private static CompleteStructure CreateStructure(byte playerId) { MutableBitBuffer buffer = new MutableBitBuffer(); buffer.SetContents(PlayerStructures[playerId]); CompleteStructure structure = CompleteStructure.Create(buffer, playerId); Assert.IsNotNull(structure, "The example structure creation must be successful."); structure.transform.position = new Vector3(0, 10, 0); return(structure); }
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); } }
private void OnTcpPacketAssembled(Socket sender, byte[] buffer, int offset, int size) { lock (this) { if (_closed) { return; } DoubleServerClient client = _tcpClients[sender]; _receiveBuffer.SetContents(buffer, offset, size); if (client.State == ClientState.TcpAuthenticating) { if (_handler.TcpAuthenticateClient(client, _receiveBuffer, out byte[] encryptionKey, out byte errorCode))
public void ResetStructure() { string file = GetFilePath(); BitBuffer data; if (File.Exists(file)) { MutableBitBuffer temp = new MutableBitBuffer(); temp.SetContents(File.ReadAllBytes(file)); data = temp; } else { data = DefaultStructure; } Assert.IsTrue(_structure.Deserialize(data), "Failed to load the structure."); }
private void OnTcpPacketAssembled(Socket ignored, byte[] buffer, int offset, int size) { lock (this) { if (CurrentState == State.Disconnected) { return; } _receiveBuffer.SetContents(_crypto.Decrypt(buffer, offset, size)); if (CurrentState == State.TcpAuthenticating) { if (_receiveBuffer.Array.Length == 1) { Close(); _handler.OnTcpAuthenticationFailure(_receiveBuffer.ReadByte()); } else { CurrentState = State.UdpAuthenticating; _sequenceIdBound = _receiveBuffer.ReadByte(); byte[] udpAuthenticationKey = _receiveBuffer.ReadBytes(8); ConnectionStartTimestamp = _receiveBuffer.ReadLong(); _udp.Start(_ip, _port); Task.Run(() => { lock (this) { int counter = 0; while (counter++ < UdpAuthenticationPacketSendCount) { _udp.Send(udpAuthenticationKey, 0, udpAuthenticationKey.Length); Monitor.Wait(this, 1000 / UdpAuthenticationPacketFrequency); if (CurrentState != State.UdpAuthenticating) { return; } } OnAuthenticationTimeout(); } }); } } else if (CurrentState == State.UdpAuthenticating) { if (_receiveBuffer.ReadByte() == 0) { CurrentState = State.Authenticated; _handler.OnFullAuthentication(_receiveBuffer); } } else { if (_receiveBuffer.ReadByte() == _receiveSequenceId) { if (++_receiveSequenceId == _sequenceIdBound) { _receiveSequenceId = 0; } _handler.OnTcpReceived(_receiveBuffer); } } } }
static BuildingController() { _exampleStructure.SetContents(new byte[] { 2, 240, 129, 94, 35, 128, 15, 252, 146, 0, 124, 32, 152, 4, 224, 3, 195, 36, 0, 31, 40, 38, 1, 248, 192, 49, 13, 192, 7, 146, 105, 0, 190, 208, 19, 7, 240, 133, 96, 60, 128, 47, 12, 131, 1, 124, 161, 24, 2, 0, 4, 63, 0, 0, 32, 8, 138, 0, 0, 193, 49, 9, 0, 8, 146, 12, 1, 192, 240, 43, 6, 0, 134, 163, 72, 0, 80, 20, 133, 0, 132, 160, 215, 8, 32, 4, 63, 32, 0, 33, 8, 34, 1, 8, 193, 16, 9, 64, 8, 138, 72, 0, 66, 112, 68, 3, 16, 130, 36, 27, 128, 48, 244, 244, 1, 132, 33, 8, 15, 32, 12, 67, 96, 0, 97, 40, 2 }); }