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); // Adding the framing header messageBuffer = FramingCodec.Encode(this.remoteAddress.Uri, messageBuffer, parent.BufferManager); 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 { // Must 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)); }
public void Send(Message message) { base.ThrowIfDisposedOrNotOpen(); ArraySegment <byte> messageBuffer = EncodeMessage(message); byte[] httpHeaders = CreateHttpHeaders(messageBuffer.Count, remoteAddress.Uri.AbsolutePath, remoteAddress.Uri.Host); string headers = Encoding.UTF8.GetString(httpHeaders); byte[] bytes = new byte[httpHeaders.Length + messageBuffer.Count]; Array.Copy(httpHeaders, bytes, httpHeaders.Length); Array.Copy(messageBuffer.Array, messageBuffer.Offset, bytes, httpHeaders.Length, messageBuffer.Count); string packet = Encoding.UTF8.GetString(bytes); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\n--> Send:"); Console.ResetColor(); Console.WriteLine(packet); try { //int bytesSent = this.socket.SendTo(bytes, 0, bytes.Length, // SocketFlags.None, this.remoteEndPoint); int bytesSent = socket.Send(bytes, 0, bytes.Length, SocketFlags.None); if (bytesSent != bytes.Length) { throw new CommunicationException(string.Format(CultureInfo.CurrentCulture, "An error occurred sending a message to {0}.", this.remoteEndPoint)); } } catch (SocketException socketException) { Console.WriteLine(socketException.Message); throw UdpChannelHelpers.ConvertTransferException(socketException); } finally { // we need to make sure buffers are always returned to the BufferManager parent.BufferManager.ReturnBuffer(messageBuffer.Array); } }
//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); } }
public SendAsyncResult(UdpOutputChannel channel, Message message, AsyncCallback callback, object state) : base(callback, state) { this.channel = channel; //obtain the transaction propagation token from the TransactionFlowProperty on the message byte[] txPropToken = TransactionFlowProperty.Get(message); this.messageBuffer = channel.EncodeMessage(message); txmsgBuffer = TransactionMessageBuffer.WriteTransactionMessageBuffer(txPropToken, messageBuffer); if ((long)txmsgBuffer.Length > channel.Factory.MaxPacketSize) { throw new CommunicationException("The output packet size is greater than the maximum size supported."); } try { IAsyncResult result = null; try { result = channel.socket.BeginSendTo(txmsgBuffer, 0, txmsgBuffer.Length, 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) { if (message == null) { throw new ArgumentNullException("message"); } base.ThrowIfDisposedOrNotOpen(); //obtain the transaction propagation token from the TransactionFlowProperty on the message byte[] txPropToken = TransactionFlowProperty.Get(message); ArraySegment <byte> messageBuffer = EncodeMessage(message); byte[] txmsgBuffer = TransactionMessageBuffer.WriteTransactionMessageBuffer(txPropToken, messageBuffer); if ((long)txmsgBuffer.Length > this.Factory.MaxPacketSize) { throw new CommunicationException("The output packet size is greater than the maximum size supported."); } try { int bytesSent = this.socket.SendTo(txmsgBuffer, 0, txmsgBuffer.Length, SocketFlags.None, this.remoteEndPoint); if (bytesSent != txmsgBuffer.Length) { 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); }
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)); }
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) { 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 // IPv6 { this.sendSocketV6.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, (int)interfaceIndex); } #endif } }