Ejemplo n.º 1
0
        public static void SendPacket(UdpClient client, IPEndPoint endPoint,
                                      ChunkConstruct packet)
        {
            if (packet.DataSize == 0)
            {
                return;
            }

            Debug.Assert((packet.Token & ~TokenHelper.TokenMask) == 0, "token out of range");

            var buffer         = new byte[MaxPacketSize];
            var compressedSize = -1;

            if (!packet.Flags.HasFlag(PacketFlags.Control))
            {
                compressedSize = Huffman.Compress(packet.Data, 0, packet.DataSize,
                                                  buffer, PacketHeaderSize, MaxPayload);
            }

            int finalSize;

            if (compressedSize > 0 && compressedSize < packet.DataSize)
            {
                finalSize     = compressedSize;
                packet.Flags |= PacketFlags.Compression;
            }
            else
            {
                finalSize = packet.DataSize;
                Buffer.BlockCopy(packet.Data, 0, buffer, PacketHeaderSize, packet.DataSize);
                packet.Flags &= ~PacketFlags.Compression;
            }

            finalSize += PacketHeaderSize;
            buffer[0]  = (byte)((((int)packet.Flags << 2) & 0xfc) | ((packet.Ack >> 8) & 0x03));  // flags and ack
            buffer[1]  = (byte)((packet.Ack) & 0xff);
            buffer[2]  = (byte)((packet.NumChunks) & 0xff);
            buffer[3]  = (byte)((packet.Token >> 24) & 0xff);
            buffer[4]  = (byte)((packet.Token >> 16) & 0xff);
            buffer[5]  = (byte)((packet.Token >> 8) & 0xff);
            buffer[6]  = (byte)((packet.Token) & 0xff);

            client.Send(buffer, finalSize, endPoint);
        }
Ejemplo n.º 2
0
        public static void SendPacket(UdpClient udpClient, IPEndPoint endPoint,
                                      NetworkChunkConstruct packet)
        {
            var buffer         = new byte[MAX_PACKET_SIZE];
            var headerSize     = PACKET_HEADER_SIZE_WITHOUT_TOKEN;
            var compressedSize = -1;

            if (packet.Flags.HasFlag(PacketFlags.TOKEN))
            {
                headerSize = PACKET_HEADER_SIZE;
                packet.Token.ToByteArray(buffer, 3);
            }

            if (!packet.Flags.HasFlag(PacketFlags.CONTROL))
            {
                compressedSize = _huffman.Compress(packet.Data, 0, packet.DataSize,
                                                   buffer, headerSize, buffer.Length - headerSize);
            }

            //compressedSize = _huffman.Compress(packet.Data, 0, packet.DataSize,
            //    buffer, PACKET_HEADER_SIZE, buffer.Length - PACKET_HEADER_SIZE);

            int finalSize;

            if (compressedSize > 0 && compressedSize < packet.DataSize)
            {
                finalSize     = compressedSize;
                packet.Flags |= PacketFlags.COMPRESSION;
            }
            else
            {
                finalSize = packet.DataSize;
                Buffer.BlockCopy(packet.Data, 0, buffer, headerSize, packet.DataSize);
                packet.Flags &= ~PacketFlags.COMPRESSION;
            }

            finalSize += headerSize;
            buffer[0]  = (byte)((((int)packet.Flags << 2) & 0xfc) | ((packet.Ack >> 8) & 0x3));
            buffer[1]  = (byte)(packet.Ack & 0xff);
            buffer[2]  = (byte)packet.NumChunks;
            udpClient.Send(buffer, finalSize, endPoint);
        }