internal IAsyncResult BeginSendCommand(int timeout, string command, AsyncCallback cb, object state) { CheckDisposed(); if (null == command) { throw new ArgumentNullException("command", "'command' argument cannot be null"); } command = AppendCRLF(command); SetProgress(true); _response = null; try { _socket.SendTimeout = timeout; byte[] cmdBytes = _encoding.GetBytes(command); CCSendStateObject stateObj = new CCSendStateObject(timeout, cmdBytes, cb, state); _socket.BeginSend(cmdBytes, 0, cmdBytes.Length, new AsyncCallback(this.OnEndSend), stateObj); return(stateObj); } catch (SocketException e) { SetProgress(false); CheckDisposed(); CheckTimeoutException(e); throw; } catch { SetProgress(false); CheckDisposed(); throw; } }
void OnEndSendEx(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, read response { OnCommandSent(stateObj.Command); try { _reader.BeginReadResponse(stateObj.Timeout, new AsyncCallback(this.OnResponse), stateObj); } catch (Exception e) { _response = _reader.Response; throw e; } } } 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; } } }