void SendAsyncCore(HttpRequestMessage request, object userState) { if (userState != null) { CancelManager.EnableCancel(request); lock (pendingAsync) { HttpStageProcessingAsyncResult pend; if (pendingAsync.TryGetValue(userState, out pend)) { if (pend == null) { throw new ArgumentException("userState is not unique", "userState"); } else { throw new ArgumentException("userState is already being used for " + pend.HttpAsyncState.request, "userState"); } } pendingAsync.Add(userState, null); } } var operation = AsyncOperationManager.CreateOperation(userState); var state = new SendAsyncState(this, operation); var result = this.BeginSendCore(request, SendCompletedCallback, state); if (userState != null && !result.IsCompleted) { lock (pendingAsync) { Debug.Assert(pendingAsync[userState] == null); pendingAsync[userState] = result; } } }
private void SendAsyncLoop(SendAsyncState sendState) { sendAsyncLoopAgain: if (sendState.countToWrite > 0) { sendEventArgs.SetBuffer(sendState.byteBuffer.ReadOffset, sendState.countToWrite); if (UnderlyingSocket.SendAsync(sendEventArgs) == false) { // completed synchrounsly CompleteSend(sendEventArgs, false); goto sendAsyncLoopAgain; } } }
/// <summary> /// Ends an asynchronous operation to send the a <see cref="AK.Net.Dns.DnsQuery"/> /// to an end point and return the <see cref="AK.Net.Dns.DnsReply"/>. /// </summary> /// <param name="iar">The asynchronous operation result.</param> /// <returns> /// The <see cref="AK.Net.Dns.DnsReply"/> to the <see cref="AK.Net.Dns.DnsQuery"/>. /// </returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="iar"/> is <see langword="null"/>. /// </exception> /// <exception cref="System.ArgumentException"> /// Thrown when the specified async result does not belong to this operation. /// </exception> /// <exception cref="System.InvalidOperationException"> /// Thrown when <see cref="AK.Net.Dns.IDnsTransport.EndSend(System.IAsyncResult)"/> /// has already been called on the specified async result. /// </exception> /// <exception cref="AK.Net.Dns.DnsTransportException"> /// Thrown when a transport error occurred during the asynchronous operation. /// </exception> public virtual DnsReply EndSend(IAsyncResult iar) { Guard.NotNull(iar, "iar"); SendAsyncState state = iar as SendAsyncState; if (state == null) { throw Guard.InvalidAsyncResult("iar"); } state.OnEndCalled(); return(state.Result); }
/// <summary> /// Begins an asynchronous operation to send the specified /// <see cref="AK.Net.Dns.DnsQuery"/> to the specified end point /// and return the <see cref="AK.Net.Dns.DnsReply"/>. /// </summary> /// <param name="query">The query to send.</param> /// <param name="endpoint">The transport end point.</param> /// <param name="callback">The method to invoke once the asynchronous operation /// has completed.</param> /// <param name="state">The user-defined state object to associate with the /// asynchronous operation.</param> /// <returns>The asynchronous operation result.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="query"/>, <paramref name="endpoint"/> or /// <paramref name="callback"/> is <see langword="null"/>. /// </exception> public virtual IAsyncResult BeginSend(DnsQuery query, IPEndPoint endpoint, AsyncCallback callback, object state) { Guard.NotNull(endpoint, "endpoint"); Guard.NotNull(callback, "callback"); SendAsyncState asyncState = new SendAsyncState(callback, state); asyncState.QueueOperation(delegate { try { asyncState.Result = Send(query, endpoint); } catch (DnsTransportException exc) { asyncState.Exception = exc; } finally { asyncState.OnComplete(); } }); return(asyncState); }