Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes the TSM from the C-DEngine binary format
        /// This is not used for the HTTP channel but reserverd for future use on other TCP/UPD channels
        /// </summary>
        /// <param name="pInBuffer"></param>
        /// <param name="pOffset"></param>
        /// <returns></returns>
        internal static TSM FromPacketBytes(byte[] pInBuffer, int pOffset)
        {
            int numberOfDataEntries = 0;

            byte[] dataBytes = null;
            TSM    tNewTSM   = new TSM(); //Save not to write ORG or SID

            using (MemoryStream byteStream = new MemoryStream(pInBuffer, pOffset, pInBuffer.Length - pOffset))
            {
                int packetSize = (byteStream.ReadByte() << 0);
                packetSize |= (byteStream.ReadByte() << 8);
                packetSize |= (byteStream.ReadByte() << 16);
                packetSize |= (byteStream.ReadByte() << 24);
                if (packetSize != pInBuffer.Length)
                {
                    throw new InvalidOperationException("Invalid packet size");
                }

                long timeBinary = byteStream.ReadByte();
                timeBinary |= ((long)byteStream.ReadByte()) << 8;
                timeBinary |= ((long)byteStream.ReadByte()) << 16;
                timeBinary |= ((long)byteStream.ReadByte()) << 24;
                timeBinary |= ((long)byteStream.ReadByte()) << 32;
                timeBinary |= ((long)byteStream.ReadByte()) << 40;
                timeBinary |= ((long)byteStream.ReadByte()) << 48;
                timeBinary |= ((long)byteStream.ReadByte()) << 56;

                int timeOffsetInMinutes = (byteStream.ReadByte() << 0);
                timeOffsetInMinutes |= (byteStream.ReadByte() << 8);

                tNewTSM.TIM = new DateTimeOffset(DateTime.FromFileTime(timeBinary), new TimeSpan(0, timeOffsetInMinutes, 0));

                tNewTSM.LVL         = (eMsgLevel)byteStream.ReadByte();
                tNewTSM.FLG         = (ushort)(((ushort)byteStream.ReadByte()) << 8);
                tNewTSM.FLG        += (ushort)byteStream.ReadByte();
                tNewTSM.QDX        += byteStream.ReadByte();
                numberOfDataEntries = byteStream.ReadByte();
                int PLBlength = byteStream.ReadByte();
                PLBlength += byteStream.ReadByte() << 8;
                PLBlength += byteStream.ReadByte() << 16;
                PLBlength += byteStream.ReadByte() << 24;
                dataBytes  = new byte[packetSize - (21 + PLBlength + pOffset)];
                byteStream.Read(dataBytes, 0, dataBytes.Length);

                if (PLBlength > 0)
                {
                    tNewTSM.PLB = new byte[PLBlength];
                    byteStream.Read(tNewTSM.PLB, 0, PLBlength);
                }
            }

            int byteIndex = 0;

            for (int n = 0; n < numberOfDataEntries; n++)
            {
                //int dataStringLength = BitConverter.ToInt32(dataBytes, byteIndex);
                int dataStringLength = (dataBytes[byteIndex] << 0);
                dataStringLength |= (dataBytes[byteIndex + 1] << 8);
                dataStringLength |= (dataBytes[byteIndex + 2] << 16);
                dataStringLength |= (dataBytes[byteIndex + 3] << 24);
                byteIndex        += 4;

                string dataString = TheCommonUtils.CArray2UTF8String(dataBytes, byteIndex, dataStringLength);
                byteIndex += dataStringLength;

                switch (n)
                {
                case 0: tNewTSM.TXT = dataString; break;

                case 1: tNewTSM.PLS = dataString; break;

                case 2: tNewTSM.ORG = dataString; break;    //ORG-OK

                case 3: tNewTSM.ENG = dataString; break;

                case 4: tNewTSM.FID = dataString; break;

                case 5: tNewTSM.SID = dataString; break;

                case 6: tNewTSM.SEID = dataString; break;

                case 7: tNewTSM.UID = dataString; break;

                case 8: tNewTSM.CST = dataString; break;

                case 9: tNewTSM.OWN = dataString; break;
                }
            }
            return(tNewTSM);
        }