internal void SetCompleted() { lock (this) { this.UpdateContext(); this._isCompleted = true; if (this._wait != null) { this._wait.Set(); } } this.dumpActivityException(); try { if (this.CallBack != null) { this.CallBack(this); } } catch (System.Exception exception) { NSTrace.WriteLineError("Exception in CB: " + exception.ToString()); throw; } catch { NSTrace.WriteLineError("Non CLS exception in CB: " + Environment.StackTrace.ToString()); throw; } }
protected virtual void Dispose(bool disposing) { lock (this) { _disposed = true; if (disposing) { } try { FtpDataConnection dc = _currentDC; if (null != dc) { dc.Dispose(); } } catch (Exception e) { NSTrace.WriteLineError("DTP.Dispose() ex: " + e.ToString()); } catch { NSTrace.WriteLineError("DTP.Dispose() non cls ex: " + Environment.StackTrace); } } }
private object DoTimeoutOp(int timeout, IOp op) { this.CheckDisposed(); object obj2 = null; this.StartTimeoutTrack(timeout); try { obj2 = op.Execute(); } catch (Exception exception) { NSTrace.WriteLineError("SocketEx (ex): " + exception.ToString()); this.StopTimeoutTrack(exception); throw; } catch { NSTrace.WriteLineError("SocketEx (non clas ex): " + Environment.StackTrace); this.StopTimeoutTrack(this.NonCLSException); throw; } this.StopTimeoutTrack(null); return(obj2); }
/*internal void SetCompleted(bool completedSync) * { * SetCompleted(); * }*/ internal void SetCompleted() { lock (this) //sync with 'AsyncWaitHandle' property { UpdateContext(); _isCompleted = true; if (null != _wait) { _wait.Set(); } } dumpActivityException(); try { if (null != CallBack) { CallBack(this); } } catch (Exception e) { NSTrace.WriteLineError("Exception in CB: " + e.ToString()); throw; } /* * catch * { * NSTrace.WriteLineError("Non CLS exception in CB: " + Environment.StackTrace.ToString()); * throw; * } */ }
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 UserStreamRead_End(IAsyncResult ar) { TransStateObjectStream stateObj = (TransStateObjectStream)ar.AsyncState; try { stateObj.UpdateContext(); int num = stateObj.UserStream.EndRead(ar); if (0 == num) //for example coping zero-length file { FinishTransferingStream(DataStream); OnDataTransfered(null, 0); OnCompleted(); stateObj.SetCompleted(); } else { DataStream.BeginWrite(_workBuffer, 0, num, new AsyncCallback(this.WriteDataStream_End), stateObj); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }
private void dumpActivityException() { System.Exception exception = this.Exception; if (exception != null) { int hashCode = Thread.CurrentThread.GetHashCode(); NSTrace.WriteLineError((string.Format("{0} ---------- Start Exception Info -----------------------------\n", hashCode) + string.Format("{0} Activity: {1}\n", hashCode, this.ActivityName) + string.Format("{0} Stack: {1}\n", hashCode, Environment.StackTrace.ToString())) + string.Format("{0} Exception: {1}\n", hashCode, exception.ToString()) + string.Format("{0} ---------- End Exception Info -----------------------------", hashCode)); } }
void OnEndConnectCB(IAsyncResult ar) { Connect_SO stateObj = (Connect_SO)ar.AsyncState; try { stateObj.UpdateContext(); _socket.EndConnect(ar); _reader.BeginReadResponse(stateObj.Timeout, new AsyncCallback(this.OnResponse), stateObj); } 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; } } }
public override void Write(byte[] buffer, int offset, int size) { if ((0 == _available) || (size > _available)) { string msg = "Trying to write after the end of the stream."; NSTrace.WriteLineError(msg); throw new EndOfStreamException(msg); } _stream.Write(buffer, offset, size); _available -= size; }
private void dumpActivityException(AsyncResultBase ar) { Exception exception = ar.Exception; int hashCode = Thread.CurrentThread.GetHashCode(); NSTrace.WriteLineError(string.Format("{0} -----------------------------", hashCode)); NSTrace.WriteLineError(string.Format("{0} Activity: {1}", hashCode, ar.ActivityName)); NSTrace.WriteLineError(string.Format("{0} Exception: {1}", hashCode, exception.GetType().FullName)); NSTrace.WriteLineError(string.Format("{0} Message: {1}", hashCode, exception.ToString())); NSTrace.WriteLineError(string.Format("{0} Stack: {1}", hashCode, exception.StackTrace)); NSTrace.WriteLineError(string.Format("{0} -----------------------------", hashCode)); }
//this function used by routines - Connect, SendCommandEx, ReadResponse void OnResponse(IAsyncResult ar) { AsyncResultBase stateObj = (AsyncResultBase)ar.AsyncState; try { stateObj.UpdateContext(); _response = _reader.EndReadResponse(ar); OnResponseReceived(); } catch (SocketException e) { if (_disposed) { stateObj.Exception = GetDisposedException(); } else if (e.ErrorCode == SockErrors.WSAETIMEDOUT) { stateObj.Exception = GetTimeoutException(e); } else { stateObj.Exception = e; } } catch (Exception e) { if (_disposed) { stateObj.Exception = GetDisposedException(); } else { stateObj.Exception = e; } } catch { if (_disposed) { stateObj.Exception = GetDisposedException(); } else { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } } _response = _reader.Response; stateObj.SetCompleted(); }
internal static IPEndPoint ConstructEndPoint(IPHostEntry host, int port) { if (0 >= host.AddressList.Length) { NSTrace.WriteLineError("Provided host structure do not contains addresses."); throw new ArgumentException("Provided host structure do not contains addresses.", "host"); } int index = 0; if (1 < host.AddressList.Length) { index = _rand.Next(host.AddressList.Length - 1); } return(new IPEndPoint(host.AddressList[index], port)); }
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; } }
internal SocketEx(SocketBase baseSocket) { this._opState = OpState.Finished; this._recvTimeout = -1; this._sendTimeout = -1; this._acceptTimeout = -1; this._connectTimeout = -1; NSTrace.WriteLineVerbose("-> SocketEx(handle)"); if (baseSocket == null) { NSTrace.WriteLineError("EX: SocketEx(handle), handle == null. " + Environment.StackTrace); throw new ArgumentNullException("baseSocket"); } this._baseSocket = baseSocket; this.Init(); NSTrace.WriteLineVerbose("<- SocketEx(handle)"); }
internal IAsyncResult BeginExecute(int timeout, string command, FtpDataType dataType, long restart, DTPStream userStream, AsyncCallback cb, object state) { RunDTP_SO stateObj = new RunDTP_SO(timeout, command, dataType, restart, userStream, cb, state); SetProgress(true); _currentDC = null; try { //---------------------------------------- //Lock Control connection to prevent //run the command like abort, quit, stat //during configuring of data connection _cc.BeginLock(Timeout.Infinite, new WaitOrTimerCallback(LockFirst_End), stateObj); } catch (Exception e) { SetProgress(false); CheckDisposed(); NSTrace.WriteLineError(_errMsg + e.ToString()); throw; } catch { SetProgress(false); CheckDisposed(); NSTrace.WriteLineError(_errMsgNonCls + Environment.StackTrace.ToString()); throw; } return(stateObj); }
static public IPEndPoint ConstructEndPoint(IPHostEntry host, int port) { if (0 >= host.AddressList.Length) { NSTrace.WriteLineError("Provided host structure do not contains addresses."); throw new ArgumentException("Provided host structure do not contains addresses.", "host"); } foreach (var addr in host.AddressList) { if (addr.AddressFamily == AddressFamily.InterNetwork) { return(new IPEndPoint(addr, port)); } } return(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port)); }
IPEndPoint ConstructEndPoint(IPHostEntry host, int port) { if (0 >= host.AddressList.Length) { string msg = "Provided host structure do not contains addresses."; NSTrace.WriteLineError(msg); throw new ArgumentException(msg); } int addressNo = 0; if (1 < host.AddressList.Length) { addressNo = _rand.Next(host.AddressList.Length - 1); } return(new IPEndPoint(host.AddressList[addressNo], port)); }
void HandleCatch(Exception e, RunDTP_SO stateObj) { //---------------------------------------- //Clear context before unlocking _client.CurrentDTP = null; //---------------------------------------- //Unlock control connection, now the command //like abort, stat, quit could be issued try { UnlockCC(stateObj); } catch (Exception ex) { NSTrace.WriteLineError("UnlockCC exception: " + ex.ToString()); } catch { NSTrace.WriteLineError("UnlockCC non-cls exception: " + Environment.StackTrace); } if (_disposed) { stateObj.Exception = GetDisposedException(); } else if (null != e) { NSTrace.WriteLineError("DTP exception: " + e.ToString()); stateObj.Exception = e; } else { NSTrace.WriteLineError("DTP non cls exception: " + Environment.StackTrace); } //Do not set completed in case we //have non-cls exception if (null != stateObj.Exception) { stateObj.SetCompleted(); } }
void dumpActivityException() { Exception e = Exception; if (null == e) { return; } int tid = Thread.CurrentThread.GetHashCode(); string msg = string.Format("{0} ---------- Start Exception Info -----------------------------\n", tid); msg += string.Format("{0} Activity: {1}\n", tid, ActivityName); msg += string.Format("{0} Stack: {1}\n", tid, Environment.StackTrace.ToString()); msg += string.Format("{0} Exception: {1}\n", tid, e.ToString()); msg += string.Format("{0} ---------- End Exception Info -----------------------------", tid); NSTrace.WriteLineError(msg); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state ) { if ((0 == _available) || (size > _available)) { string msg = "Trying to write after the end of the stream."; NSTrace.WriteLineError(msg); throw new EndOfStreamException(msg); } IAsyncResult ar = _stream.BeginWrite(buffer, offset, size, callback, state); _available -= size; return(ar); }
public override int Read(byte[] buffer, int offset, int size) { if (0 == _available) { string msg = "Trying to read after the end of the stream."; NSTrace.WriteLineError(msg); throw new EndOfStreamException(msg); } if (size > _available) { size = (int)_available; } int r = _stream.Read(buffer, offset, size); _available -= r; return(r); }
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 Lock_End(object state, bool timedout) { Reset_SO stateObj = (Reset_SO)state; try { stateObj.UpdateContext(); if (timedout) { string msg = string.Format("Unable to lock CC within {0} time: ", stateObj.Timeout); NSTrace.WriteLineError(msg + Environment.StackTrace); stateObj.Exception = GetTimeoutException(stateObj.Timeout); stateObj.SetCompleted(); } else { try { Cmd_RunDTP curDTP = _client.CurrentDTP; if (null != curDTP) { curDTP.Reset(); } } finally { _cc.Unlock(); } //---------------------------------------- //Wait till DTP finishes _client.BeginWaitForDTPFinished(stateObj.Timeout, new WaitOrTimerCallback(DTPFinishedWait_End), stateObj); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } }
void OnResponseReceived() { try { if (null != ResponseReceived) { ResponseReceived(this, new ResponseReceivedEventArgs(_response)); } } catch (Exception e) { NSTrace.WriteLineError("OnResponseReceived event ex: " + e.ToString()); throw; } catch { NSTrace.WriteLineError("OnResponseReceived event non cls ex."); throw; } }
void DTPFinishedWait_End(object state, bool timedout) { Reset_SO stateObj = (Reset_SO)state; try { stateObj.UpdateContext(); if (timedout) { string msg = string.Format("DTP is not finished withing {0} time: ", stateObj.Timeout); NSTrace.WriteLineError(msg + Environment.StackTrace); stateObj.Exception = GetTimeoutException(stateObj.Timeout); } } catch (Exception e) { stateObj.Exception = e; } stateObj.SetCompleted(); }
void OnCompleted() { try { if (null != Completed) { Completed(this, EventArgs.Empty); } } catch (Exception e) { NSTrace.WriteLineError("Completed event ex: " + e.ToString()); throw; } catch { NSTrace.WriteLineError("Completed event non cls ex." + Environment.StackTrace.ToString()); throw; } }
void RunDTP_End(IAsyncResult ar) { GetFile_SO stateObj = (GetFile_SO)ar.AsyncState; try { stateObj.UpdateContext(); _currentDTP.EndExecute(ar); } catch (Exception e) { stateObj.Exception = e; } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } stateObj.SetCompleted(); }
string GetTypeCommand(FtpDataType dataType) { string typeCmd = "TYPE "; if (FtpDataType.Ascii == dataType) { typeCmd += "A"; } else if (FtpDataType.Binary == dataType) { typeCmd += "I"; } else { string msg = string.Format("Data type is unsupported ({0}).", dataType.ToString()); NSTrace.WriteLineError(msg); throw new NotSupportedException(msg); } return(typeCmd); }
DTPStreamType ValidateInputStream(DTPStream stream) { //------------------------------------------ //Validate input if (null == stream) { throw new ArgumentNullException("steam", "The value cannot be null."); } DTPStreamType dtpType = stream.Type; if ((DTPStreamType.ForWriting != dtpType) && (DTPStreamType.ForReading != dtpType)) { string msg = string.Format("Unknown DTP type ({0})", stream.Type.ToString()); NSTrace.WriteLineError(msg); throw new ArgumentException(msg, "type"); } return(dtpType); }
void WriteDataStream_End(IAsyncResult ar) { TransStateObjectStream stateObj = (TransStateObjectStream)ar.AsyncState; try { stateObj.UpdateContext(); DataStream.EndWrite(ar); if (_aborted) { throw new FtpAbortedException(); } if (stateObj.UserStream.AvailableSpace > 0) { //read and send more data stateObj.UserStream.BeginRead(_workBuffer, 0, _workBuffer.Length, new AsyncCallback(this.UserStreamRead_End), stateObj); } else { FinishTransferingStream(DataStream); OnCompleted(); stateObj.SetCompleted(); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }