Example #1
0
        /// <summary>
        /// Writes a packet to a stream.
        /// </summary>
        public static void Write(Packet Packet, OutStream Stream)
        {
            // Build flags and header
            PacketFlags flags = PacketFlags.Empty;
            if (Packet.AcknowledgementNumber.HasValue)
                flags |= PacketFlags.Acknowledgement;
            if (Packet.RoundTripTime.HasValue)
                flags |= PacketFlags.RoundTripTime;
            if (Packet.ChunkData != null)
            {
                flags |= PacketFlags.Chunk;
                if (Packet.ChunkInitial)
                    flags |= PacketFlags.ChunkInitial;
                if (Packet.ChunkFinal)
                    flags |= PacketFlags.ChunkFinal;
            }
            if (Packet.PingRequest)
                flags |= PacketFlags.PingRequest;
            if (Packet.PingResponse)
                flags |= PacketFlags.PingResponse;
            if (Packet.Disconnect)
                flags |= PacketFlags.Disconnect;
            Stream.WriteByte((byte)flags);
            Stream.WriteInt(Packet.SequenceNumber);

            // Additional information
            int ack;
            if (Packet.AcknowledgementNumber.TryGetValue(out ack))
                Stream.WriteInt(ack);

            double rtt;
            if (Packet.RoundTripTime.TryGetValue(out rtt))
                Stream.WriteDouble(rtt);

            // Chunk
            if (Packet.ChunkData != null)
                Stream.Write(Packet.ChunkData, 0, Packet.ChunkData.Length);
        }