/// <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="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns> /// Async property /// </returns> public IAsyncResult BeginSend(byte[] buffer, int offset, int size, 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 mAsyncActiveUdpClientSendCount); UdpClientSendDelegate d = new UdpClientSendDelegate(this.Send); if (this.mAsyncActiveUdpClientSendEvent == null) { lock (LOCK_SEND) { if (this.mAsyncActiveUdpClientSendEvent == null) { this.mAsyncActiveUdpClientSendEvent = new AutoResetEvent(true); } } } this.mAsyncActiveUdpClientSendEvent.WaitOne(); this.mUdpClientSendDelegate = d; return(d.BeginInvoke(buffer, offset, size, callback, state)); }
public int EndSend(IAsyncResult asyncResult) { if (asyncResult == null) { ThrowHelper.ThrowArgumentNullException("asyncResult"); } if (this.mUdpClientSendDelegate == null) { ThrowHelper.ThrowArgumentException("Wrong async result or EndSend called multiple times.", "asyncResult"); } try { return(this.mUdpClientSendDelegate.EndInvoke(asyncResult)); } finally { this.mUdpClientSendDelegate = null; this.mAsyncActiveUdpClientSendEvent.Set(); CloseAsyncActiveUdpClientSendEvent(Interlocked.Decrement(ref mAsyncActiveUdpClientSendCount)); } }