Beispiel #1
0
        private void ReceiveBodyData(IAsyncResult ar)
        {
            try
            {
                NetPackage mPackage = (NetPackage)ar.AsyncState;
                int        len      = clientSocket.EndReceive(ar);
                if (len > 0)
                {
                    mPackage.BodyIndex += len;
                    if (mPackage.BodyIndex < mPackage.BodyLength)
                    {
                        clientSocket.BeginReceive(mPackage.BodyBuffer,
                                                  mPackage.BodyIndex,
                                                  mPackage.BodyLength - mPackage.BodyIndex,
                                                  SocketFlags.None,
                                                  ReceiveBodyData,
                                                  mPackage);
                    }
                    else
                    {
                        //处理数据
                        T data = NetTool.Deserialize <T>(mPackage.BodyBuffer);
                        OnReceiveData(data);

                        mPackage.RestPackage();
                        clientSocket.BeginReceive(
                            mPackage.HeadBuffer,
                            0,
                            mPackage.HeadLength,
                            SocketFlags.None,
                            ReceiveHeadData,
                            mPackage);
                    }
                }
                else
                {
                    Clear();
                }
            }
            catch (Exception e)
            {
                DetailLog.Error("RcvBodyError:" + e.Message);
            }
        }
Beispiel #2
0
 public void SendData(T data)
 {
     byte[] sendData = NetTool.PackNetData(data);
     //byte[] sendData = NetTool.AddHeadInfo(NetTool.Serialize<T>(data));
     SendData(sendData);
 }