private static void ConnectCallback(IAsyncResult asyncres) { TSocket.ConnectHelper asyncState = asyncres.AsyncState as TSocket.ConnectHelper; lock (asyncState.Mutex) { asyncState.CallbackDone = true; try { if (asyncState.Client.Client != null) { asyncState.Client.Dispose(); } } catch (SocketException socketException) { } if (asyncState.DoCleanup) { asyncres.AsyncWaitHandle.Dispose(); if (asyncState.Client != null) { ((IDisposable)asyncState.Client).Dispose(); } asyncState.Client = null; } } }
public override void Open() { if (this.IsOpen) { throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected"); } if (string.IsNullOrEmpty(this.host)) { throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host"); } if (this.port <= 0) { throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port"); } if (this.client == null) { this.InitSocket(); } if (this.timeout != 0) { TSocket.ConnectHelper connectHelper = new TSocket.ConnectHelper(this.client); var t = this.client.ConnectAsync(this.host, this.port); t.Wait(); //, new AsyncCallback(TSocket.ConnectCallback), connectHelper); //if ((!asyncResult.AsyncWaitHandle.WaitOne(this.timeout) ? true : !this.client.Connected)) //{ // lock (connectHelper.Mutex) // { // if (!connectHelper.CallbackDone) // { // connectHelper.DoCleanup = true; // this.client = null; // } // else // { // asyncResult.AsyncWaitHandle.Dispose(); // this.client.Dispose(); // } // } // throw new TTransportException(TTransportException.ExceptionType.TimedOut, "Connect timed out"); //} } else { this.client.ConnectAsync(this.host, this.port).Wait(); } this.inputStream = this.client.GetStream(); this.outputStream = this.client.GetStream(); }