示例#1
0
        /// <summary>
        /// 服务器端收包处理
        /// </summary>
        /// <param name="data"></param>
        /// <param name="OnHeart"></param>
        /// <param name="OnUnPackage"></param>
        /// <param name="onFile"></param>
        public void Pack(byte[] data, Action <DateTime> onHeart, Action <ISocketProtocal> onUnPackage, Action <byte[]> onFile)
        {
            lock (_locker)
            {
                _buffer.AddRange(data);

                while (_buffer.Count >= P_Head)
                {
                    var buffer = _buffer.ToArray();

                    var bodyLen = GetLength(buffer);

                    var type = GetType(buffer);

                    if (buffer.Length >= P_Head + bodyLen)
                    {
                        var sm = new QueueSocketMsg()
                        {
                            BodyLength = bodyLen,
                            Type       = (byte)type,
                            Content    = GetContent(buffer, P_Head, bodyLen)
                        };
                        _buffer.RemoveRange(0, P_Head + bodyLen);
                        onUnPackage?.Invoke(sm);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// socket 传输字节编码
        /// 格式为:1+4+4+x+4+x+4
        /// </summary>
        /// <param name="queueSocketMsg"></param>
        /// <returns></returns>
        public static byte[] Encode(QueueSocketMsg queueSocketMsg)
        {
            List <byte> list = new List <byte>();

            var total = 4 + 4 + 4;

            var nlen = 0;

            var tlen = 0;

            byte[] n  = null;
            byte[] tp = null;
            byte[] d  = null;

            if (!string.IsNullOrEmpty(queueSocketMsg.Name))
            {
                n      = Encoding.UTF8.GetBytes(queueSocketMsg.Name);
                nlen   = n.Length;
                total += nlen;
            }
            if (!string.IsNullOrEmpty(queueSocketMsg.Topic))
            {
                tp     = Encoding.UTF8.GetBytes(queueSocketMsg.Topic);
                tlen   = tp.Length;
                total += tlen;
            }
            if (!string.IsNullOrEmpty(queueSocketMsg.Data))
            {
                d      = Encoding.UTF8.GetBytes(queueSocketMsg.Data);
                total += d.Length;
            }

            list.Add(queueSocketMsg.Type);
            list.AddRange(BitConverter.GetBytes(total));
            list.AddRange(BitConverter.GetBytes(nlen));
            if (nlen > 0)
            {
                list.AddRange(n);
            }
            list.AddRange(BitConverter.GetBytes(tlen));
            if (tlen > 0)
            {
                list.AddRange(tp);
            }
            if (d != null)
            {
                list.AddRange(d);
            }
            var arr = list.ToArray();

            list.Clear();
            return(arr);
        }
示例#3
0
        public static QueueSocketMsg Parse(byte[] data, QueueSocketMsgType type)
        {
            var msg = new QueueSocketMsg();

            if (data != null)
            {
                msg.BodyLength = data.Length;
            }
            else
            {
                msg.BodyLength = 0;
            }

            msg.Type = (byte)type;

            if (msg.BodyLength > 0)
            {
                msg.Content = data;
            }

            return(msg);
        }
示例#4
0
        /// <summary>
        /// socket 传输字节解码
        /// </summary>
        /// <param name="data"></param>
        /// <param name="onDecode"></param>
        public static bool Decode(byte[] data, Action <QueueSocketMsg[], int> onDecode)
        {
            int offset = 0;

            try
            {
                if (data != null && data.Length > offset + MIN)
                {
                    var list = new List <QueueSocketMsg>();

                    while (data.Length > offset + MIN)
                    {
                        var total = BitConverter.ToInt32(data, offset + 1);

                        if (data.Length >= offset + total + 1)
                        {
                            offset += 5;

                            var qm = new QueueSocketMsg((QueueSocketMsgType)data[0]);
                            qm.Total = total;

                            qm.NLen = BitConverter.ToInt32(data, offset);
                            offset += 4;


                            if (qm.NLen > 0)
                            {
                                var narr = new byte[qm.NLen];
                                Buffer.BlockCopy(data, offset, narr, 0, narr.Length);
                                qm.Name = Encoding.UTF8.GetString(narr);
                            }
                            offset += qm.NLen;

                            qm.TLen = BitConverter.ToInt32(data, offset);

                            offset += 4;

                            if (qm.TLen > 0)
                            {
                                var tarr = new byte[qm.TLen];
                                Buffer.BlockCopy(data, offset, tarr, 0, tarr.Length);
                                qm.Topic = Encoding.UTF8.GetString(tarr);
                            }
                            offset += qm.TLen;

                            var dlen = qm.Total - 4 - 4 - qm.NLen - 4 - qm.TLen;

                            if (dlen > 0)
                            {
                                var darr = new byte[dlen];
                                Buffer.BlockCopy(data, offset, darr, 0, dlen);
                                qm.Data = Encoding.UTF8.GetString(darr);
                                offset += dlen;
                            }
                            list.Add(qm);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (list.Count > 0)
                    {
                        onDecode?.Invoke(list.ToArray(), offset);
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteLine($"QCoder.Decode error:{ex.Message} stack:{ex.StackTrace} data:{data.Length} offset:{offset}");
            }
            onDecode?.Invoke(null, 0);
            return(false);
        }