Exemplo n.º 1
0
        // sends a connection challenge packet to the endpoint
        private void sendConnectionChallenge(NetcodePrivateConnectToken connectToken, EndPoint endpoint)
        {
            log("Sending connection challenge", NetcodeLogLevel.Debug);

            var challengeToken = new NetcodeChallengeToken();

            challengeToken.ClientID = connectToken.ClientID;
            challengeToken.UserData = connectToken.UserData;

            ulong challengeSequence = nextChallengeSequenceNumber++;

            byte[] tokenBytes = BufferPool.GetBuffer(300);
            using (var tokenWriter = ByteArrayReaderWriter.Get(tokenBytes))
                challengeToken.Write(tokenWriter);

            byte[] encryptedToken = BufferPool.GetBuffer(300);
            int    encryptedTokenBytes;

            try
            {
                encryptedTokenBytes = PacketIO.EncryptChallengeToken(challengeSequence, tokenBytes, challengeKey, encryptedToken);
            }
            catch
            {
                BufferPool.ReturnBuffer(tokenBytes);
                BufferPool.ReturnBuffer(encryptedToken);
                return;
            }

            var challengePacket = new NetcodeConnectionChallengeResponsePacket();

            challengePacket.ChallengeTokenSequence = challengeSequence;
            challengePacket.ChallengeTokenBytes    = encryptedToken;

            var cryptIdx = encryptionManager.FindEncryptionMapping(endpoint, time);

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

            var cryptKey = encryptionManager.GetSendKey(cryptIdx);

            serializePacket(new NetcodePacketHeader()
            {
                PacketType = NetcodePacketType.ConnectionChallenge
            }, (writer) =>
            {
                challengePacket.Write(writer);
            }, endpoint, cryptKey);

            BufferPool.ReturnBuffer(tokenBytes);
            BufferPool.ReturnBuffer(encryptedToken);
        }