public SendAsyncResult(UdpOutputChannel channel, Message message, AsyncCallback callback, object state) : base(callback, state) { this.channel = channel; this.messageBuffer = channel.EncodeMessage(message); try { IAsyncResult result = null; try { result = channel.socket.BeginSendTo(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count, SocketFlags.None, channel.remoteEndPoint, new AsyncCallback(OnSend), this); } catch (SocketException socketException) { throw UdpChannelHelpers.ConvertTransferException(socketException); } if (!result.CompletedSynchronously) { return; } CompleteSend(result, true); } catch { CleanupBuffer(); throw; } }
public void Send(Message message) { base.ThrowIfDisposedOrNotOpen(); ArraySegment <byte> messageBuffer = EncodeMessage(message); try { int bytesSent = this.socket.SendTo(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count, SocketFlags.None, this.remoteEndPoint); if (bytesSent != messageBuffer.Count) { throw new CommunicationException(string.Format(CultureInfo.CurrentCulture, "A Udp error occurred sending a message to {0}.", this.remoteEndPoint)); } } catch (SocketException socketException) { throw UdpChannelHelpers.ConvertTransferException(socketException); } finally { // we need to make sure buffers are always returned to the BufferManager parent.BufferManager.ReturnBuffer(messageBuffer.Array); } }
void CompleteSend(IAsyncResult result, bool synchronous) { try { int bytesSent = channel.socket.EndSendTo(result); if (bytesSent != messageBuffer.Count) { throw new CommunicationException(string.Format(CultureInfo.CurrentCulture, "A Udp error occurred sending a message to {0}.", channel.remoteEndPoint)); } } catch (SocketException socketException) { throw UdpChannelHelpers.ConvertTransferException(socketException); } finally { CleanupBuffer(); } base.Complete(synchronous); }