public TcpClient EndAcceptTcpClient(IAsyncResult asyncResult) { if (!_override) { return(_listener.EndAcceptTcpClient(asyncResult)); } else { if (asyncResult == null) { throw new Exception("Unable to handle null async result."); } WrappedTcpListenerAsyncResult result = (WrappedTcpListenerAsyncResult)asyncResult; TcpClient ret = null; if (((int)(_thread.ThreadState & ThreadState.Unstarted) != (int)ThreadState.Unstarted) && ((int)(_thread.ThreadState & ThreadState.Stopped) != (int)ThreadState.Stopped)) { try { _thread.Abort(); } catch (Exception e) { } _listener.Stop(); _thread.Join(); if (_backlog.HasValue) { _listener.Start(_backlog.Value); } else { _listener.Start(); } } else { ret = result.Client; } result = null; return(ret); } }
private void _BackgroundAccept(object obj) { WrappedTcpListenerAsyncResult result = (WrappedTcpListenerAsyncResult)obj; Socket socket = null; TcpClient client = null; try { if (result.AcceptSocket) { socket = _listener.AcceptSocket(); } else { client = _listener.AcceptTcpClient(); } } catch (ThreadAbortException tae) { result = null; return; } catch (Exception e) { socket = null; client = null; } if (!_closed) { if (result.AcceptSocket) { result.Complete(socket); } else { result.Complete(client); } _waitHandle.Set(); ThreadPool.QueueUserWorkItem(new WaitCallback(_ProcCallBack), result); } }
private void _ProcCallBack(object obj) { if ((int)(_thread.ThreadState & ThreadState.Stopped) != (int)ThreadState.Stopped) { try { _thread.Join(); } catch (Exception e) { } } WrappedTcpListenerAsyncResult result = (WrappedTcpListenerAsyncResult)obj; try { if (result.CallBack != null) { result.CallBack(result); } } catch (Exception e) { } }
public IAsyncResult BeginAcceptTcpClient(AsyncCallback callback, object state) { if (!_override) { return(_listener.BeginAcceptSocket(callback, state)); } else { if (_thread != null) { if (((int)(_thread.ThreadState & ThreadState.Unstarted) != (int)ThreadState.Unstarted) && ((int)(_thread.ThreadState & ThreadState.Stopped) != (int)ThreadState.Stopped)) { throw new Exception("Unable to begin Accepting Socket, already asynchronously waiting"); } } WrappedTcpListenerAsyncResult result = new WrappedTcpListenerAsyncResult(state, _waitHandle, callback, false); _thread = new Thread(new ParameterizedThreadStart(_BackgroundAccept)); _thread.IsBackground = true; _thread.Start(result); return(result); } }