internal IAsyncResult BeginReadResponse(int timeout, AsyncCallback cb, object state) { SetProgress(true); ReaderStateObject stateObj = null; try { _response = new FtpResponse(_encoding); stateObj = new ReaderStateObject(cb, state); _response.NewLineEvent += new FtpResponse.NewLineEventHandler(this.OnNewLine); _linesBuilder.NewLineEvent += new BytesRoad.Net.Ftp.Advanced.LinesBuilder.NewLineEventHandler(LinesBuilder_NewLineEvent); _socket.ReceiveTimeout = timeout; BuildResponse(stateObj); } catch { SetProgress(false); _response.NewLineEvent -= new FtpResponse.NewLineEventHandler(this.OnNewLine); _linesBuilder.NewLineEvent -= new BytesRoad.Net.Ftp.Advanced.LinesBuilder.NewLineEventHandler(LinesBuilder_NewLineEvent); throw; } return(stateObj); }
private void OnRecieved(IAsyncResult ar) { ReaderStateObject stateObj = (ReaderStateObject)ar.AsyncState; try { stateObj.UpdateContext(); int readNum = _socket.EndReceive(ar); if (0 == readNum) { throw GetClosedException(); } _linesBuilder.PutData(_recvBuffer, readNum, false); BuildResponse(stateObj); } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }
void BuildResponse(ReaderStateObject stateObj) { bool needMoreData = true; stateObj.UpdateContext(); do { ParseExistentData(); if (_response.IsCompleted) { stateObj.SetCompleted(); needMoreData = false; break; } } while(_linesBuilder.Available > 0); if (needMoreData) { //start reading response _socket.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, new AsyncCallback(this.OnRecieved), stateObj); } }