public void Send(string sessionId, xxTCPMsg msg, bool closeClient)
 {
     try
     {
         // Convert the string data to byte data using ASCII encoding.
         //            byte[] byteData = Encoding.ASCII.GetBytes(data);
         if (string.IsNullOrEmpty(sessionId))
         {
             throw new Exception("Session id is null!");
         }
         xxClient client = xxTCPClients.GetClient(sessionId);
         if (client == null)
         {
             throw new Exception("Not found client by session:" + sessionId);
         }
         LOG.DebugFormat("({2}) Send {0} bytes to {1}", msg.MsgBytes.Length, client.socket.RemoteEndPoint, Name);
         if (PrintSendHex)
         {
             PrintUtils.PrintHex(msg.MsgBytes);
         }
         SendState state = new SendState();
         state.CloseClient  = closeClient;
         state.RemoteSocket = client.socket;
         state.Client       = client;
         state.Msg          = msg;
         // Begin sending the data to the remote device.
         client.socket.BeginSend(msg.MsgBytes, 0, msg.MsgBytes.Length, 0,
                                 new AsyncCallback(SendCallback), state);
     }
     catch (Exception e)
     {
         ReadException(e, null);
     }
 }
        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);
            }
        }