public void Unsubscribe() { using (NetMQContext context = NetMQContext.Create()) { using (NetMQBeacon speaker = new NetMQBeacon(context)) { speaker.Configure(9999); using (NetMQBeacon listener = new NetMQBeacon(context)) { listener.Configure(9999); listener.Subscribe("H"); // this should send one broadcast message and stop speaker.Publish("Hello"); string peerName; string message = listener.ReceiveString(out peerName); listener.Unsubscribe(); Assert.AreEqual("Hello", message); ISocketPollable socket = listener; socket.Socket.Options.ReceiveTimeout = TimeSpan.FromSeconds(2); Assert.Throws <AgainException>(() => { message = listener.ReceiveString(out peerName); }); } } } }
public void Setup() { poller = mockPoller.Object; sender = mockSender.Object; receiver = mockReceiver.Object; publisher = mockPublisher.Object; subscriber = mockSubscriber.Object; pollableSocket = mockPollableSocket.Object; mockSender .SetupGet(m => m.PollableSocket) .Returns(pollableSocket); mockReceiver .SetupGet(m => m.PollableSocket) .Returns(pollableSocket); mockPublisher .SetupGet(m => m.PollableSocket) .Returns(pollableSocket); mockSubscriber .SetupGet(m => m.PollableSocket) .Returns(pollableSocket); }
public void Remove(ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException(nameof(socket)); } if (socket.IsDisposed) { throw new ArgumentException("Must not be disposed.", nameof(socket)); } CheckDisposed(); Run(() => { // Ensure the socket wasn't disposed while this code was waiting to be run on the poller thread if (socket.IsDisposed) { throw new InvalidOperationException( $"{nameof(NetMQPoller)}.{nameof(Remove)} was called from a non-poller thread, " + "so ran asynchronously. " + $"The {socket.GetType().Name} being removed was disposed while the remove " + $"operation waited to start on the poller thread. Use {nameof(RemoveAndDispose)} " + "instead, which will enqueue the remove and dispose to happen on the poller thread."); } socket.Socket.EventsChanged -= OnSocketEventsChanged; m_sockets.Remove(socket.Socket); m_isPollSetDirty = true; }); }
public Task <bool> ContainsAsync([NotNull] ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException(nameof(socket)); } CheckDisposed(); var tcs = new TaskCompletionSource <bool>(); Run(() => tcs.SetResult(m_sockets.Contains(socket))); return(tcs.Task); }
/// <summary> /// </summary> public void Remove(ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException(nameof(socket)); } CheckDisposed(); Run(() => { socket.Socket.EventsChanged -= OnSocketEventsChanged; m_sockets.Remove(socket.Socket); m_isPollSetDirty = true; }); }
/// <summary> /// Delete the given socket from this Poller's list. /// </summary> /// <param name="socket">the ISocketPollable to remove from the list</param> /// <exception cref="ArgumentNullException">socket must not be null.</exception> /// <exception cref="ObjectDisposedException">This poller must not have already been disposed.</exception> public void RemoveSocket([NotNull] ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException("socket"); } if (m_disposed) { throw new ObjectDisposedException("Poller is disposed"); } socket.Socket.EventsChanged -= OnSocketEventsChanged; m_sockets.Remove(socket.Socket); m_isDirty = true; }
/// <summary> /// </summary> public void Add(ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException(nameof(socket)); } CheckDisposed(); Run(() => { if (m_sockets.Contains(socket.Socket)) { return; } m_sockets.Add(socket.Socket); socket.Socket.EventsChanged += OnSocketEventsChanged; m_isPollSetDirty = true; }); }
/// <inheritdoc /> public void Connect(ISocketPollable endpoint) { if (DispatchedSockets.Contains(endpoint) == false) { SyncLock.Wait(); try { if (DispatchedSockets.Contains(endpoint) == false) { DispatchedSockets.Add(endpoint); if (PollingThread?.IsValueCreated == true) { PollingThread.Value.Add(endpoint); } } } finally { SyncLock.Release(); } } }
/// <summary> /// Add the given socket to this Poller's list. /// </summary> /// <param name="socket">the ISocketPollable to add to the list</param> /// <exception cref="ArgumentNullException">socket must not be null.</exception> /// <exception cref="ArgumentException">socket must not have already been added to this poller.</exception> /// <exception cref="ObjectDisposedException">This poller must not have already been disposed.</exception> public void AddSocket([NotNull] ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException("socket"); } if (m_sockets.Contains(socket.Socket)) { throw new ArgumentException("Socket already added to poller"); } if (m_disposed) { throw new ObjectDisposedException("Poller is disposed"); } m_sockets.Add(socket.Socket); socket.Socket.EventsChanged += OnSocketEventsChanged; m_isDirty = true; }
public void Add(ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException(nameof(socket)); } if (socket.IsDisposed) { throw new ArgumentException("Must not be disposed.", nameof(socket)); } CheckDisposed(); Run(() => { // Ensure the socket wasn't disposed while this code was waiting to be run on the poller thread if (socket.IsDisposed) { throw new InvalidOperationException( $"{nameof(NetMQPoller)}.{nameof(Add)} was called from a non-poller thread, " + "so ran asynchronously. " + $"The {socket.GetType().Name} being added was disposed while the addition " + "operation waited to start on the poller thread. You must remove a socket " + "before disposing it, or dispose the poller first."); } if (m_sockets.Contains(socket.Socket)) { return; } m_sockets.Add(socket.Socket); socket.Socket.EventsChanged += OnSocketEventsChanged; m_isPollSetDirty = true; }); }
private void CreatePoller(ISocketPollable socket) { _poller = new NetMQPoller { socket }; }
/// <summary> /// Delete the given socket from this Poller's list. /// </summary> /// <param name="socket">the ISocketPollable to remove from the list</param> /// <exception cref="ArgumentNullException">socket must not be null.</exception> /// <exception cref="ObjectDisposedException">This poller must not have already been disposed.</exception> public void RemoveSocket( ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException("socket"); } if (m_disposed) { throw new ObjectDisposedException("Poller is disposed"); } socket.Socket.EventsChanged -= OnSocketEventsChanged; m_sockets.Remove(socket.Socket); m_isDirty = true; }
// NOTE this interface provides an abstraction over the legacy Poller and newer NetMQPoller classes for use in NetMQMonitor void ISocketPollableCollection.Add(ISocketPollable socket) { AddSocket(socket); }
void ISocketPollableCollection.Remove(ISocketPollable socket) { RemoveSocket(socket); }
public void Remove(ISocketPollable socket) { if (socket == null) throw new ArgumentNullException("socket"); CheckDisposed(); Run(() => { socket.Socket.EventsChanged -= OnSocketEventsChanged; m_sockets.Remove(socket.Socket); m_isPollSetDirty = true; }); }
/// <summary> /// Add the given socket to this Poller's list. /// </summary> /// <param name="socket">the ISocketPollable to add to the list</param> /// <exception cref="ArgumentNullException">socket must not be null.</exception> /// <exception cref="ArgumentException">socket must not have already been added to this poller.</exception> /// <exception cref="ObjectDisposedException">This poller must not have already been disposed.</exception> public void AddSocket( ISocketPollable socket) { if (socket == null) { throw new ArgumentNullException("socket"); } if (m_sockets.Contains(socket.Socket)) { throw new ArgumentException("Socket already added to poller"); } if (m_disposed) { throw new ObjectDisposedException("Poller is disposed"); } m_sockets.Add(socket.Socket); socket.Socket.EventsChanged += OnSocketEventsChanged; m_isDirty = true; }
void INetMQPoller.Add(ISocketPollable socket) { AddSocket(socket); }
public void Add(ISocketPollable socket) { if (socket == null) throw new ArgumentNullException("socket"); CheckDisposed(); Run(() => { if (m_sockets.Contains(socket.Socket)) return; m_sockets.Add(socket.Socket); socket.Socket.EventsChanged += OnSocketEventsChanged; m_isPollSetDirty = true; }); }
/// <summary> /// Initializes a new instance of <see cref="NetMQConnection"/> /// </summary> /// <param name="pollableSocket">Inner socket connection that is monitored by <see cref="INetMQPoller"/></param> /// <param name="messageFactory">Factory for creating <see cref="NetMQMessage"/>s</param> public NetMQConnection(ISocketPollable pollableSocket, INetMQMessageFactory messageFactory) { this.PollableSocket = pollableSocket ?? throw new ArgumentNullException(nameof(pollableSocket)); this.messageFactory = messageFactory ?? throw new ArgumentNullException(nameof(messageFactory)); }