Exemple #1
0
        void OnEndSend(IAsyncResult ar)
        {
            CCSendStateObject stateObj = (CCSendStateObject)ar.AsyncState;

            try
            {
                stateObj.UpdateContext();
                stateObj.Sent += _socket.EndSend(ar);

                int cmdLen = stateObj.Command.Length;
                if (stateObj.Sent < cmdLen)                //all data was sent?
                {
                    _socket.BeginSend(stateObj.Command,
                                      stateObj.Sent,
                                      cmdLen - stateObj.Sent,
                                      new AsyncCallback(this.OnEndSend),
                                      stateObj);
                }
                else                 //all data was sent, set completed
                {
                    OnCommandSent(((CCSendStateObject)ar.AsyncState).Command);
                    stateObj.SetCompleted();
                }
            }
            catch (SocketException e)
            {
                if (_disposed)
                {
                    stateObj.Exception = GetDisposedException();
                }
                else if (e.ErrorCode == SockErrors.WSAETIMEDOUT)
                {
                    stateObj.Exception = GetTimeoutException(e);
                }
                else
                {
                    stateObj.Exception = e;
                }

                stateObj.SetCompleted();
            }
            catch (Exception e)
            {
                if (_disposed)
                {
                    stateObj.Exception = GetDisposedException();
                }
                else
                {
                    stateObj.Exception = e;
                }
                stateObj.SetCompleted();
            }
            catch
            {
                if (_disposed)
                {
                    stateObj.Exception = GetDisposedException();
                    stateObj.SetCompleted();
                }
                else
                {
                    NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace);
                    throw;
                }
            }
        }