Пример #1
0
        public static List <DMETypePacketFragment> FromPayload(NetMessageTypes msgClass, byte msgType, byte[] payload, int index, int length)
        {
            List <DMETypePacketFragment> fragments = new List <DMETypePacketFragment>();

            int i = 0;

            while (i < length)
            {
                ushort subPacketSize = (ushort)(length - i);
                if (subPacketSize > Constants.DME_FRAGMENT_MAX_PAYLOAD_SIZE)
                {
                    subPacketSize = Constants.DME_FRAGMENT_MAX_PAYLOAD_SIZE;
                }

                var frag = new DMETypePacketFragment()
                {
                    FragmentMessageClass = (byte)msgClass,
                    FragmentMessageType  = msgType,
                    SubPacketSize        = subPacketSize,
                    SubPacketCount       = 0,
                    SubPacketIndex       = (ushort)fragments.Count,
                    MultiPacketindex     = 0,
                    PacketBufferSize     = length,
                    PacketBufferOffset   = i,
                    Payload = new byte[subPacketSize]
                };

                // Copy payload segment into fragment payload
                Array.Copy(payload, i + index, frag.Payload, 0, subPacketSize);

                //
                fragments.Add(frag);

                // Increment i
                i += subPacketSize;
            }

            // Recorrect fragment counts
            foreach (var frag in fragments)
            {
                frag.SubPacketCount = (ushort)fragments.Count;
            }

            return(fragments);
        }
Пример #2
0
        /// <summary>
        /// Serializes the message.
        /// </summary>
        public List <byte[]> Serialize()
        {
            var results = new List <byte[]>();

            byte[] result          = null;
            var    buffer          = new byte[1024 * 10];
            int    length          = 0;
            int    totalHeaderSize = HEADER_SIZE;

            // Serialize message
            using (MemoryStream stream = new MemoryStream(buffer, true))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    Serialize(writer);
                    length = (int)writer.BaseStream.Position;
                }
            }

            // Check for fragmentation
            if (Id == RT_MSG_TYPE.RT_MSG_SERVER_APP && length > Constants.MEDIUS_MESSAGE_MAXLEN)
            {
                var msgClass  = (NetMessageTypes)buffer[0];
                var msgType   = buffer[1];
                var fragments = DMETypePacketFragment.FromPayload(msgClass, msgType, buffer, 2, length - 2);

                foreach (var frag in fragments)
                {
                    // Serialize message
                    using (MemoryStream stream = new MemoryStream(buffer, true))
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream))
                        {
                            // Serialize header
                            writer.Write((byte)this.Id);
                            writer.Write((ushort)0);

                            // Serialize message
                            new RT_MSG_SERVER_APP()
                            {
                                Message = frag
                            }.Serialize(writer);
                            length = (int)stream.Position - totalHeaderSize;

                            // Write length
                            writer.Seek(1, SeekOrigin.Begin);
                            writer.Write((ushort)length);


                            result = new byte[length + totalHeaderSize];
                            Array.Copy(buffer, 0, result, 0, result.Length);
                            results.Add(result);
                        }
                    }
                }
            }
            else
            {
                // Add id and length to header
                result    = new byte[length + totalHeaderSize];
                result[0] = (byte)this.Id;
                result[1] = (byte)(length & 0xFF);
                result[2] = (byte)((length >> 8) & 0xFF);

                Array.Copy(buffer, 0, result, totalHeaderSize, length);
                results.Add(result);
            }

            return(results);
        }