Exemple #1
0
        public override void Deserialize(BinaryReader reader)
        {
            MediusAppPacketIds id = reader.Read <MediusAppPacketIds>();

            try
            {
                AppMessage = BaseAppMessage.Instantiate(id, reader);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error deserializing {Id}:{id}");
                throw e;
            }
        }
        public static List <DMETypePacketFragment> FromPayload(MediusAppPacketIds id, byte[] payload, int index, int length)
        {
            List <DMETypePacketFragment> fragments = new List <DMETypePacketFragment>();

            byte messageClass = (byte)id;
            byte messageType  = (byte)(((int)id) >> 8);

            int i = 0;

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

                var frag = new DMETypePacketFragment()
                {
                    MessageClass       = messageClass,
                    MessageType        = messageType,
                    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);
        }
Exemple #3
0
        /// <summary>
        /// Serializes the message.
        /// </summary>
        public void Serialize(out List <byte[]> results)
        {
            results = new List <byte[]>();
            byte[] result = null;
            var    buffer = new byte[1024 * 10];
            int    length = 0;

            // 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 > MediusConstants.MEDIUS_MESSAGE_MAXLEN)
            {
                MediusAppPacketIds appId = (MediusAppPacketIds)((buffer[1] << 8) | buffer[0]);
                var fragments            = DMETypePacketFragment.FromPayload(appId, buffer, 2, length - 2);

                foreach (var frag in fragments)
                {
                    new RT_MSG_SERVER_APP()
                    {
                        AppMessage = frag
                    }.Serialize(out var fragBuffers);
                    results.AddRange(fragBuffers);
                }
            }
            else
            {
                // Add id and length to header
                result    = new byte[length + 3];
                result[0] = (byte)this.Id;
                result[1] = (byte)(length & 0xFF);
                result[2] = (byte)((length >> 8) & 0xFF);

                Array.Copy(buffer, 0, result, 3, length);
                results.Add(result);
            }
        }
Exemple #4
0
        public static BaseAppMessage Instantiate(MediusAppPacketIds id, BinaryReader reader)
        {
            BaseAppMessage msg;

            // Init
            Initialize();

            // Instantiate
            if (!_messageClassById.TryGetValue(id, out var classType))
            {
                msg = new RawAppMessage(id);
            }
            else
            {
                msg = (BaseAppMessage)Activator.CreateInstance(classType);
            }

            // Deserialize
            msg.Deserialize(reader);
            return(msg);
        }
Exemple #5
0
        public override void Deserialize(BinaryReader reader)
        {
            MediusAppPacketIds id = reader.Read <MediusAppPacketIds>();

            AppMessage = BaseAppMessage.Instantiate(id, reader);
        }
 public static List <DMETypePacketFragment> FromPayload(MediusAppPacketIds id, byte[] payload)
 {
     return(FromPayload(id, payload, 0, payload.Length));
 }
Exemple #7
0
 public MediusAppAttribute(MediusAppPacketIds id)
 {
     PacketId = id;
 }
Exemple #8
0
 public RawAppMessage(MediusAppPacketIds id)
 {
     _id = (ushort)id;
 }