Quantcast
Channel: Active questions tagged zeromq - Stack Overflow
Viewing all articles
Browse latest Browse all 193

How to dispose of Sockets, Monitors and Pollers in NetMQ / ZeroMQ correctly?

$
0
0

I'm working with C#, Unity and NetMQ (ZeroMQ for c#). I'm creating and connecting/binding a PublisherSocket and SubscriberSocket and are monitoring it with a NetMQPoller and NetMQMonitor. I'm doing the following:

m_Publisher = new PublisherSocket();m_Subscriber = new SubscriberSocket();m_Poller = new NetMQPoller();m_SubMonitor = new NetMQMonitor(m_Subscriber, m_InprocSubMonitorAddress, SocketEvents.Connected | SocketEvents.Disconnected);// Eventsm_SubMonitor.Connected += (s, e) =>{    Connected = true;    UnityMainThreadDispatcher.Instance().Enqueue(() => OnConnected?.Invoke(this, EventArgs.Empty));};m_SubMonitor.Disconnected += (s, e) =>{    Connected = false;    UnityMainThreadDispatcher.Instance().Enqueue(() => OnDisconnected?.Invoke(this, EventArgs.Empty));};// Monitoringm_SubMonitor.AttachToPoller(m_Poller);m_Poller.RunAsync();// Subscriberm_Subscriber.Connect($"tcp://{m_IPAddress}:{m_Port}");// Publisherm_Publisher.Bind($"tcp://*:{m_Port}");

Feel free to correct me, if that is the wrong way to do it, but its working. The problem occurs when trying to connect to an address, which is not online (so Connected Event is never invoked). It should be possible to cancle the Connection attempt and try to connect again afterwards. On the second attemp I get an "Address already in use" Error, stating the m_InprocSubMonitorAddress ("inproc://sub.inproc") and pointing to the creation of the NetMQMonitor.My guess is, that I'm not disposing of the Sockets, Poller and Monitor correctly, but I cant seem to get it right. I've tried multiple ways, the current beeing:

m_SubMonitor.DetachFromPoller();m_Poller?.Stop();m_Poller?.Dispose();m_SubMonitor?.Stop();m_SubMonitor?.Dispose();m_Publisher?.Unbind($"tcp://*:{m_Port}");//m_Publisher?.Close();m_Publisher?.Dispose();m_Subscriber?.Disconnect($"tcp://{m_IPAddress}:{m_Port}");//m_Subscriber?.Close();m_Subscriber?.Dispose();

What would be the correct way? It is not possible for me to use using, because I'm working with the sockets in multiple functions and threads.Do I need to call NetMQConfig.Cleanup on each disconnect? Currently I'm calling it once, when the Application Quits. I'm also passing false into it, so it should not block. Is that correct?

I appreciate any help!


Viewing all articles
Browse latest Browse all 193

Trending Articles