Example #1
0
        /// <summary>
        /// Begins the connect.
        /// </summary>
        /// <param name="endPoint">The end point.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        /// <param name="clientStreamFactory">The client stream factory.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        /// <returns>Async property</returns>
        public IAsyncResult BeginConnect(AddressEndPoint endPoint, int bufferSize, IClientStreamFactory clientStreamFactory, AsyncCallback callback, object state)
        {
            DoDisposeCheck();
            if (endPoint == null)
            {
                ThrowHelper.ThrowArgumentNullException("endPoint");
            }
            if (bufferSize < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException("bufferSize");
            }
            if (clientStreamFactory == null)
            {
                ThrowHelper.ThrowArgumentNullException("clientStreamFactory");
            }

            Interlocked.Increment(ref mAsyncActiveConnectCount);
            NetworkManagerConnectDelegate d = new NetworkManagerConnectDelegate(this.Connect);

            if (this.mAsyncActiveConnectEvent == null)
            {
                lock (LOCK_CONNECT)
                {
                    if (this.mAsyncActiveConnectEvent == null)
                    {
                        this.mAsyncActiveConnectEvent = new AutoResetEvent(true);
                    }
                }
            }
            this.mAsyncActiveConnectEvent.WaitOne();
            this.mConnectDelegate = d;
            return(d.BeginInvoke(endPoint, bufferSize, clientStreamFactory, callback, state));
        }
Example #2
0
 /// <summary>
 /// Ends the connect.
 /// </summary>
 /// <param name="asyncResult">The async result.</param>
 /// <returns>
 /// Network Stream instance
 /// </returns>
 public NetworkStream EndConnect(IAsyncResult asyncResult)
 {
     if (asyncResult == null)
     {
         ThrowHelper.ThrowArgumentNullException("asyncResult");
     }
     if (this.mConnectDelegate == null)
     {
         ThrowHelper.ThrowArgumentException("Wrong async result or EndConnect called multiple times.", "asyncResult");
     }
     try
     {
         return(this.mConnectDelegate.EndInvoke(asyncResult));
     }
     finally
     {
         this.mConnectDelegate = null;
         this.mAsyncActiveConnectEvent.Set();
         CloseAsyncActiveConnectEvent(Interlocked.Decrement(ref mAsyncActiveConnectCount));
     }
 }