public Remove ( ISocketPollable socket ) : void | ||
socket | ISocketPollable | |
Résultat | void |
private void Configure([NotNull] string interfaceName, int port) { // In case the beacon was configured twice if (m_udpSocket != null) { m_poller.Remove(m_udpSocket); #if NET35 m_udpSocket.Close(); #else m_udpSocket.Dispose(); #endif } m_udpPort = port; m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_poller.Add(m_udpSocket, OnUdpReady); // Ask operating system for broadcast permissions on socket m_udpSocket.EnableBroadcast = true; // Allow multiple owners to bind to socket; incoming // messages will replicate to each owner m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPAddress bindTo = null; IPAddress sendTo = null; if (interfaceName == "*") { bindTo = IPAddress.Any; sendTo = IPAddress.Broadcast; } else if (interfaceName == "loopback") { bindTo = IPAddress.Loopback; sendTo = IPAddress.Broadcast; } else { var interfaceCollection = new InterfaceCollection(); var interfaceAddress = !string.IsNullOrEmpty(interfaceName) ? IPAddress.Parse(interfaceName) : null; foreach (var @interface in interfaceCollection) { if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress)) { // because windows and unix differ in how they handle broadcast addressing this needs to be platform specific // on windows any interface can recieve broadcast by requesting to enable broadcast on the socket // on linux to recieve broadcast you must bind to the broadcast address specifically //bindTo = @interface.Address; sendTo = @interface.BroadcastAddress; #if NET35 || NET40 if (Environment.OSVersion.Platform == PlatformID.Unix) #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) #endif { bindTo = @interface.BroadcastAddress; } else { bindTo = @interface.Address; } sendTo = @interface.BroadcastAddress; break; } } } if (bindTo != null) { m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort); m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort)); } m_pipe.SendFrame(bindTo?.ToString() ?? ""); }
public void Remove(NetMQSocket socket) { m_poller.Remove(socket); m_sockets.Remove(socket); }
private void Configure([NotNull] string interfaceName, int port) { // In case the beacon was configured twice if (m_udpSocket != null) { m_poller.Remove(m_udpSocket); #if NET35 m_udpSocket.Close(); #else m_udpSocket.Dispose(); #endif } m_udpPort = port; m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_poller.Add(m_udpSocket, OnUdpReady); // Ask operating system for broadcast permissions on socket m_udpSocket.EnableBroadcast = true; // Allow multiple owners to bind to socket; incoming // messages will replicate to each owner m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPAddress bindTo = null; IPAddress sendTo = null; if (interfaceName == "*") { bindTo = IPAddress.Any; sendTo = IPAddress.Broadcast; } else if (interfaceName == "loopback") { bindTo = IPAddress.Loopback; sendTo = IPAddress.Broadcast; } else { var interfaceCollection = new InterfaceCollection(); var interfaceAddress = !string.IsNullOrEmpty(interfaceName) ? IPAddress.Parse(interfaceName) : null; foreach (var @interface in interfaceCollection) { if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress)) { sendTo = @interface.BroadcastAddress; bindTo = @interface.Address; break; } } } if (bindTo != null) { m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort); m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort)); } m_pipe.SendFrame(bindTo?.ToString() ?? ""); }
/// <summary> /// run the broker - if not bound to endpoint automatically binds to known endpoint /// </summary> /// <param name="token">CancellationToken to cancel the method</param> /// <exception cref="InvalidOperationException">Can't start same broker more than once!</exception> public void RunSynchronous (CancellationToken token) { if (m_isRunning) throw new InvalidOperationException ("Can't start same broker more than once!"); if (!m_isBound) Bind (); m_isRunning = true; using (var poller = new NetMQPoller ()) { Socket.ReceiveReady += ProcessReceivedMessage; // get timer for scheduling heartbeat var timer = new NetMQTimer (HeartbeatInterval); // send every 'HeartbeatInterval' a heartbeat to all not expired workers timer.Elapsed += (s, e) => SendHeartbeat (); poller.Add (Socket); poller.Add (timer); Log ("Starting to listen for incoming messages ..."); // start the poller and wait for the return, which will happen once token is // signalling Cancel(!) Task.Factory.StartNew (poller.Run, token).Wait (); Log ("... Stopped!"); // clean up poller.Remove (timer); poller.Remove (Socket); // unregister event handler Socket.ReceiveReady -= ProcessReceivedMessage; } m_isRunning = false; }
internal void Remove(NetMQSocket socket) { m_poller.Remove(socket); m_sockets.Remove(socket); }
private void Configure(string interfaceName, int port) { // In case the beacon was configured twice if (m_udpSocket != null) { m_poller.Remove(m_udpSocket); m_udpSocket.Close(); } m_udpPort = port; m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_poller.Add(m_udpSocket, OnUdpReady); // Ask operating system for broadcast permissions on socket m_udpSocket.EnableBroadcast = true; // Allow multiple owners to bind to socket; incoming // messages will replicate to each owner m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPAddress bindTo = null; IPAddress sendTo = null; if (interfaceName == "*") { bindTo = IPAddress.Any; sendTo = IPAddress.Broadcast; } else { var interfaceCollection = new InterfaceCollection(); var interfaceAddress = !string.IsNullOrEmpty(interfaceName) ? IPAddress.Parse(interfaceName) : null; foreach (var @interface in interfaceCollection) { if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress)) { sendTo = @interface.BroadcastAddress; bindTo = @interface.Address; break; } } } if (bindTo != null) { m_broadcastAddress = new IPEndPoint(sendTo, m_udpPort); m_udpSocket.Bind(new IPEndPoint(bindTo, m_udpPort)); string hostname = ""; try { if (!IPAddress.Any.Equals(bindTo) && !IPAddress.IPv6Any.Equals(bindTo)) { var host = Dns.GetHostEntry(bindTo); hostname = host != null ? host.HostName : ""; } } catch (Exception) {} m_pipe.SendFrame(hostname); } }