public void Begin(DespatcheStateModel so)
 {
     if (!_isDisposed && !_isBeging)
     {
         _isBeging = true;
         try
         {
             lock (_lockObj)
             {
                 if (IsReceiveMode)
                 {
                     _lasyAsyncResult = _socket.BeginReceive(so.Buffer, so.Offset, so.Size, SocketFlags.None, ReadCallback, so);
                 }
                 else
                 {
                     _lasyAsyncResult = _socket.BeginSend(so.Buffer, so.Offset, so.Size, SocketFlags.None, ReadCallback, so);
                 }
             }
         }
         catch (Exception e)
         {
             OnDisoconnect(e);
         }
     }
 }
        public void Begin(byte[] buffer, int offset, int size)
        {
            DespatcheStateModel despatcheStateModel = new DespatcheStateModel();

            despatcheStateModel.Size        = size;
            despatcheStateModel.Buffer      = buffer;
            despatcheStateModel.Offset      = offset;
            despatcheStateModel.IsCompleted = false;
            DespatcheStateModel so = despatcheStateModel;

            Begin(so);
        }
 private void _CompletedHandle_AsyncCallback(IAsyncResult ar)
 {
     if (!_isDisposed)
     {
         lock (_lockObj)
         {
             _CompletedHandle.EndInvoke(ar);
             if (_queueCompletedHandleInvoke.Count > 0)
             {
                 DespatcheStateModel obj = _queueCompletedHandleInvoke.Dequeue();
                 _CompletedHandle.BeginInvoke(obj, _CompletedHandle_AsyncCallback, null);
             }
             else
             {
                 _CompletedHandle_Invokeing = false;
             }
         }
     }
 }
 private void ReadCallback(IAsyncResult ar)
 {
     if (!_isDisposed)
     {
         DespatcheStateModel despatcheStateModel = null;
         despatcheStateModel = (DespatcheStateModel)ar.AsyncState;
         try
         {
             int num = _socket.EndReceive(ar);
             despatcheStateModel.Offset += num;
             despatcheStateModel.Size   -= num;
             if (num == 0)
             {
                 _ = _socket.Connected;
                 //bool flag = 1 == 0;
                 if (_DisoconnectHandle != null)
                 {
                     _DisoconnectHandle.BeginInvoke(null, null);
                 }
             }
             else if (despatcheStateModel.Size == 0)
             {
                 despatcheStateModel.IsCompleted = true;
                 Completed(despatcheStateModel);
             }
             else if (IsReceiveMode)
             {
                 _lasyAsyncResult = _socket.BeginReceive(despatcheStateModel.Buffer, despatcheStateModel.Offset, despatcheStateModel.Size, SocketFlags.None, ReadCallback, despatcheStateModel);
             }
             else
             {
                 _lasyAsyncResult = _socket.BeginSend(despatcheStateModel.Buffer, despatcheStateModel.Offset, despatcheStateModel.Size, SocketFlags.None, ReadCallback, despatcheStateModel);
             }
         }
         catch (Exception e)
         {
             OnDisoconnect(e);
         }
     }
 }
 private void Completed(DespatcheStateModel model)
 {
     if (!_isDisposed)
     {
         _isBeging        = false;
         _lasyAsyncResult = null;
         lock (_lockObj)
         {
             if (_queueCompletedHandleInvoke == null)
             {
                 _queueCompletedHandleInvoke = new AQueue <DespatcheStateModel>();
             }
             _queueCompletedHandleInvoke.Enqueue(model);
             if (!_CompletedHandle_Invokeing)
             {
                 model = _queueCompletedHandleInvoke.Dequeue();
                 _CompletedHandle_Invokeing = true;
                 _CompletedHandle.BeginInvoke(model, _CompletedHandle_AsyncCallback, null);
             }
         }
     }
 }