Beispiel #1
0
        private void processConnectionKeepAlive(NetcodePacketHeader header, int length, ByteArrayReaderWriter stream)
        {
            if (checkReplay(header))
            {
                return;
            }

            var decryptKey = serverToClientKey;
            var keepAlive  = new NetcodeKeepAlivePacket()
            {
                Header = header
            };

            if (!keepAlive.Read(stream, length, decryptKey, connectToken.ProtocolID))
            {
                return;
            }

            if (this.state == ClientState.Connected || this.state == ClientState.SendingChallengeResponse)
            {
                lastResponseTime = time;
            }

            if (this.state == ClientState.SendingChallengeResponse)
            {
                this.clientIndex = keepAlive.ClientIndex;
                this.maxSlots    = keepAlive.MaxSlots;
                changeState(ClientState.Connected);
            }
        }
        // process an incoming connection keep alive packet
        private void processConnectionKeepAlive(ByteArrayReaderWriter reader, NetcodePacketHeader header, int size, EndPoint sender)
        {
            if (checkReplay(header, sender))
            {
                log("Detected replay in keep-alive", NetcodeLogLevel.Debug);
                return;
            }

            // encryption mapping was not registered, so don't bother
            int cryptIdx = encryptionManager.FindEncryptionMapping(sender, time);

            if (cryptIdx == -1)
            {
                log("No crytpo key for sender", NetcodeLogLevel.Debug);
                return;
            }

            // grab the decryption key and decrypt the packet
            var decryptKey = encryptionManager.GetReceiveKey(cryptIdx);

            var keepAlivePacket = new NetcodeKeepAlivePacket()
            {
                Header = header
            };

            if (!keepAlivePacket.Read(reader, size - (int)reader.ReadPosition, decryptKey, protocolID))
            {
                log("Failed to decrypt", NetcodeLogLevel.Debug);
                return;
            }

            if (keepAlivePacket.ClientIndex >= maxSlots)
            {
                log("Invalid client index", NetcodeLogLevel.Debug);
                return;
            }

            var client = this.clientSlots[(int)keepAlivePacket.ClientIndex];

            if (client == null)
            {
                log("Failed to find client for endpoint", NetcodeLogLevel.Debug);
                return;
            }

            if (!client.RemoteEndpoint.Equals(sender))
            {
                log("Client does not match sender", NetcodeLogLevel.Debug);
                return;
            }

            if (!client.Confirmed)
            {
                // trigger callback
                if (OnClientConnected != null)
                {
                    OnClientConnected(client);
                }

                log("Client {0} connected", NetcodeLogLevel.Info, client.RemoteEndpoint);
            }

            client.Confirmed = true;

            client.Touch(time);

            int idx = encryptionManager.FindEncryptionMapping(client.RemoteEndpoint, time);

            encryptionManager.Touch(idx, client.RemoteEndpoint, time);
        }