public void init(string address, int port) { this.address = address; this.port = port; //create context context = NetMQContext.Create(); //create client publisher = context.CreatePublisherSocket(); //connect publisher publisher.Bind("tcp://" + address + ":" + port); }
public void BindBothSockets() { using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { pub.Bind("pgm://224.0.0.1:5555"); sub.Bind("pgm://224.0.0.1:5555"); sub.Subscribe(""); pub.SendFrame("Hi"); bool more; Assert.AreEqual("Hi", sub.ReceiveFrameString(out more)); Assert.IsFalse(more); } }
public void PubSub_Should_Not_Crash_If_No_Thread_Sleep() { NUnitUtils.PrintTestName(); var swAll = Stopwatch.StartNew(); using (var pub = new PublisherSocket()) { using (var sub = new SubscriberSocket()) { var freePort = NUnitUtils.TcpPortFree(); pub.Bind("tcp://127.0.0.1:" + freePort); sub.Connect("tcp://127.0.0.1:" + freePort); sub.Subscribe("*"); var sw = Stopwatch.StartNew(); { for (var i = 0; i < 50; i++) { pub.SendFrame("*"); // Ping. Console.Write("*"); string topic; var gotTopic = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out topic); string ping; var gotPing = sub.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out ping); if (gotTopic) { Console.Write("\n"); break; } } } Console.WriteLine("Connected in {0} ms.", sw.ElapsedMilliseconds); } } NUnitUtils.PrintElapsedTime(swAll.Elapsed); }
public void ReceiveMessageWithTimeout() { { var pubSync = new AutoResetEvent(false); var payload = new byte[300]; const int waitTime = 500; var t1 = new Task(() => { using (var pubSocket = new PublisherSocket()) { pubSocket.Bind("tcp://127.0.0.1:12345"); pubSync.WaitOne(); Thread.Sleep(waitTime); pubSocket.SendFrame(payload); pubSync.WaitOne(); } }, TaskCreationOptions.LongRunning); var t2 = new Task(() => { using (var subSocket = new SubscriberSocket()) { subSocket.Connect("tcp://127.0.0.1:12345"); subSocket.Subscribe(""); Thread.Sleep(100); pubSync.Set(); NetMQMessage msg = null; Assert.IsFalse(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(100), ref msg)); Assert.IsTrue(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(waitTime), ref msg)); Assert.NotNull(msg); Assert.AreEqual(1, msg.FrameCount); Assert.AreEqual(300, msg.First.MessageSize); pubSync.Set(); } }, TaskCreationOptions.LongRunning); t1.Start(); t2.Start(); Task.WaitAll(t1, t2); } }
private PublisherSocket GetNewPublisherSocket(string addressZeroMq) { PublisherSocket publisherSocket; { _loggerDelegate?.Invoke(string.Format("Publisher socket binding to: {0}\n", addressZeroMq)); publisherSocket = new PublisherSocket(); // Corner case: wait until publisher socket is ready (see code below that waits for // "_publisherReadySignal"). NetMQMonitor monitor; { // Must ensure that we have a unique monitor name for every instance of this class. string endPoint = string.Format("inproc://#SubjectNetMQ#Publisher#{0}#{1}", addressZeroMq, Guid.NewGuid().ToString()); monitor = new NetMQMonitor(publisherSocket, endPoint, SocketEvents.Accepted | SocketEvents.Listening ); monitor.Accepted += Publisher_Event_Accepted; monitor.Listening += Publisher_Event_Listening; monitor.StartAsync(); } publisherSocket.Options.SendHighWatermark = this.HighwaterMark; try { publisherSocket.Bind(addressZeroMq); } catch (NetMQException ex) { // This is usually because the address is in use. throw new Exception(string.Format("Error E56874. Cannot bind publisher to '{0}'. 95% probability that this is caused by trying to bind a publisher to a port already in use by another process. To fix, choose a unique publisher port for this process. For more on this error, see 'Readme.md' (or the GitHub homepage for NetMQ.ReactiveExtensions).", addressZeroMq), ex); } // Corner case: wait until publisher socket is ready (see code below that sets "_publisherReadySignal"). { Stopwatch sw = Stopwatch.StartNew(); _publisherReadySignal.WaitOne(TimeSpan.FromMilliseconds(3000)); _loggerDelegate?.Invoke(string.Format("Publisher: Waited {0} ms for binding.\n", sw.ElapsedMilliseconds)); } { monitor.Accepted -= Publisher_Event_Accepted; monitor.Listening -= Publisher_Event_Listening; // Current issue with NegMQ: Cannot stop or dispose monitor, or else it stops the parent socket. //monitor.Stop(); //monitor.Dispose(); } } // Otherwise, the first item we publish may get missed by the subscriber. 500 milliseconds consistently works // locally, but occasionally fails on the AppVeyor build server. 650 milliseconds is optimal. using (EventWaitHandle wait = new ManualResetEvent(false)) { // Cannot use Thread.Sleep() here, as this is incompatible with .NET Core 1.0, Windows 8.0, 8.1, and 10. wait.WaitOne(TimeSpan.FromMilliseconds(650)); } return publisherSocket; }
public void Test_Two_Subscribers() { NUnitUtils.PrintTestName(); var sw = Stopwatch.StartNew(); using (var pub = new PublisherSocket()) { using (var sub1 = new SubscriberSocket()) { using (var sub2 = new SubscriberSocket()) { var freePort = NUnitUtils.TcpPortFree(); pub.Bind("tcp://127.0.0.1:" + freePort); sub1.Connect("tcp://127.0.0.1:" + freePort); sub1.Subscribe("A"); sub2.Connect("tcp://127.0.0.1:" + freePort); sub2.Subscribe("B"); Thread.Sleep(500); var swInner = Stopwatch.StartNew(); { pub.SendFrame("A\n"); // Ping. { string topic; var pass1 = sub1.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic); if (pass1) { Console.Write(topic); } else { Assert.Fail(); } } pub.SendFrame("B\n"); // Ping. { string topic; var pass2 = sub2.TryReceiveFrameString(TimeSpan.FromMilliseconds(250), out topic); if (pass2) { Console.Write(topic); } else { Assert.Fail(); } } } Console.WriteLine("Connected in {0} ms.", swInner.ElapsedMilliseconds); } } } NUnitUtils.PrintElapsedTime(sw.Elapsed); }