示例#1
0
 public xxTCPMsg(xxTCPHeader header, xxTCPBody body)
 {
     this.header = header;
     this.body   = body;
     this.body.Encode();
     bodyBytes         = this.body.GetBytes();
     header.bodyLength = bodyBytes.Length;
     this.header.Encode();
     headerBytes = this.header.GetBytes();
     MsgBytes    = new byte[headerBytes.Length + bodyBytes.Length];
     headerBytes.CopyTo(MsgBytes, 0);
     bodyBytes.CopyTo(MsgBytes, headerBytes.Length);
 }
        public void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            ReadState readState = (ReadState)ar.AsyncState;

            try
            {
                Socket handler = readState.workSocket;

                // Read data from the client socket.
                int         bytesRead = handler.EndReceive(ar);
                xxTCPHeader header    = readState.header;

                if (bytesRead > 0)
                {
                    if (PrintReceiveHex)
                    {
                        PrintUtils.PrintHex(readState.BodyBytes);
                    }

                    xxTCPBody body = header.InstanceBody();
                    body.BodyBytes = readState.BodyBytes;
                    body.Decode();
                    body.Debug();
                    body.Info();

                    MainNotify?.Invoke(header, body);
                    xxTCPMsg sendMsg = body.GetSendMsg();
                    if (sendMsg != null)
                    {
                        Send(header.SessionId, sendMsg, sendMsg.CloseClient);
                    }

                    //将客户端状态重置为接收状态
                    ReadState readStateNew = new ReadState();
                    readStateNew.workSocket  = handler;
                    readStateNew.sessionId   = header.SessionId;
                    readStateNew.HeaderBytes = new byte[HeaderLength];
                    handler.BeginReceive(readStateNew.HeaderBytes, 0, HeaderLength, SocketFlags.None, new AsyncCallback(ReadHeadCallback), readStateNew);
                }
            }
            catch (Exception e)
            {
                ReadException(e, readState);
            }
        }
        public void ReadHeadCallback(IAsyncResult ar)
        {
            ReadState readState = (ReadState)ar.AsyncState;

            try
            {
                Socket handler = readState.workSocket;
                if (!handler.Connected)
                {
                    LOG.InfoFormat("({0}) Connection closed!", Name);
                    return;
                }
                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    xxTCPHeader header = InstanceHeader?.Invoke();
                    if (header == null)
                    {
                        LOG.ErrorFormat("({0}) Not found instance xxHeader class", Name);
                        return;
                    }
                    header.bytes        = readState.HeaderBytes;
                    header.RemoteSocket = handler;
                    header.Decode();
                    header.SessionId = readState.sessionId;
                    readState.header = header;
                    header.Debug();
                    header.Info();
                    if (PrintReceiveHex)
                    {
                        PrintUtils.PrintHex(header.bytes);
                    }



                    LOG.InfoFormat("({2}) Read header from:{0},body len:{1}", handler.RemoteEndPoint, header.bodyLength, Name);
                    readState.BodyBytes = new byte[header.bodyLength];
                    handler.BeginReceive(readState.BodyBytes, 0, header.bodyLength, SocketFlags.None, new AsyncCallback(ReadCallback), readState);
                }
            }
            catch (Exception e)
            {
                ReadException(e, readState);
            }
        }