Exemple #1
0
        public DynamicVCPDU ToPdu(byte[] data)
        {
            if (null == regsiteredPDUs)
            {
                regsiteredPDUs = new List <DynamicVCPDU>();
                RegisterDefaultPdus();
            }

            DynamicVCPDU res = null;

            foreach (DynamicVCPDU pdu in regsiteredPDUs)
            {
                if (PduMarshaler.Unmarshal(data, pdu))
                {
                    res = pdu;
                    break;
                }
            }

            if (res == null)
            {
                DynamicVCException.Throw("UnknownDynamicVCPDU was not registered.");
            }

            // Clear references in the list and register new PDUs.
            regsiteredPDUs.Clear();
            RegisterDefaultPdus();

            return(res);
        }
        public int GetNonDataSize()
        {
            int       channelIdSize = 0;
            const int headerSize    = 1;

            // TODO: refine
            switch (this.HeaderBits.CbChannelId)
            {
            case cbChId_Values.OneByte:
                channelIdSize = 1;
                break;

            case cbChId_Values.TwoBytes:
                channelIdSize = 2;
                break;

            case cbChId_Values.FourBytes:
                channelIdSize = 4;
                break;

            case cbChId_Values.Invalid:
            default:
                DynamicVCException.Throw("channelIdSize is invalid.");
                break;
            }

            return(headerSize + channelIdSize);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public SoftSyncReqDvcPDU(SoftSyncReqFlags_Value flags, ushort numberOfTunnels, SoftSyncChannelList[] softSyncChannelLists)
        {
            HeaderBits = new Header(Cmd_Values.SoftSyncReq, 0, 0);
            this.Pad = 0;            
            this.Flags = flags;
            this.NumberOfTunnels = numberOfTunnels;
            this.SoftSyncChannelLists = softSyncChannelLists;
            // Section 2.2.5.1, Length (4 bytes): A 32-bit, unsigned integer indicating the total size, in bytes, of SoftSyncChannelLists field.
            // TDI: this length should also including the length of Length(4 bytes), Flags(2 bytes) and NumberOfTunnels(2 bytes) in DYNVC_SOFT_SYNC_REQUEST PDU.
            // this length value is Length(4) + Flags(2) + NumberOfTunnels(2) = 8.  
            this.Length = 8;

            if(flags.HasFlag(SoftSyncReqFlags_Value.SOFT_SYNC_CHANNEL_LIST_PRESENT))
            {
                if (softSyncChannelLists == null)
                {
                    DynamicVCException.Throw("Should have one or more Soft-Sync Channel Lists.");
                }

                foreach (var channel in softSyncChannelLists)
                {
                    this.Length += (uint)channel.GetSize();
                }
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public SoftSyncReqDvcPDU(SoftSyncReqFlags_Value flags, ushort numberOfTunnels, SoftSyncChannelList[] softSyncChannelLists)
        {
            HeaderBits                = new Header(Cmd_Values.SoftSyncReq, 0x00, 0x00);
            this.Pad                  = 0x00;
            this.Flags                = flags;
            this.NumberOfTunnels      = numberOfTunnels;
            this.SoftSyncChannelLists = softSyncChannelLists;

            // Section 2.2.5.1, Length (4 bytes): A 32-bit, unsigned integer indicating the total size, in bytes, of the Length, Flags, NumberOfTunnels, and SoftSyncChannelLists fields.

            this.Length = 8;

            if (flags.HasFlag(SoftSyncReqFlags_Value.SOFT_SYNC_CHANNEL_LIST_PRESENT))
            {
                if (softSyncChannelLists == null)
                {
                    DynamicVCException.Throw("Should have one or more Soft-Sync Channel Lists.");
                }

                foreach (var channel in softSyncChannelLists)
                {
                    this.Length += (uint)channel.GetSize();
                }
            }
        }
        public void AppendData(byte[] data)
        {
            if (null == buffer)
            {
                DynamicVCException.Throw("Haven't added the first buffer yet.");
                return;
            }

            int len = data.Length;

            SimpleLogger.Log("DataFragmentManager: Append a data fragment, length: {0}, total: {1}", len, Length);

            long expected = (long)Length - buffer.Length;

            if ((long)len > expected)
            {
                DynamicVCException.Throw("The length of the data is larger than expected.");
            }
            buffer.Write(data, 0, len);

            if (0 == expected)
            {
                Completed = true;
            }
        }
        /// <summary>
        /// Compute the Non Data size.
        /// </summary>
        /// <param name="isSp">Indicates the 3-4 bits are Sp or Len</param>
        /// <returns></returns>
        public int NonDataSize(bool isSp)
        {
            // Header length = 1;
            int len = 1;

            // cbChId indicates the length of the ChannelId field.
            switch (this.HeaderBits.CbChannelId)
            {
            case cbChId_Values.OneByte:
                len += 1;
                break;

            case cbChId_Values.TwoBytes:
                len += 2;
                break;

            case cbChId_Values.FourBytes:
                len += 4;
                break;

            case cbChId_Values.Invalid:
            default:
                DynamicVCException.Throw("The length of the ChannelId field is invalid.");
                break;
            }

            if (!isSp)
            {
                // If the 3-4 bit is Len, this value indicates the length of Length field.
                // Length field is applied to DYNVC_DATA_FIRST and DYNVC_DATA_FIRST_COMPRESSED structures.
                switch ((Len_Values)this.HeaderBits.Sp)
                {
                case Len_Values.OneByte:
                    len += 1;
                    break;

                case Len_Values.TwoBytes:
                    len += 2;
                    break;

                case Len_Values.FourBytes:
                    len += 4;
                    break;

                case Len_Values.Invalid:
                default:
                    DynamicVCException.Throw("The length of Length field is invalid.");
                    break;
                }
            }

            return(len);
        }
 private static void HandleException(DynamicVCException e)
 {
     if (IsMainThread() && (!cleanupThread))
     {
         // Not work for protocol based adapter Need more investigation
         // throw e;
     }
     else if (unprocessedException == null)
     {
         unprocessedException = e;
     }
 }
Exemple #8
0
        /// <summary>
        /// Create the DYNVC_DATA PDU
        /// </summary>
        /// <param name="channelId">The channelId</param>
        /// <param name="data">The uncompressed data</param>
        /// <param name="channelChunkLength">The maximum number of uncompressed bytes in a single segment </param>
        /// <returns>Return the first and second PDUs in an array</returns>
        public DataDvcBasePdu[] CreateDataPdu(uint channelId, byte[] data, int channelChunkLength)
        {
            DataFirstDvcPdu first = new DataFirstDvcPdu(channelId, (uint)data.Length, null);
            DataDvcPdu      other = new DataDvcPdu(channelId, null);

            int firstNonDataSize = first.GetNonDataSize();
            int otherNonDataSize = other.GetNonDataSize();

            if (data.Length <= channelChunkLength - otherNonDataSize)
            {
                other.Data = data;
                return(new DataDvcBasePdu[] { other });
            }

            // TODO: need to test for the following code
            byte[]                buf  = new byte[channelChunkLength - firstNonDataSize];
            MemoryStream          ms   = new MemoryStream(data);
            List <DataDvcBasePdu> pdus = new List <DataDvcBasePdu>();

            if (channelChunkLength - firstNonDataSize != ms.Read(buf, 0, channelChunkLength - firstNonDataSize))
            {
                DynamicVCException.Throw("Cannot create correct data PDUs.");
            }
            first.Data = buf;
            pdus.Add(first);

            buf = new byte[channelChunkLength - otherNonDataSize];
            // TODO: Check this logic
            int readLen = 0;

            readLen = ms.Read(buf, 0, channelChunkLength - otherNonDataSize);
            while (readLen == channelChunkLength - otherNonDataSize)
            {
                pdus.Add(new DataDvcPdu(channelId, buf));
                buf     = new byte[channelChunkLength - otherNonDataSize];
                readLen = ms.Read(buf, 0, channelChunkLength - otherNonDataSize);
            }

            if (readLen > 0)
            {
                byte[] newBuf = new byte[readLen];
                Array.Copy(buf, newBuf, readLen);
                pdus.Add(new DataDvcPdu(channelId, newBuf));
            }

            return(pdus.ToArray());
        }
        private void SetRawData(bool marshaling, PduMarshaler marshaler)
        {
            byte[] data = marshaler.RawData;

            if ((null == data) || (data.Length <= 0))
            {
                if (marshaling)
                {
                    DynamicVCException.Throw("The PDU object was not marshaled successfully.");
                }
                else
                {
                    DynamicVCException.Throw("The PDU object was not unmarshaled successfully.");
                }
            }
            RawData = data;
        }
 protected void UpdateLengthOfLength(uint length)
 {
     // TODO: check this logic
     if (length <= Byte.MaxValue)
     {
         HeaderBits.Sp = (int)Len_Values.OneByte;
     }
     else if (length <= UInt16.MaxValue)
     {
         HeaderBits.Sp = (int)Len_Values.TwoBytes;
     }
     else if (length <= UInt32.MaxValue)
     {
         HeaderBits.Sp = (int)Len_Values.FourBytes;
     }
     else
     {
         DynamicVCException.Throw("The field Length is too long.");
     }
 }
 protected void UpdateCbChannelId()
 {
     // TODO: check this logic
     if (ChannelId <= Byte.MaxValue)
     {
         HeaderBits.CbChannelId = cbChId_Values.OneByte;
     }
     else if (ChannelId <= UInt16.MaxValue)
     {
         HeaderBits.CbChannelId = cbChId_Values.TwoBytes;
     }
     else if (ChannelId <= UInt32.MaxValue)
     {
         HeaderBits.CbChannelId = cbChId_Values.FourBytes;
     }
     else
     {
         DynamicVCException.Throw("ChannelId is valid.");
     }
 }
 protected void WriteChannelId(PduMarshaler marshaler)
 {
     //TODO: Refine this method
     switch (HeaderBits.CbChannelId)
     {
         case cbChId_Values.OneByte:
             marshaler.WriteByte(Convert.ToByte(ChannelId));
             break;
         case cbChId_Values.TwoBytes:
             marshaler.WriteUInt16(Convert.ToUInt16(ChannelId));
             break;
         case cbChId_Values.FourBytes:
             marshaler.WriteUInt32(Convert.ToUInt32(ChannelId));
             break;
         case cbChId_Values.Invalid:
         default:
             DynamicVCException.Throw("chChId is invalid.");
             break;
     }
 }
 protected void WriteLength(PduMarshaler marshaler, uint Length)
 {
     //TODO: Refine this method
     switch ((Len_Values)HeaderBits.Sp)
     {
         case Len_Values.OneByte:
             marshaler.WriteByte(Convert.ToByte(Length));
             break;
         case Len_Values.TwoBytes:
             marshaler.WriteUInt16(Convert.ToUInt16(Length));
             break;
         case Len_Values.FourBytes:
             marshaler.WriteUInt32(Convert.ToUInt32(Length));
             break;
         case Len_Values.Invalid:
         default:
             DynamicVCException.Throw("Len is invalid.");
             break;
     }
 }
        public static void Throw(string message, Exception inner)
        {
            DynamicVCException e = new DynamicVCException(message, inner);

            HandleException(e);
        }
        public static void Throw(string message, Exception inner)
        {
            DynamicVCException e = new DynamicVCException(message, inner);

            HandleException(e);
        }
        public static void Throw(Exception inner)
        {
            DynamicVCException e = new DynamicVCException("Check inner exception for more details.", inner);

            HandleException(e);
        }
        public static void Throw(Exception inner)
        {
            DynamicVCException e = new DynamicVCException("Check inner exception for more details.", inner);

            HandleException(e);
        }
 private static void HandleException(DynamicVCException e)
 {
     if (IsMainThread() && (!cleanupThread))
     {
         // Not work for protocol based adapter Need more investigation
         // throw e;
     }
     else if (unprocessedException == null)
     {
         unprocessedException = e;
     }
 }