private void sendConnectionRequest(EndPoint server) { byte[] packetBuffer = BufferPool.GetBuffer(1 + 13 + 8 + 8 + 8 + Defines.NETCODE_CONNECT_TOKEN_PRIVATE_BYTES); using (var stream = ByteArrayReaderWriter.Get(packetBuffer)) { stream.Write((byte)0); stream.WriteASCII(Defines.NETCODE_VERSION_INFO_STR); stream.Write(connectToken.ProtocolID); stream.Write(connectToken.ExpireTimestamp); stream.Write(connectToken.ConnectTokenSequence); stream.Write(connectToken.PrivateConnectTokenBytes); } socket.SendTo(packetBuffer, server); BufferPool.ReturnBuffer(packetBuffer); }
// 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, endpoint); BufferPool.ReturnBuffer(packetBuffer); BufferPool.ReturnBuffer(encryptedPacketBuffer); }