Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        // send a keep-alive packet to the client
        private void sendKeepAlive(RemoteClient client)
        {
            var packet = new NetcodeKeepAlivePacket()
            {
                ClientIndex = client.ClientIndex, MaxSlots = (uint)this.maxSlots
            };

            var cryptIdx = encryptionManager.FindEncryptionMapping(client.RemoteEndpoint, time);

            if (cryptIdx == -1)
            {
                return;
            }

            var cryptKey = encryptionManager.GetSendKey(cryptIdx);

            serializePacket(new NetcodePacketHeader()
            {
                PacketType = NetcodePacketType.ConnectionKeepAlive
            }, (writer) =>
            {
                packet.Write(writer);
            }, client.RemoteEndpoint, cryptKey);
        }
Exemplo n.º 3
0
        // 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);
        }