private static Socket CreateUnicastSocket(long adapterIndex, UdpTransportConfiguration udpTransportConfiguration) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { DontFragment = false }; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Bind(new IPEndPoint(IPAddress.Any, udpTransportConfiguration.UnicastReceiveEndpoint.Port)); return(socket); }
private UdpClient(UdpTransportConfiguration configuration, List <Socket> multicastSockets, List <Socket> unicastSockets, IJobQueue <UdpUnicastJob> unicastJobQueue, IObjectPool <byte[]> sendReceiveBufferPool, IAuditAggregator <double> inboundBytesAggregator, IAuditAggregator <double> outboundBytesAggregator, IAuditAggregator <double> inboundReceiveProcessDispatchLatencyAggregator) { this.configuration = configuration; this.multicastSockets = multicastSockets; this.unicastSockets = unicastSockets; this.unicastJobQueue = unicastJobQueue; this.sendReceiveBufferPool = sendReceiveBufferPool; this.inboundBytesAggregator = inboundBytesAggregator; this.outboundBytesAggregator = outboundBytesAggregator; this.inboundReceiveProcessDispatchLatencyAggregator = inboundReceiveProcessDispatchLatencyAggregator; this.sendArgsPool = ObjectPool.CreateStackBacked(() => new SocketAsyncEventArgs()); }
private static Socket CreateMulticastSocket(long adapterIndex, UdpTransportConfiguration udpTransportConfiguration) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { DontFragment = false, MulticastLoopback = true }; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(udpTransportConfiguration.MulticastAddress)); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0); //0: localhost, 1: lan (via switch) socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(adapterIndex)); socket.Bind(new IPEndPoint(IPAddress.Any, udpTransportConfiguration.MulticastReceiveEndpoint.Port)); return(socket); }
public UdpTransportFactory(UdpTransportConfiguration configuration = null) { this.configuration = configuration ?? UdpTransportConfiguration.Default; }
public static UdpClient Create(UdpTransportConfiguration udpTransportConfiguration, IScheduler udpUnicastScheduler, IObjectPool <byte[]> sendReceiveBufferPool, IAuditAggregator <double> inboundBytesAggregator, IAuditAggregator <double> outboundBytesAggregator, IAuditAggregator <double> inboundReceiveProcessDispatchLatencyAggregator) { var multicastSockets = new List <Socket>(); var unicastSockets = new List <Socket>(); foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { if (!networkInterface.SupportsMulticast || networkInterface.OperationalStatus != OperationalStatus.Up || networkInterface.IsReceiveOnly) { continue; } // HACK loopback disable if (networkInterface.Name.Contains("3")) { continue; } var ipv4Properties = networkInterface.GetIPProperties()?.GetIPv4Properties(); if (ipv4Properties != null) { multicastSockets.Add(CreateMulticastSocket(ipv4Properties.Index, udpTransportConfiguration)); if (!udpTransportConfiguration.MulticastReceiveEndpoint.Equals(udpTransportConfiguration.UnicastReceiveEndpoint)) { unicastSockets.Add(CreateUnicastSocket(ipv4Properties.Index, udpTransportConfiguration)); } } var ni = networkInterface; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { Console.WriteLine(ni.Name); foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { Console.WriteLine(ip.Address.ToString()); } } } } var unicastJobQueue = udpUnicastScheduler.CreateJobQueue <UdpUnicastJob>( job => { var outboundBuffer = job.OutboundBuffer; outboundBytesAggregator.Put(outboundBuffer.Length); var e = new SocketAsyncEventArgs(); e.RemoteEndPoint = job.RemoteInfo.IPEndpoint; e.SetBuffer(outboundBuffer.GetBuffer(), 0, (int)outboundBuffer.Position); e.Completed += (sender, args) => { // Duplicate code with below. args.SetBuffer(null, 0, 0); args.Dispose(); job.SendCompletionHandler(); }; const int kSendStateAsync = 1; const int kSendStateDone = 2; const int kSendStateError = 3; int sendState; try { bool completingAsynchronously = job.RemoteInfo.Socket.SendToAsync(e); sendState = completingAsynchronously ? kSendStateAsync : kSendStateDone; } catch (ObjectDisposedException) { sendState = kSendStateError; } if (sendState == kSendStateDone || sendState == kSendStateError) { // Completed synchronously so e.Completed won't be called. e.SetBuffer(null, 0, 0); e.Dispose(); job.SendCompletionHandler(); } }); return(new UdpClient(udpTransportConfiguration, multicastSockets, unicastSockets, unicastJobQueue, sendReceiveBufferPool, inboundBytesAggregator, outboundBytesAggregator, inboundReceiveProcessDispatchLatencyAggregator)); }
public static CourierBuilder UseUdpTransport(this CourierBuilder builder, UdpTransportConfiguration configuration) { return(builder.UseTransport(new UdpTransportFactory(configuration))); }
public UdpTransportFactory(UdpTransportConfiguration configuration = null) { this.configuration = configuration ?? UdpTransportConfiguration.Default; }
public static CourierBuilder UseUdpTransport(this CourierBuilder builder, UdpTransportConfiguration configuration) { return builder.UseTransport(new UdpTransportFactory(configuration)); }