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); } }
protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state) { UdpChannelHelpers.ValidateTimeout(timeout); if (!this.IsDisposed) { this.EnsureChannelAvailable(); } return(this.channelQueue.BeginDequeue(timeout, callback, state)); }
//Synchronously returns a channel that is attached to this listener. protected override IInputChannel OnAcceptChannel(TimeSpan timeout) { UdpChannelHelpers.ValidateTimeout(timeout); if (!this.IsDisposed) { this.EnsureChannelAvailable(); } IInputChannel channel; if (this.channelQueue.Dequeue(timeout, out channel)) { return(channel); } else { throw CreateAcceptTimeoutException(timeout); } }
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); }
protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.channelQueue.BeginWaitForItem(timeout, callback, state)); }
protected override bool OnWaitForChannel(TimeSpan timeout) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.channelQueue.WaitForItem(timeout)); }
internal UdpOutputChannel(UdpChannelFactory factory, EndpointAddress remoteAddress, Uri via, MessageEncoder encoder) : base(factory) { // validate addressing arguments if (!string.Equals(via.Scheme, UdpConstants.Scheme, StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "The scheme {0} specified in address is not supported.", via.Scheme), "via"); } if (via.IsDefaultPort) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "The address {0} must specify a remote port.", via), "via"); } // convert the Uri host into an IP Address IPAddress remoteIP = null; switch (via.HostNameType) { default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Cannot determine the remote host address from {0}.", this.via.ToString()), "via"); case UriHostNameType.IPv4: case UriHostNameType.IPv6: remoteIP = IPAddress.Parse(via.Host); break; case UriHostNameType.Basic: case UriHostNameType.Dns: { IPHostEntry hostEntry = Dns.GetHostEntry(via.Host); if (hostEntry.AddressList.Length > 0) { remoteIP = hostEntry.AddressList[0]; } else { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Failed to resolve remote host: {0}.", via.Host), "via"); } break; } } if (factory.Multicast && !UdpChannelHelpers.IsInMulticastRange(remoteIP)) { throw new ArgumentOutOfRangeException("remoteEndPoint", "Via must be in the valid multicast range."); } this.parent = factory; this.remoteAddress = remoteAddress; this.via = via; this.encoder = encoder; this.remoteEndPoint = new IPEndPoint(remoteIP, via.Port); this.socket = new Socket(this.remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); if (parent.Multicast) { this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); #if LATER // Support outgoing interface if (this.remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) { this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder((int)interfaceIndex)); } else // we are IPv6 { this.sendSocketV6.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, (int)interfaceIndex); } #endif } }
public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.messageQueue.BeginWaitForItem(timeout, callback, state)); }
public bool WaitForMessage(TimeSpan timeout) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.messageQueue.WaitForItem(timeout)); }
public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.messageQueue.BeginDequeue(timeout, callback, state)); }
public bool TryReceive(TimeSpan timeout, out Message message) { UdpChannelHelpers.ValidateTimeout(timeout); return(this.messageQueue.Dequeue(timeout, out message)); }