Exemple #1
0
            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);
                }
            }
Exemple #2
0
            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);
                }
            }
Exemple #3
0
 public void OnAuthenticationTimeout(DoubleClient.State state)
 {
     UnityFixedDispatcher.InvokeNoDelay(() => {
         if (_client != null)
         {
             Stop();
             _onConnected(false, SocketError.Success, 0, true, false);
         }
     });
 }
Exemple #4
0
 public void OnTcpAuthenticationFailure(byte errorCode)
 {
     UnityFixedDispatcher.InvokeNoDelay(() => {
         if (_client != null)
         {
             Stop();
             _onConnected(false, SocketError.Success, errorCode, false, false);
         }
     });
 }
Exemple #5
0
 public void OnConnectionFailure(SocketError error)
 {
     UnityFixedDispatcher.InvokeNoDelay(() => {
         if (_client != null)
         {
             Stop();
             _onConnected(false, error, 0, false, false);
         }
     });
 }
Exemple #6
0
            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;
            }
Exemple #7
0
 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);
                 }
             }
         });
     }
 }
Exemple #8
0
            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);
                });
            }
Exemple #9
0
            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);
                }
            }
Exemple #10
0
            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));
                                }
                            }
                        });
                    }
                });
            }
Exemple #11
0
 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);
             }
         });
     }
 }