Exemple #1
0
        /// <summary>
        /// Begins the send.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="size">The size.</param>
        /// <param name="remoteEp">The remote ep.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        /// Async property
        /// </returns>
        public IAsyncResult BeginSend(byte[] buffer, int offset, int size, AddressEndPoint remoteEp, AsyncCallback callback, object state)
        {
            DoDisposeCheck();
            if (buffer == null)
            {
                ThrowHelper.ThrowArgumentNullException("buffer");
            }
            if ((offset < 0) || (offset > buffer.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException("offset");
            }
            if ((size < 0) || (size > (buffer.Length - offset)))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException("size");
            }

            Interlocked.Increment(ref mAsyncActiveUdpClientSendToCount);
            UdpClientSendToDelegate d = new UdpClientSendToDelegate(this.Send);

            if (this.mAsyncActiveUdpClientSendToEvent == null)
            {
                lock (LOCK_SENDTO)
                {
                    if (this.mAsyncActiveUdpClientSendToEvent == null)
                    {
                        this.mAsyncActiveUdpClientSendToEvent = new AutoResetEvent(true);
                    }
                }
            }
            this.mAsyncActiveUdpClientSendToEvent.WaitOne();
            this.mUdpClientSendToDelegate = d;
            return(d.BeginInvoke(buffer, offset, size, remoteEp, callback, state));
        }
Exemple #2
0
        public int EndSend(IAsyncResult asyncResult, AddressEndPoint remoteEp)
        {
            if (asyncResult == null)
            {
                ThrowHelper.ThrowArgumentNullException("asyncResult");
            }
            if (this.mUdpClientSendToDelegate == null)
            {
                ThrowHelper.ThrowArgumentException("Wrong async result or EndSend called multiple times.", "asyncResult");
            }

            try
            {
                return(this.mUdpClientSendToDelegate.EndInvoke(asyncResult));
            }
            finally
            {
                this.mUdpClientSendToDelegate = null;
                this.mAsyncActiveUdpClientSendToEvent.Set();
                CloseAsyncActiveUdpClientSendToEvent(Interlocked.Decrement(ref mAsyncActiveUdpClientSendToCount));
            }
        }