void DataStreamRead_End(IAsyncResult ar) { TransStateObjectStream stateObj = (TransStateObjectStream)ar.AsyncState; try { stateObj.UpdateContext(); int num = DataStream.EndRead(ar); DTPStream userStream = (DTPStream)stateObj.UserStream; if (num > 0) { long require = userStream.AvailableSpace; if (num > require) { num = (int)require; } stateObj.Transfered += num; OnDataTransfered(_workBuffer, num); //write received data to user stream userStream.BeginWrite(_workBuffer, 0, num, new AsyncCallback(this.UserStreamWrite_End), stateObj); } else { userStream.Flush(); OnCompleted(); stateObj.SetCompleted(); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }
void UserStreamWrite_End(IAsyncResult ar) { TransStateObjectStream stateObj = (TransStateObjectStream)ar.AsyncState; try { stateObj.UpdateContext(); DTPStream userStream = (DTPStream)stateObj.UserStream; userStream.EndWrite(ar); if (_aborted) { throw new FtpAbortedException(); } if (userStream.AvailableSpace > 0) { DataStream.BeginRead(_workBuffer, 0, _workBuffer.Length, new AsyncCallback(this.DataStreamRead_End), stateObj); } else { _manuallyClosed = true; userStream.Flush(); OnCompleted(); stateObj.SetCompleted(); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }
void RunDownloadingStream(DTPStream userStream) { bool needMoreData = true; while (true) { int num = DataStream.Read(_workBuffer, 0, _workBuffer.Length); if (num > 0) { long require = userStream.AvailableSpace; if (num > require) { num = (int)require; needMoreData = false; } OnDataTransfered(_workBuffer, num); userStream.Write(_workBuffer, 0, num); } if ((0 == num) || (false == needMoreData)) { //Determine wether we had read enough information. //We need to know it, because usually server //should close connection by him self, //and if we will do this instead, server will //return errorneous response later if (num > 0) { _manuallyClosed = true; } userStream.Flush(); OnCompleted(); break; } else if (_aborted) { throw new FtpAbortedException(); } } }