Example #1
0
        /// <summary>
        /// 转发协议
        /// </summary>
        /// <param name="args">协议包</param>
        protected void Shoot(ReceivedEventArgs args)
        {
            Shoot_Request request = new Shoot_Request();

            ByteFormatter.Deserialize(request, args.Buffer, ProtocolHead.HeadSize);
            IPEndPoint   remoteEndPoint = new IPEndPoint(request.RemoteIp, request.RemotePort);
            SocketResult result         = SendAsync(request.Tag,
                                                    remoteEndPoint,
                                                    request.Buffer,
                                                    p => p.ProtocolId == request.ProtocolId,
                                                    p =>
            {
                Shoot_Response shoot = new Shoot_Response
                {
                    Result = Convert.ToByte(SocketResult.Success),
                    Buffer = p.Buffer
                };
                List <byte> responseBuffer = Protocol.Response(args.TimeStamp, shoot);
                args.Channel.Send(args.RemoteEndPoint, responseBuffer);
            });

            if (result != SocketResult.Success)
            {
                Shoot_Response response = new Shoot_Response
                {
                    Result = Convert.ToByte(result),
                    Buffer = new List <byte>()
                };
                args.Channel.Send(remoteEndPoint, Protocol.Response(args.TimeStamp, response));
            }
        }
Example #2
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
                        });
                    }
                }
            }
        }