Esempio n. 1
0
        public SocketPack Unpack(Socket socket, IPEndPoint remoteEndPoint, List <byte> buffer, int offset)
        {
            int head = buffer.FindIndex(offset, b => b == ProtocolHead.Tag);

            //未找到协议
            if (head == -1)
            {
                return(new SocketPack
                {
                    Result = AnalysisResult.Empty,
                    Offset = offset,
                    Size = buffer.Count - offset
                });
            }
            else
            {
                int lessSize = buffer.Count - head;
                //长度小于协议头
                if (lessSize < ProtocolHead.HeadSize)
                {
                    return(new SocketPack
                    {
                        Result = AnalysisResult.Half,
                        Offset = head,
                        Size = lessSize
                    });
                }
                else
                {
                    ProtocolHead protocolHead = new ProtocolHead();
                    ByteFormatter.Deserialize(protocolHead, buffer, head + 1);
                    if (lessSize < protocolHead.ContentSize + ProtocolHead.HeadSize)
                    {
                        return(new SocketPack
                        {
                            Result = AnalysisResult.Half,
                            Offset = head,
                            Size = lessSize
                        });
                    }
                    else
                    {
                        return(new SocketPack
                        {
                            Result = AnalysisResult.Full,
                            Offset = head,
                            Size = protocolHead.ContentSize + ProtocolHead.HeadSize,
                            ProtocolId = protocolHead.Id,
                            TimeStamp = protocolHead.TimeStamp
                        });
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 封包响应协议
        /// </summary>
        /// <param name="timeStamp">发送时间戳</param>
        /// <param name="protocol">响应协议</param>
        /// <returns>响应字节流</returns>
        public static List <byte> Response(long timeStamp, Protocol protocol)
        {
            List <byte> content = ByteFormatter.Serialize(protocol);

            ProtocolHead head = new ProtocolHead()
            {
                Id          = protocol.Id,
                ContentSize = Convert.ToUInt16(content.Count),
                TimeStamp   = timeStamp
            };
            List <byte> buffer = ByteFormatter.Serialize(head);

            buffer.AddRange(content);
            return(buffer);
        }
Esempio n. 3
0
        /// <summary>
        /// 封包请求协议
        /// </summary>
        /// <param name="protocol">协议</param>
        /// <returns>第一个字段表示请求字节流,第二个字段表示时间戳</returns>
        public static Tuple <List <byte>, long> Request(Protocol protocol)
        {
            List <byte> content = ByteFormatter.Serialize(protocol);

            ProtocolHead head = new ProtocolHead()
            {
                Id          = protocol.Id,
                ContentSize = Convert.ToUInt16(content.Count),
                TimeStamp   = TimeStampConvert.ToUtcTimeStamp()
            };
            List <byte> buffer = ByteFormatter.Serialize(head);

            buffer.AddRange(content);
            return(new Tuple <List <byte>, long>(buffer, head.TimeStamp));
        }