Example #1
0
        /// <summary>
        /// Begins the accept socket.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        /// <returns>Async property</returns>
        public IAsyncResult BeginAcceptSocket(AsyncCallback callback, object state)
        {
            Interlocked.Increment(ref mAsyncActiveAcceptSocketCount);
            TcpListenerAcceptSocketDelegate d = new TcpListenerAcceptSocketDelegate(this.AcceptSocket);

            if (this.mAsyncActiveAcceptSocketEvent == null)
            {
                lock (LOCK_ACCEPTSOCKET)
                {
                    if (this.mAsyncActiveAcceptSocketEvent == null)
                    {
                        this.mAsyncActiveAcceptSocketEvent = new AutoResetEvent(true);
                    }
                }
            }
            this.mAsyncActiveAcceptSocketEvent.WaitOne();
            this.mAcceptSocketDelegate = d;
            return(d.BeginInvoke(callback, state));
        }
Example #2
0
 /// <summary>
 /// Ends the accept socket.
 /// </summary>
 /// <param name="asyncResult">The async result.</param>
 /// <returns>Socket implementation</returns>
 public ISocket EndAcceptSocket(IAsyncResult asyncResult)
 {
     if (asyncResult == null)
     {
         ThrowHelper.ThrowArgumentNullException("asyncResult");
     }
     if (this.mAcceptSocketDelegate == null)
     {
         ThrowHelper.ThrowArgumentException("Wrong async result or EndAcceptSocket called multiple times.", "asyncResult");
     }
     try
     {
         return(this.mAcceptSocketDelegate.EndInvoke(asyncResult));
     }
     finally
     {
         this.mAcceptSocketDelegate = null;
         this.mAsyncActiveAcceptSocketEvent.Set();
         CloseAsyncActiveAcceptSocketEvent(Interlocked.Decrement(ref mAsyncActiveAcceptSocketCount));
     }
 }