Esempio n. 1
0
        private void sendPacket(NetcodePacketHeader packetHeader, byte[] packetData, int packetDataLen, byte[] key)
        {
            // assign a sequence number to this packet
            packetHeader.SequenceNumber = this.nextPacketSequence++;

            // encrypt packet data
            byte[] encryptedPacketBuffer = BufferPool.GetBuffer(2048);
            int    encryptedBytes        = PacketIO.EncryptPacketData(packetHeader, connectToken.ProtocolID, packetData, packetDataLen, key, encryptedPacketBuffer);

            int packetLen = 0;

            // write packet to byte array
            var packetBuffer = BufferPool.GetBuffer(2048);

            using (var packetWriter = ByteArrayReaderWriter.Get(packetBuffer))
            {
                packetHeader.Write(packetWriter);
                packetWriter.WriteBuffer(encryptedPacketBuffer, encryptedBytes);

                packetLen = (int)packetWriter.WritePosition;
            }

            // send packet
            try {
                socket.SendTo(packetBuffer, packetLen, currentServerEndpoint);
            }
            catch { }

            BufferPool.ReturnBuffer(packetBuffer);
            BufferPool.ReturnBuffer(encryptedPacketBuffer);
        }
Esempio n. 2
0
        // encrypts a packet and sends it to the endpoint
        private void sendPacketToClient(NetcodePacketHeader packetHeader, byte[] packetData, int packetDataLen, EndPoint endpoint, byte[] key)
        {
            // assign a sequence number to this packet
            packetHeader.SequenceNumber = this.nextSequenceNumber++;

            // encrypt packet data
            byte[] encryptedPacketBuffer = BufferPool.GetBuffer(2048);
            int    encryptedBytes        = PacketIO.EncryptPacketData(packetHeader, protocolID, packetData, packetDataLen, key, encryptedPacketBuffer);

            int packetLen = 0;

            // write packet to byte array
            var packetBuffer = BufferPool.GetBuffer(2048);

            using (var packetWriter = ByteArrayReaderWriter.Get(packetBuffer))
            {
                packetHeader.Write(packetWriter);
                packetWriter.WriteBuffer(encryptedPacketBuffer, encryptedBytes);

                packetLen = (int)packetWriter.WritePosition;
            }

            // send packet
            listenSocket.SendTo(packetBuffer, packetLen, SocketFlags.None, endpoint);

            BufferPool.ReturnBuffer(packetBuffer);
            BufferPool.ReturnBuffer(encryptedPacketBuffer);
        }