/// <summary>
        /// Decodes data when a payload is maybe expected.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="callback"></param>
        public static void DecodePayload(byte[] data, IDecodePayloadCallback callback)
        {
            var bufferTail = ByteBuffer.Wrap(data);

            var buffers           = new List <object>();
            int bufferTail_offset = 0;

            while (bufferTail.Capacity - bufferTail_offset > 0)
            {
                var strLen        = new StringBuilder();
                var isString      = (bufferTail.Get(0 + bufferTail_offset) & 0xFF) == 0;
                var numberTooLong = false;
                for (int i = 1;; i++)
                {
                    int b = bufferTail.Get(i + bufferTail_offset) & 0xFF;
                    if (b == 255)
                    {
                        break;
                    }
                    // support only integer
                    if (strLen.Length > MAX_INT_CHAR_LENGTH)
                    {
                        numberTooLong = true;
                        break;
                    }
                    strLen.Append(b);
                }
                if (numberTooLong)
                {
                    callback.Call(_err, 0, 1);
                    return;
                }
                bufferTail_offset += strLen.Length + 1;

                int msgLength = int.Parse(strLen.ToString());

                bufferTail.Position(1 + bufferTail_offset);
                bufferTail.Limit(msgLength + 1 + bufferTail_offset);
                var msg = new byte[bufferTail.Remaining()];
                bufferTail.Get(msg, 0, msg.Length);

                if (isString)
                {
                    buffers.Add(ByteArrayToString(msg));
                }
                else
                {
                    buffers.Add(msg);
                }
                bufferTail.Clear();
                bufferTail.Position(msgLength + 1 + bufferTail_offset);
                bufferTail_offset += msgLength + 1;
            }

            var total = buffers.Count;

            for (int i = 0; i < total; i++)
            {
                var buffer = buffers[i];
                if (buffer is string)
                {
                    callback.Call(DecodePacket((string)buffer, true), i, total);
                }
                else if (buffer is byte[])
                {
                    callback.Call(DecodePacket((byte[])buffer), i, total);
                }
            }
        }
        /// <summary>
        /// Decodes data when a payload is maybe expected.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="callback"></param>
        public static void DecodePayload(string data, IDecodePayloadCallback callback)
        {
            if (String.IsNullOrEmpty(data))
            {
                callback.Call(_err, 0, 1);
                return;
            }

            var length = new StringBuilder();

            for (int i = 0, l = data.Length; i < l; i++)
            {
                var chr = Convert.ToChar(data.Substring(i, 1));

                if (chr != ':')
                {
                    length.Append(chr);
                }
                else
                {
                    int n;
                    if (!int.TryParse(length.ToString(), out n))
                    {
                        callback.Call(_err, 0, 1);
                        return;
                    }

                    string msg;
                    try
                    {
                        msg = data.Substring(i + 1, n);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        callback.Call(_err, 0, 1);
                        return;
                    }

                    if (msg.Length != 0)
                    {
                        Packet packet = DecodePacket(msg, true);
                        if (_err.Type == packet.Type && _err.Data == packet.Data)
                        {
                            callback.Call(_err, 0, 1);
                            return;
                        }

                        bool ret = callback.Call(packet, i + n, l);
                        if (!ret)
                        {
                            return;
                        }
                    }

                    i     += n;
                    length = new StringBuilder();
                }
            }
            if (length.Length > 0)
            {
                callback.Call(_err, 0, 1);
            }
        }
Exemplo n.º 3
0
        public static void DecodePayload(byte[] data, IDecodePayloadCallback callback)
        {
            ByteBuffer    byteBuffer = ByteBuffer.Wrap(data);
            List <object> objectList = new List <object>();
            int           num1;
            int           num2;

            for (int index = 0; byteBuffer.Capacity - index > 0; index = num1 + (num2 + 1))
            {
                StringBuilder stringBuilder = new StringBuilder();
                bool          flag1         = ((int)byteBuffer.Get((long)index) & (int)byte.MaxValue) == 0;
                bool          flag2         = false;
                int           num3          = 1;
                while (true)
                {
                    int num4 = (int)byteBuffer.Get((long)(num3 + index)) & (int)byte.MaxValue;
                    if (num4 != (int)byte.MaxValue)
                    {
                        if (stringBuilder.Length <= Packet.MAX_INT_CHAR_LENGTH)
                        {
                            stringBuilder.Append(num4);
                            ++num3;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        goto label_7;
                    }
                }

                flag2 = true;
label_7:
                if (flag2)
                {
                    callback.Call(Packet._err, 0, 1);
                    return;
                }

                num1 = index + (stringBuilder.Length + 1);
                num2 = int.Parse(stringBuilder.ToString());
                byteBuffer.Position((long)(1 + num1));
                byteBuffer.Limit((long)(num2 + 1 + num1));
                byte[] numArray = new byte[byteBuffer.Remaining()];
                byteBuffer.Get(numArray, 0, numArray.Length);
                if (flag1)
                {
                    objectList.Add((object)Packet.ByteArrayToString(numArray));
                }
                else
                {
                    objectList.Add((object)numArray);
                }
                byteBuffer.Clear();
                byteBuffer.Position((long)(num2 + 1 + num1));
            }

            int count = objectList.Count;

            for (int index = 0; index < count; ++index)
            {
                object obj = objectList[index];
                if (obj is string)
                {
                    callback.Call(Packet.DecodePacket((string)obj, true), index, count);
                }
                else if (obj is byte[])
                {
                    callback.Call(Packet.DecodePacket((byte[])obj), index, count);
                }
            }
        }
Exemplo n.º 4
0
        public static void DecodePayload(string data, IDecodePayloadCallback callback)
        {
            if (string.IsNullOrEmpty(data))
            {
                callback.Call(Packet._err, 0, 1);
            }
            else
            {
                StringBuilder stringBuilder = new StringBuilder();
                int           startIndex    = 0;
                for (int length = data.Length; startIndex < length; ++startIndex)
                {
                    char ch = Convert.ToChar(data.Substring(startIndex, 1));
                    if (ch != ':')
                    {
                        stringBuilder.Append(ch);
                    }
                    else
                    {
                        int result;
                        if (!int.TryParse(stringBuilder.ToString(), out result))
                        {
                            callback.Call(Packet._err, 0, 1);
                            return;
                        }

                        string data1;
                        try {
                            data1 = data.Substring(startIndex + 1, result);
                        } catch (ArgumentOutOfRangeException ex) {
                            callback.Call(Packet._err, 0, 1);
                            return;
                        }

                        if ((uint)data1.Length > 0U)
                        {
                            Packet packet = Packet.DecodePacket(data1, true);
                            if (Packet._err.Type == packet.Type && Packet._err.Data == packet.Data)
                            {
                                callback.Call(Packet._err, 0, 1);
                                return;
                            }

                            if (!callback.Call(packet, startIndex + result, length))
                            {
                                return;
                            }
                        }

                        startIndex   += result;
                        stringBuilder = new StringBuilder();
                    }
                }

                if (stringBuilder.Length <= 0)
                {
                    return;
                }
                callback.Call(Packet._err, 0, 1);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Decodes data when a payload is maybe expected.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="callback"></param>
        public static void DecodePayload(string data, IDecodePayloadCallback callback)
        {
            if (String.IsNullOrEmpty(data))
            {
                callback.Call(_err, 0, 1);
                return;
            }

            var length = new StringBuilder();
            for (int i = 0, l = data.Length; i < l; i++)
            {
                var chr = Convert.ToChar(data.Substring(i, 1));

                if (chr != ':')
                {
                    length.Append(chr);
                }
                else
                {
                    int n;
                    if (!int.TryParse(length.ToString(), out n))
                    {
                        callback.Call(_err, 0, 1);
                        return;
                    }

                    string msg;
                    try
                    {
                        msg = data.Substring(i + 1, n);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        callback.Call(_err, 0, 1);
                        return;
                    }

                    if (msg.Length != 0)
                    {
                        Packet packet = DecodePacket(msg, true);
                        if (_err.Type == packet.Type && _err.Data == packet.Data)
                        {
                            callback.Call(_err, 0, 1);
                            return;
                        }

                        bool ret = callback.Call(packet, i + n, l);
                        if (!ret)
                        {
                            return;
                        }

                    }

                    i += n;
                    length = new StringBuilder();
                }
            }
            if (length.Length > 0)
            {
                callback.Call(_err, 0, 1);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Decodes data when a payload is maybe expected.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="callback"></param>
        public static void DecodePayload(byte[] data, IDecodePayloadCallback callback)
        {
            var bufferTail = ByteBuffer.Wrap(data);

            var buffers = new List<object>();
            int bufferTail_offset = 0;
            while (bufferTail.Capacity - bufferTail_offset > 0)
            {
                var strLen = new StringBuilder();
                var isString = (bufferTail.Get(0 + bufferTail_offset) & 0xFF) == 0;
                var numberTooLong = false;
                for (int i = 1; ; i++)
                {
                    int b = bufferTail.Get(i + bufferTail_offset) & 0xFF;
                    if (b == 255)
                    {
                        break;
                    }
                    // support only integer
                    if (strLen.Length > MAX_INT_CHAR_LENGTH)
                    {
                        numberTooLong = true;
                        break;
                    }
                    strLen.Append(b);
                }
                if (numberTooLong)
                {
                    callback.Call(_err, 0, 1);
                    return;
                }
                bufferTail_offset += strLen.Length + 1;

                int msgLength = int.Parse(strLen.ToString());

                bufferTail.Position(1 + bufferTail_offset);
                bufferTail.Limit(msgLength + 1 + bufferTail_offset);
                var msg = new byte[bufferTail.Remaining()];
                bufferTail.Get(msg, 0, msg.Length);

                if (isString)
                {
                    buffers.Add(ByteArrayToString(msg));
                }
                else
                {
                    buffers.Add(msg);
                }
                bufferTail.Clear();
                bufferTail.Position(msgLength + 1 + bufferTail_offset);
                bufferTail_offset += msgLength + 1;
            }

            var total = buffers.Count;
            for (int i = 0; i < total; i++)
            {
                var buffer = buffers[i];
                if (buffer is string)
                {
                    callback.Call(DecodePacket((string)buffer, true), i, total);
                }
                else if (buffer is byte[])
                {
                    callback.Call(DecodePacket((byte[])buffer), i, total);
                }
            }
        }