public void ReconnectOnRouterBug() { { using (var dealer = new DealerSocket()) { dealer.Options.Identity = Encoding.ASCII.GetBytes("dealer"); dealer.Bind("tcp://localhost:6667"); using (var router = new RouterSocket()) { router.Options.RouterMandatory = true; router.Connect("tcp://localhost:6667"); Thread.Sleep(100); router.SendMoreFrame("dealer").SendFrame("Hello"); var message = dealer.ReceiveFrameString(); Assert.That(message == "Hello"); router.Disconnect("tcp://localhost:6667"); Thread.Sleep(1000); router.Connect("tcp://localhost:6667"); Thread.Sleep(100); router.SendMoreFrame("dealer").SendFrame("Hello"); message = dealer.ReceiveFrameString(); Assert.That(message == "Hello"); } } } }
/// <summary> /// Handling replies to a data push, a historical data request, or an available data request /// </summary> private void HistoricalDataSocketReceiveReady(object sender, NetMQSocketEventArgs e) { lock (_historicalDataSocketLock) { if (_historicalDataSocket == null) { return; } // 1st message part: what kind of stuff we're receiving var type = _historicalDataSocket.ReceiveFrameString(); if (type.Equals(MessageType.HistPushReply, StringComparison.InvariantCultureIgnoreCase)) { HandleDataPushReply(); } else if (type.Equals(MessageType.HistReply, StringComparison.InvariantCultureIgnoreCase)) { HandleHistoricalDataRequestReply(); } else if (type.Equals(MessageType.AvailableDataReply, StringComparison.InvariantCultureIgnoreCase)) { HandleAvailabledataReply(); } else if (type.Equals(MessageType.Error, StringComparison.InvariantCultureIgnoreCase)) { HandleErrorReply(); } } }
static void Main(string[] args) { Console.WriteLine("====== SINK ======"); using (var sink = new DealerSocket()) { sink.Bind("tcp://127.0.0.1:7777"); var startOfBatchTrigger = sink.ReceiveFrameString(); Console.WriteLine("Seen start of batch"); var watch = new Stopwatch(); watch.Start(); for (int taskNumber = 0; taskNumber < 100; taskNumber++) { var workerDoneTrigger = sink.ReceiveFrameString(); if (taskNumber % 10 == 0) { Console.Write(":"); } else { Console.Write("."); } } watch.Stop(); Console.WriteLine(); Console.WriteLine($"Total elapsed time {watch.ElapsedMilliseconds} msec"); Console.ReadLine(); } }
/// <summary> /// Process replies on the request socket. /// Heartbeats, errors, and subscribing to real time data streams. /// </summary> private void RealTimeRequestSocketReceiveReady(object sender, NetMQSocketEventArgs e) { lock (realTimeRequestSocketLock) { string reply = realTimeRequestSocket?.ReceiveFrameString(); if (reply == null) { return; } DataRequestMessageType mesageTye = (DataRequestMessageType)BitConverter.ToInt16(realTimeRequestSocket.ReceiveFrameBytes(), 0); RealTimeDataRequest request; switch (mesageTye) { case DataRequestMessageType.Pong: lastHeartBeat = DateTime.Now; break; case DataRequestMessageType.Error: string error = realTimeRequestSocket.ReceiveFrameString(); byte[] buffer1 = realTimeRequestSocket.ReceiveFrameBytes(); using (MemoryStream ms = new MemoryStream(buffer1)) { request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(ms); } // Error event RaiseEvent(Error, this, new ErrorArgs(-1, "Real time data request error: " + error, request.RequestID)); break; case DataRequestMessageType.Success: byte[] buffer = realTimeRequestSocket.ReceiveFrameBytes(); using (MemoryStream ms = new MemoryStream(buffer)) { request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(ms); } lock (realTimeDataStreamsLock) { RealTimeDataStreams.Add(request); } realTimeDataSocket.Subscribe(BitConverter.GetBytes(request.Instrument.ID)); break; case DataRequestMessageType.RTDCanceled: string symbol = realTimeRequestSocket.ReceiveFrameString(); break; default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// Process replies on the request socket. /// Heartbeats, errors, and subscribing to real time data streams. /// </summary> private void RealTimeRequestSocketReceiveReady(object sender, NetMQSocketEventArgs e) { lock (_realTimeRequestSocketLock) { using (var ms = new MemoryStream()) { var reply = _realTimeRequestSocket?.ReceiveFrameString(); if (reply == null) { return; } reply = _realTimeRequestSocket.ReceiveFrameString(); if (reply.Equals(MessageType.Pong, StringComparison.InvariantCultureIgnoreCase)) { // Reply to heartbeat message _lastHeartBeat = DateTime.Now; } else if (reply.Equals(MessageType.Error, StringComparison.InvariantCultureIgnoreCase)) { // Something went wrong // First the message var error = _realTimeRequestSocket.ReceiveFrameString(); // Then the request var buffer = _realTimeRequestSocket.ReceiveFrameBytes(); var request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(buffer, ms); // Error event RaiseEvent(Error, this, new ErrorArgs(-1, "Real time data request error: " + error, request.RequestID)); } else if (reply.Equals(MessageType.Success, StringComparison.InvariantCultureIgnoreCase)) { // Successful request to start a new real time data stream // Receive the request var buffer = _realTimeRequestSocket.ReceiveFrameBytes(); var request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(buffer, ms); // Add it to the active streams lock (_realTimeDataStreamsLock) { RealTimeDataStreams.Add(request); } // TODO: Solve issue with null request // Request worked, so we subscribe to the stream _realTimeDataSocket.Subscribe(BitConverter.GetBytes(request.Instrument.ID.Value)); } else if (reply.Equals(MessageType.RTDCanceled, StringComparison.InvariantCultureIgnoreCase)) { // Successful cancelation of a real time data stream // Also receive the symbol var symbol = _realTimeRequestSocket.ReceiveFrameString(); // Nothing to do? } } } }
public IMessageBroker Startup() { if (IsStarted) { return(this); } var cancellationTokenSource = _cancellationTokenSource = new CancellationTokenSource(); _proxyTask = Task.Run(() => { var address = _configuration.GetAddress(); var pubAddress = $"@{_configuration.Schema}://{address}:{_configuration.XPubPort}"; var subAddress = $"@{_configuration.Schema}://{address}:{_configuration.XSubPort}"; #if DEBUG System.Diagnostics.Debug.WriteLine($"[{nameof( MessageBroker )}]XPub listening on: {pubAddress}"); System.Diagnostics.Debug.WriteLine($"[{nameof( MessageBroker )}]XSub listening on: {subAddress}"); #endif using (var xpubSocket = new XPublisherSocket(pubAddress)) using (var xsubSocket = new XSubscriberSocket(subAddress)) using (var controlIn = new DealerSocket("@inproc://control-in")) using (var controlOut = new DealerSocket("@inproc://control-out")) using (var controlInRecv = new DealerSocket(">inproc://control-in")) using (var controlOutRecv = new DealerSocket(">inproc://control-out")) { Task.Run(() => { while (!cancellationTokenSource.IsCancellationRequested) { try { var topic = controlInRecv.ReceiveFrameString(); var timestamp_bytes = controlInRecv.ReceiveFrameBytes(); var timestamp_int64 = BitConverter.ToInt64(timestamp_bytes, 0); var timestamp = new DateTimeOffset(timestamp_int64, TimeSpan.FromMinutes(0)); var message = controlInRecv.ReceiveFrameString(); var wrapper = new MessageWrapper(topic, message, timestamp); Logger?.LogReceive(wrapper); } catch (System.ObjectDisposedException) { return; } } }, cancellationTokenSource.Token); //proxy messages between frontend / backend var proxy = _proxy = new Proxy(xsubSocket, xpubSocket, controlIn, controlOut); //WARNING:blocks indefinitely. proxy.Start(); } }, cancellationTokenSource.Token); return(this); }
public void WithLibzmqServer() { using (var ctx = new ZContext()) using (var client = new DealerSocket()) using (var server = ZSocket.Create(ctx, ZSocketType.DEALER)) { var serverCert = new ZCert(); server.CurveServer = true; server.CurveSecretKey = serverCert.SecretKey; server.Bind($"tcp://127.0.0.1:55367"); var clientKeyPair = new NetMQCertificate(); client.Options.CurveCertificate = clientKeyPair; client.Options.CurveServerKey = serverCert.PublicKey; client.Connect("tcp://127.0.0.1:55367"); server.SendBytes(Encoding.ASCII.GetBytes("Hello"), 0, 5); var hello = client.ReceiveFrameString(); Assert.Equal("Hello", hello); client.SendFrame("Hello"); var frame = server.ReceiveFrame(); Assert.Equal("Hello", frame.ReadString()); } }
public void CurveTest() { var serverPair = new NetMQCertificate(); using var server = new DealerSocket(); server.Options.CurveServer = true; server.Options.CurveCertificate = serverPair; server.Bind($"tcp://127.0.0.1:55367"); var clientPair = new NetMQCertificate(); using var client = new DealerSocket(); client.Options.CurveServerKey = serverPair.PublicKey; client.Options.CurveCertificate = clientPair; client.Connect("tcp://127.0.0.1:55367"); for (int i = 0; i < 100; i++) { client.SendFrame("Hello"); var hello = server.ReceiveFrameString(); Assert.Equal("Hello", hello); server.SendFrame("World"); var world = client.ReceiveFrameString(); Assert.Equal("World", world); } }
public void ConnectionStringMultipleAddresses() { using (var server1 = new DealerSocket("@tcp://127.0.0.1:51504")) using (var server2 = new DealerSocket("@tcp://127.0.0.1:51505,@tcp://127.0.0.1:51506")) using (var client = new DealerSocket("tcp://127.0.0.1:51504,tcp://127.0.0.1:51505,tcp://127.0.0.1:51506")) { // send three hello messages client.SendFrame("Hello"); client.SendFrame("Hello"); client.SendFrame("Hello"); Assert.AreEqual("Hello", server1.ReceiveFrameString()); Assert.AreEqual("Hello", server2.ReceiveFrameString()); Assert.AreEqual("Hello", server2.ReceiveFrameString()); } }
private static void WorkerTaskB(object portNumber) { using (var worker = new DealerSocket()) { worker.Options.Identity = Encoding.Unicode.GetBytes("B"); worker.Connect(string.Format("tcp://localhost:{0}", portNumber)); int total = 0; bool end = false; while (!end) { string request = worker.ReceiveFrameString(); if (request == "END") { end = true; } else { total++; } } Console.WriteLine("B Received: {0}", total); } }
private static void WorkerTaskA(object portNumber) { using (var worker = new DealerSocket()) { worker.Options.Identity = Encoding.Unicode.GetBytes("A"); worker.Connect($"tcp://localhost:{portNumber}"); int total = 0; bool end = false; while (!end) { string request = worker.ReceiveFrameString(); if (request == "END") { end = true; } else { total++; } } Console.WriteLine("A Received: {0}", total); } }
public void ConnectionStringMultipleAddresses() { using (DealerSocket server1 = new DealerSocket("@tcp://127.0.0.1:5555")) using (DealerSocket server2 = new DealerSocket("@tcp://127.0.0.1:5556,@tcp://127.0.0.1:5557")) using (DealerSocket client = new DealerSocket("tcp://127.0.0.1:5555,tcp://127.0.0.1:5556,tcp://127.0.0.1:5557")) { // send three helloes client.SendFrame("Hello"); client.SendFrame("Hello"); client.SendFrame("Hello"); Assert.AreEqual("Hello", server1.ReceiveFrameString()); Assert.AreEqual("Hello", server2.ReceiveFrameString()); Assert.AreEqual("Hello", server2.ReceiveFrameString()); } }
public string SendMessage(string message) { clientSocket.SendFrame(message, false); string res = clientSocket.ReceiveFrameString(); Console.WriteLine(res); return(res); }
public void BindRandom() { using (var randomDealer = new DealerSocket()) using (var connectingDealer = new DealerSocket()) { int port = randomDealer.BindRandomPort("tcp://*"); connectingDealer.Connect("tcp://127.0.0.1:" + port); randomDealer.SendFrame("test"); Assert.AreEqual("test", connectingDealer.ReceiveFrameString()); } }
public void Ipv6ToIpv4() { using (var localDealer = new DealerSocket()) using (NetMQSocket connectingDealer = new DealerSocket()) { localDealer.Options.IPv4Only = false; var port = localDealer.BindRandomPort("tcp://*"); connectingDealer.Connect($"tcp://{IPAddress.Loopback}:{port}"); connectingDealer.SendFrame("test"); Assert.AreEqual("test", localDealer.ReceiveFrameString()); } }
public void Ipv6ToIpv6() { using (var localDealer = new DealerSocket()) using (var connectingDealer = new DealerSocket()) { localDealer.Options.IPv4Only = false; var port = localDealer.BindRandomPort("tcp://*"); connectingDealer.Options.IPv4Only = false; connectingDealer.Connect(string.Format("tcp://{0}:{1}", IPAddress.IPv6Loopback, port)); connectingDealer.SendFrame("test"); Assert.AreEqual("test", localDealer.ReceiveFrameString()); } }
public void BindToLocal() { var validAliasesForLocalHost = new[] { "127.0.0.1", "localhost", Dns.GetHostName() }; foreach (var alias in validAliasesForLocalHost) { using (var localDealer = new DealerSocket()) using (var connectingDealer = new DealerSocket()) { var port = localDealer.BindRandomPort("tcp://*"); connectingDealer.Connect($"tcp://{alias}:{port}"); localDealer.SendFrame("test"); Assert.AreEqual("test", connectingDealer.ReceiveFrameString()); } } }
public void BindToLocal() { var validAliasesForLocalHost = new[] { "127.0.0.1", "localhost", Dns.GetHostName() }; foreach (var alias in validAliasesForLocalHost) { using (var localDealer = new DealerSocket()) using (var connectingDealer = new DealerSocket()) { var port = localDealer.BindRandomPort("tcp://*"); connectingDealer.Connect(string.Format("tcp://{0}:{1}", alias, port)); localDealer.SendFrame("test"); Assert.AreEqual("test", connectingDealer.ReceiveFrameString()); Console.WriteLine(alias + " connected "); } } }
private static void awaitClients(DealerSocket input, int clients, Dictionary <string, DealerSocket> result, NetMQContext context, IDistributedApp app) { while (result.Count < clients) { var clientCmd = input.ReceiveFrameString(); var json = JObject.Parse(clientCmd) as dynamic; string clientUrl = json.Url; //connect var socket = context.CreateDealerSocket(); socket.Connect(clientUrl); result[clientUrl] = socket; //register instances var instances = (json.Instances as JArray) .Select(val => Guid.Parse(val.ToString())); foreach (var id in instances) { app.RemoteInstance(id, msg => writeMessage(socket, msg)); } } }
public void RemoveSocket() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var router3 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var dealer3 = new DealerSocket()) using (var poller = new NetMQPoller { router1, router2, router3 }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); int port3 = router3.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); dealer3.Connect("tcp://127.0.0.1:" + port3); bool first = true; router1.ReceiveReady += (s, e) => { if (!first) { Assert.Fail("This should not happen because we cancelled the socket"); } first = false; // identity e.Socket.SkipFrame(); bool more; Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more)); Assert.False(more); // cancelling the socket poller.Remove(e.Socket); // remove self }; router2.ReceiveReady += (s, e) => { // identity byte[] identity = e.Socket.ReceiveFrameBytes(); // message e.Socket.SkipFrame(); e.Socket.SendMoreFrame(identity); e.Socket.SendFrame("2"); }; router3.ReceiveReady += (s, e) => { // identity byte[] identity = e.Socket.ReceiveFrameBytes(); // message e.Socket.SkipFrame(); e.Socket.SendMoreFrame(identity).SendFrame("3"); }; Task pollerTask = Task.Factory.StartNew(poller.Run); // Send three messages. Only the first will be processed, as then handler removes // the socket from the poller. dealer1.SendFrame("Hello"); dealer1.SendFrame("Hello2"); dealer1.SendFrame("Hello3"); // making sure the socket defined before the one cancelled still works dealer2.SendFrame("1"); Assert.AreEqual("2", dealer2.ReceiveFrameString()); // making sure the socket defined after the one cancelled still works dealer3.SendFrame("1"); Assert.AreEqual("3", dealer3.ReceiveFrameString()); poller.Stop(); Assert.IsTrue(pollerTask.IsCompleted); } }