コード例 #1
0
        /// <summary>
        /// Attempts to deserialize a packet header by reading it from the specified
        /// <see cref="BinaryReader"/> class instance.
        /// </summary>
        /// <param name="reader">The <see cref="BinaryReader"/> instance to attempt to read
        /// a packet header from.</param>
        /// <returns>An instance of the <see cref="MeisterPacketHeader"/> class if de-serialization was a success. Null
        /// is returned when de-serialization failed.</returns>
        internal static MeisterPacketHeader Deserialize(BinaryReader reader)
        {
            if (reader == null)
                return null;

            //Make sure there's enough data to read an entire header..
            long remaining_size = (reader.BaseStream.Length - reader.BaseStream.Position);
            if (remaining_size < MeisterPacketHeader.HeaderSize)
                return null;

            MeisterPacketHeader header = new MeisterPacketHeader();

            header.Size = reader.ReadUInt16();
            header.SequenceIndex = reader.ReadUInt16();

            byte version = reader.ReadByte();
            if (version != MeisterPacketHeader.Version)
                return null;

            header.Identifier = reader.ReadUInt16();
            header.BodySize = reader.ReadUInt16();

            return header;
        }
コード例 #2
0
ファイル: TestPacket.cs プロジェクト: robindegen/buildmeister
 public bool Deserialize(BinaryReader reader, MeisterPacketHeader header)
 {
     this.Neger = reader.ReadInt32();
     return true;
 }
コード例 #3
0
        /// <summary>
        /// Transmits the specified packet to the remote end point.
        /// </summary>
        /// <param name="packet">The packet to transmit to the remot end point.</param>
        /// <returns>True when transmitting the specified packet was a success.</returns>
        public bool Send(IMeisterPacket packet)
        {
            if (packet == null)
                return false;

            //Create a new binary writer to let the packet serialize to..
            BinaryWriter body_writer = new BinaryWriter(new MemoryStream());

            //Serialize the packet..
            if (!packet.Serialize(body_writer))
                return false;

            //Get the size of the packet body
            long body_size_large = body_writer.BaseStream.Length;
            if (body_size_large > UInt16.MaxValue)
                return false;

            UInt16 body_size = (UInt16)body_size_large;

            //Create a writer for the header
            BinaryWriter header_writer = new BinaryWriter(new MemoryStream());

            //Initialize a new packet header, with the body size and packet identifier
            MeisterPacketHeader header = new MeisterPacketHeader(body_size, 0, packet.GetIdentifier());
            header.Serialize(header_writer);

            //Determine the header size
            UInt16 header_size = MeisterPacketHeader.HeaderSize;

            //Reset the positions of both the writer for the header and body
            header_writer.BaseStream.Position = 0;
            body_writer.BaseStream.Position = 0;

            //Copy the contents of both the packet and header into our buffer
            byte[] send_buffer = new byte[header.Size];
            header_writer.BaseStream.Read(send_buffer, 0, (int)header_size);
            body_writer.BaseStream.Read(send_buffer, (int)header_size, (int)body_size);

            //Start transmission to the remote end point
            this.BaseSocket.BeginSend(send_buffer, 0, send_buffer.Length, SocketFlags.None, OnSend, null);
            return true;
        }