public SocketListener(ZMQ.SocketType socketType, Int32 port, Func<RequestType, BinaryStream, Func<BinaryStream, Response>> handlerMapping) { _socketType = socketType; _handlerMapping = handlerMapping; _address = String.Format("tcp://*:{0}", port); }
static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: remote_thr <connect-to> <message-size> <message-count>"); return(1); } string connectTo = args[0]; int messageSize = int.Parse(args[1]); int messageCount = int.Parse(args[2]); var context = ZMQ.CtxNew(); var pushSocket = ZMQ.Socket(context, ZmqSocketType.Push); pushSocket.Connect(connectTo); for (int i = 0; i != messageCount; i++) { var message = new Msg(); message.InitPool(messageSize); pushSocket.Send(ref message, SendReceiveOptions.None); message.Close(); } pushSocket.Close(); context.Terminate(); return(0); }
public ConsumerConnector(String stateStorageDirectory, String brokerAddress, BrokerInfoResponse configuration, ZMQ.Context context) { _stateStorageDirectory = stateStorageDirectory; _brokerAddress = brokerAddress; _configuration = configuration; _context = context; }
static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: local_lat <bind-to> <message-size> <roundtrip-count>"); return(1); } string bindTo = args[0]; int messageSize = int.Parse(args[1]); int roundtripCount = int.Parse(args[2]); var context = ZMQ.CtxNew(); var repSocket = ZMQ.Socket(context, ZmqSocketType.Rep); repSocket.Bind(bindTo); for (int i = 0; i != roundtripCount; i++) { Msg message = repSocket.Recv(SendReceiveOptions.None); if (ZMQ.MsgSize(message) != messageSize) { Console.WriteLine("message of incorrect size received. Received: " + message.Size + " Expected: " + messageSize); return(-1); } repSocket.Send(message, SendReceiveOptions.None); } repSocket.Close(); context.Terminate(); return(0); }
private void Initialize(ConsumerConfiguration configuration, ZMQ.Context zeromqContext) { _configuration = configuration; _zeromqContext = zeromqContext; _sender = new RequestSender(configuration.Address, ZMQ.SocketType.REQ, _zeromqContext); }
/// <summary> /// Wait until message is ready to be received from the socket or until timeout is reached /// </summary> /// <param name="timeout"></param> /// <returns></returns> public bool Poll(TimeSpan timeout) { PollEvents events = GetPollEvents(); PollItem item = new PollItem(m_socketHandle, events); PollItem[] items = new PollItem[] { item }; ZMQ.Poll(items, (int)timeout.TotalMilliseconds); if (item.ResultEvent.HasFlag(PollEvents.PollError) && !IgnoreErrors) { Errors++; if (Errors > 1) { throw new ErrorPollingException("Error while polling", this); } } else { Errors = 0; } InvokeEvents(this, item.ResultEvent); return(items[0].ResultEvent != PollEvents.None); }
public static String GetVersionString() { Int32 major, minor, patch; ZMQ.zmq_version(out major, out minor, out patch); return(String.Format($"{major}.{minor}.{patch}")); }
/// <summary> /// Close the context /// </summary> public void Terminate() { if (Interlocked.CompareExchange(ref m_isClosed, 1, 0) == 0) { ZMQ.Term(m_ctx); } }
public RequestSender(String address, ZMQ.SocketType socketType, ZMQ.Context context) { _context = context; _address = Protocolize(address); _socket = CreateSocket(socketType); _socket.Connect(_address, CancellationToken.None); }
public void ShouldAllowInfinitePolling() { Ctx contextNew = ZMQ.CtxNew(); SocketBase receiver = ZMQ.Socket(contextNew, ZmqSocketType.Dealer); ZMQ.Bind(receiver, "inproc://test"); Task.Factory.StartNew(() => { Thread.Sleep(500); SocketBase sender = ZMQ.Socket(contextNew, ZmqSocketType.Dealer); ZMQ.Connect(sender, "inproc://test"); ZMQ.Send(sender, "ping", SendReceiveOptions.None); ZMQ.Close(sender); }); var pollItems = new PollItem[] { new PollItem(receiver, PollEvents.PollIn) }; Assert.DoesNotThrow(() => ZMQ.Poll(pollItems, -1)); var msg = ZMQ.Recv(receiver, SendReceiveOptions.DontWait); var actual = Encoding.ASCII.GetString(msg.Data, 0, msg.Size); Assert.AreEqual("ping", actual); ZMQ.Close(receiver); ZMQ.Term(contextNew); }
static void Main(string[] args) { Console.WriteLine("ZMQ version = " + ZMQ.GetVersionString()); IntPtr zmqContext = ZMQ.zmq_ctx_new(); IntPtr requestSocket = ZMQ.zmq_socket(zmqContext, ZMQ.ZMQ_REQ); using (var endpoint = ASCII("tcp://localhost:60000")) { ZMQ.zmq_connect(requestSocket, endpoint); String msg = "Hello, World!"; if (args.Length > 0) { msg = args[0]; } using (var buffer = ASCII(msg)) ZMQ.zmq_send(requestSocket, buffer, buffer.Length, 0); String text; using (var buffer = new PinnedBuffer(new byte[64])) { ZMQ.zmq_recv(requestSocket, buffer, buffer.Length, 0); text = Encoding.ASCII.GetString(buffer); } Console.WriteLine(text); } ZMQ.zmq_close(requestSocket); ZMQ.zmq_ctx_term(zmqContext); }
protected internal virtual Msg ReceiveInternal(SendReceiveOptions options, out bool hasMore) { var msg = ZMQ.Recv(m_socketHandle, options); hasMore = msg.HasMore; return(msg); }
public ConsumerMessageStream(String stateStorageDirectory, String brokerAddress, BrokerInfoResponse configuration, ZMQ.Context context) { _stateStorageDirectory = stateStorageDirectory; _brokerAddress = brokerAddress; _configuration = configuration; _context = context; Messages = new ConcurrentQueue<Tuple<Int32, Message>>(); }
/// <summary> /// Close the socket /// </summary> public void Close() { if (!m_isClosed) { m_isClosed = true; ZMQ.Close(m_socketHandle); } }
static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: local_thr <bind-to> <message-size> <message-count>"); return(1); } string bindTo = args[0]; int messageSize = int.Parse(args[1]); int messageCount = int.Parse(args[2]); var context = ZMQ.CtxNew(); var pullSocket = ZMQ.Socket(context, ZmqSocketType.Pull); pullSocket.Bind(bindTo); var message = new Msg(); message.InitEmpty(); pullSocket.Recv(ref message, SendReceiveOptions.None); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i != messageCount - 1; i++) { pullSocket.Recv(ref message, SendReceiveOptions.None); if (message.Size != messageSize) { Console.WriteLine("message of incorrect size received. Received: " + message.Size + " Expected: " + messageSize); return(-1); } } stopWatch.Stop(); var millisecondsElapsed = stopWatch.ElapsedMilliseconds; if (millisecondsElapsed == 0) { millisecondsElapsed = 1; } message.Close(); double messagesPerSecond = (double)messageCount / millisecondsElapsed * 1000; double megabits = messagesPerSecond * messageSize * 8 / 1000000; Console.WriteLine("message size: {0} [B]", messageSize); Console.WriteLine("message count: {0}", messageCount); Console.WriteLine("mean throughput: {0:0.000} [msg/s]", messagesPerSecond); Console.WriteLine("mean throughput: {0:0.000} [Mb/s]", megabits); pullSocket.Close(); context.Terminate(); return(0); }
public ZmqBroker(ZMQ.Context zmqContext, string clientInboundAddress, string clientOutboundAddress, string serverInboundAddress, string serverOutboundAddress) { this.zmqContext = zmqContext; this.clientInboundAddress = clientInboundAddress; this.clientOutboundAddress = clientOutboundAddress; this.serverInboundAddress = serverInboundAddress; this.serverOutboundAddress = serverOutboundAddress; }
static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: remote_lat remote_lat <connect-to> <message-size> <roundtrip-count>"); return(1); } string connectTo = args[0]; int messageSize = int.Parse(args[1]); int roundtripCount = int.Parse(args[2]); var context = ZMQ.CtxNew(); var reqSocket = ZMQ.Socket(context, ZmqSocketType.Req); reqSocket.Connect(connectTo); var message = new Msg(); message.InitPool(messageSize); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i != roundtripCount; i++) { reqSocket.Send(ref message, SendReceiveOptions.None); reqSocket.Recv(ref message, SendReceiveOptions.None); if (message.Size != messageSize) { Console.WriteLine("message of incorrect size received. Received: " + message.Size + " Expected: " + messageSize); return(-1); } } stopWatch.Stop(); message.Close(); double elapsedMicroseconds = stopWatch.ElapsedTicks * 1000000 / Stopwatch.Frequency; double latency = elapsedMicroseconds / (roundtripCount * 2); Console.WriteLine("message size: {0} [B]", messageSize); Console.WriteLine("roundtrip count: {0}", roundtripCount); Console.WriteLine("average latency: {0:0.000} [µs]", latency); reqSocket.Close(); context.Terminate(); return(0); }
public void Monitor(string endpoint, SocketEvent events = SocketEvent.All) { if (endpoint == null) { throw new ArgumentNullException("endpoint"); } if (endpoint == string.Empty) { throw new ArgumentException("Unable to publish socket events to an empty endpoint.", "endpoint"); } ZMQ.SocketMonitor(SocketHandle, endpoint, events); }
public NetMQMonitor(NetMQContext context, NetMQSocket monitoredSocket, string endpoint, SocketEvent eventsToMonitor) { Endpoint = endpoint; Timeout = TimeSpan.FromSeconds(0.5); ZMQ.SocketMonitor(monitoredSocket.SocketHandle, Endpoint, eventsToMonitor); MonitoringSocket = context.CreatePairSocket(); MonitoringSocket.Options.Linger = TimeSpan.Zero; MonitoringSocket.ReceiveReady += Handle; m_isOwner = true; }
public ZmqClient(ZMQ.Context zmqContext, string inboundAddress, string outboundAddress) { this.zmqContext = zmqContext; this.inboundAddress = inboundAddress; this.outboundAddress = outboundAddress; this.identity = zmqContext.NewIdentity(); this.nextId = 0; this.requestCallbacks = new ConcurrentDictionary<string, TaskCompletionSource<ResponseData>>(); this.requestsQueue = new BlockingCollection<byte[]>(new ConcurrentQueue<byte[]>(), int.MaxValue); }
public ZmqServer(ZMQ.Context zmqContext, string inboundAddress, string outboundAddress, IServiceFactory serviceFactory) { this.zmqContext = zmqContext; this.inboundAddress = inboundAddress; this.outboundAddress = outboundAddress; this.identity = zmqContext.NewIdentity(); this.responsesQueue = new BlockingCollection<Tuple<byte[], byte[]>>(new ConcurrentQueue<Tuple<byte[], byte[]>>(), int.MaxValue); this.serviceFactory = serviceFactory; }
public void RunPipeline(Sockets.PairSocket shim) { m_pipe = shim; shim.SignalOK(); while (!m_terminated) { PollItem[] pollItems = new PollItem[] { new PollItem(m_pipe.SocketHandle, PollEvents.PollIn), new PollItem(m_udpSocket, PollEvents.PollIn), }; long timeout = -1; if (m_transmit != null) { timeout = m_pingAt - Clock.NowMs(); if (timeout < 0) { timeout = 0; } } ZMQ.Poll(pollItems, m_udpSocket != null ? 2 : 1, (int)timeout); if (pollItems[0].ResultEvent.HasFlag(PollEvents.PollIn)) { HandlePipe(); } if (pollItems[1].ResultEvent.HasFlag(PollEvents.PollIn)) { HandleUdp(); } if (m_transmit != null && Clock.NowMs() > m_pingAt) { SendUdpFrame(m_transmit); m_pingAt = Clock.NowMs() + m_interval; } } m_udpSocket.Dispose(); }
public NetMQSocket CreateSocket(ZmqSocketType socketType) { var socketHandle = ZMQ.Socket(m_ctx, socketType); switch (socketType) { case ZmqSocketType.Pair: return(new PairSocket(socketHandle)); case ZmqSocketType.Pub: return(new PublisherSocket(socketHandle)); case ZmqSocketType.Sub: return(new SubscriberSocket(socketHandle)); case ZmqSocketType.Req: return(new RequestSocket(socketHandle)); case ZmqSocketType.Rep: return(new ResponseSocket(socketHandle)); case ZmqSocketType.Dealer: return(new DealerSocket(socketHandle)); case ZmqSocketType.Router: return(new RouterSocket(socketHandle)); case ZmqSocketType.Pull: return(new PullSocket(socketHandle)); case ZmqSocketType.Push: return(new PushSocket(socketHandle)); case ZmqSocketType.Xpub: return(new XPublisherSocket(socketHandle)); case ZmqSocketType.Xsub: return(new XSubscriberSocket(socketHandle)); case ZmqSocketType.Stream: return(new StreamSocket(socketHandle)); default: throw new ArgumentOutOfRangeException("socketType"); } }
public Connection(ZMQ.Context ctx, string sender_id, string sub_addr, string pub_addr) { CTX = ctx; this.SenderId = sender_id; this.sub_addr = sub_addr; this.pub_addr = pub_addr; sendThread = new Thread(sendProc); sendThread.Name = "Mongrel2 Connection send thread"; sendThread.IsBackground = true; sendThread.Start(); recvThread = new Thread(recvProc); recvThread.Name = "Mongrel2 Connection receive thread"; recvThread.IsBackground = true; recvThread.Start(); }
public static NetMQMessage ReceiveMessage(this NetMQSocket socket, TimeSpan timeout) { var item = new PollItem(socket.SocketHandle, PollEvents.PollIn); var items = new[] { item }; ZMQ.Poll(items, (int)timeout.TotalMilliseconds); if (item.ResultEvent.HasFlag(PollEvents.PollError) && !socket.IgnoreErrors) { throw new ErrorPollingException("Error while polling", socket); } if (!item.ResultEvent.HasFlag(PollEvents.PollIn)) { return(null); } var msg = socket.ReceiveMessage(); return(msg); }
void puller_PollInHandler(ZMQ.Socket socket, ZMQ.IOMultiPlex revents) { var message = socket.Recv(UTF8Encoding.UTF8); Console.WriteLine("Message received" + message); }
internal void SetSocketOption(ZmqSocketOptions socketOptions, object value) { ZMQ.SetSocketOption(m_socketHandle, socketOptions, value); }
public virtual void Send(byte[] data, int length, SendReceiveOptions options) { Msg msg = new Msg(data, length, Options.CopyMessages); ZMQ.Send(m_socketHandle, msg, options); }
public PartitionConsumer(ConsumerConfiguration configuration, ZMQ.Context zeromqContext) { Initialize(configuration, zeromqContext); }
internal void SetSocketOptionTimeSpan(ZmqSocketOptions socketOptions, TimeSpan value) { ZMQ.SetSocketOption(m_socketHandle, socketOptions, (int)value.TotalMilliseconds); }
internal TimeSpan GetSocketOptionTimeSpan(ZmqSocketOptions socketOptions) { return(TimeSpan.FromMilliseconds(ZMQ.GetSocketOption(m_socketHandle, socketOptions))); }
/// <summary> /// Create a new context /// </summary> /// <returns>The new context</returns> public static NetMQContext Create() { return(new NetMQContext(ZMQ.CtxNew())); }
public void Start() { Thread.CurrentThread.Name = "NetMQPollerThread"; m_isStoppedEvent.Reset(); m_isStarted = true; try { // the sockets may have been created in another thread, to make sure we can fully use them we do full memory barried // at the begining of the loop Thread.MemoryBarrier(); // Recalculate all timers now foreach (NetMQTimer netMQTimer in m_timers) { if (netMQTimer.Enable) { netMQTimer.When = Clock.NowMs() + netMQTimer.Interval; } } while (!m_cancellationTokenSource.IsCancellationRequested) { if (m_isDirty) { RebuildPollset(); } ZMQ.Poll(m_pollset, m_pollSize, TicklessTimer()); // that way we make sure we can continue the loop if new timers are added. // timers cannot be removed int timersCount = m_timers.Count; for (int i = 0; i < timersCount; i++) { var timer = m_timers[i]; if (Clock.NowMs() >= timer.When && timer.When != -1) { timer.InvokeElapsed(this); if (timer.Enable) { timer.When = timer.Interval + Clock.NowMs(); } } } for (int itemNbr = 0; itemNbr < m_pollSize; itemNbr++) { NetMQSocket socket = m_pollact[itemNbr]; PollItem item = m_pollset[itemNbr]; if (item.ResultEvent.HasFlag(PollEvents.PollError) && !socket.IgnoreErrors) { socket.Errors++; if (socket.Errors > 1) { RemoveSocket(socket); item.ResultEvent = PollEvents.None; } } else { socket.Errors = 0; } if (item.ResultEvent != PollEvents.None) { socket.InvokeEvents(this, item.ResultEvent); } } if (m_zombies.Any()) { // Now handle any timer zombies // This is going to be slow if we have many zombies foreach (NetMQTimer netMQTimer in m_zombies) { m_timers.Remove(netMQTimer); } m_zombies.Clear(); } } } finally { m_isStoppedEvent.Set(); } }
internal int GetSocketOption(ZmqSocketOptions socketOptions) { return(ZMQ.GetSocketOption(m_socketHandle, socketOptions)); }
public Socket(ZMQ.Socket zmqSocket) { _zmqSocket = zmqSocket; }
public PartitionConsumer(String address, ZMQ.Context zeromqContext) { var configuration = new ConsumerConfiguration { Address = address }; Initialize(configuration, zeromqContext); }
/// <summary> /// Unbind the socket from specific address /// </summary> /// <param name="address">The address to unbind from</param> public void Unbind(string address) { ZMQ.Unbind(m_socketHandle, address); }
void Program_PollErrHandler(ZMQ.Socket socket, ZMQ.IOMultiPlex revents) { Console.WriteLine("ERROR"); }
internal long GetSocketOptionLong(ZmqSocketOptions socketOptions) { return((long)ZMQ.GetSocketOptionX(m_socketHandle, socketOptions)); }
private void PollWhile(Func <bool> condition) { if (m_disposed) { throw new ObjectDisposedException("Poller is disposed"); } if (m_isStarted) { throw new InvalidOperationException("Poller is started"); } if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = "NetMQPollerThread"; } m_isStoppedEvent.Reset(); m_isStarted = true; try { // the sockets may have been created in another thread, to make sure we can fully use them we do full memory barried // at the begining of the loop Thread.MemoryBarrier(); // Recalculate all timers now foreach (NetMQTimer netMQTimer in m_timers) { if (netMQTimer.Enable) { netMQTimer.When = Clock.NowMs() + netMQTimer.Interval; } } while (condition()) { if (m_isDirty) { RebuildPollset(); } var pollStart = Clock.NowMs(); var timeout = TicklessTimer(); var nbEvents = ZMQ.Poll(m_pollset, m_pollSize, timeout); // Get the expected end time in case we time out. This looks redundant but, unfortunately, // it happens that Poll takes slightly less than the requested time and 'Clock.NowMs() >= timer.When' // may not true, even if it is supposed to be. In other words, even when Poll times out, it happens // that 'Clock.NowMs() < pollStart + timeout' var expectedPollEnd = nbEvents == 0 ? pollStart + timeout : -1; // that way we make sure we can continue the loop if new timers are added. // timers cannot be removed int timersCount = m_timers.Count; for (int i = 0; i < timersCount; i++) { var timer = m_timers[i]; if ((Clock.NowMs() >= timer.When || expectedPollEnd >= timer.When) && timer.When != -1) { timer.InvokeElapsed(this); if (timer.Enable) { timer.When = timer.Interval + Clock.NowMs(); } } } for (int itemNbr = 0; itemNbr < m_pollSize; itemNbr++) { PollItem item = m_pollset[itemNbr]; if (item.Socket != null) { NetMQSocket socket = m_pollact[itemNbr] as NetMQSocket; if (item.ResultEvent.HasFlag(PollEvents.PollError) && !socket.IgnoreErrors) { socket.Errors++; if (socket.Errors > 1) { RemoveSocket(socket); item.ResultEvent = PollEvents.None; } } else { socket.Errors = 0; } if (item.ResultEvent != PollEvents.None) { socket.InvokeEvents(this, item.ResultEvent); } } else { if (item.ResultEvent.HasFlag(PollEvents.PollError) || item.ResultEvent.HasFlag(PollEvents.PollIn)) { Action <Socket> action; if (m_pollinSockets.TryGetValue(item.FileDescriptor, out action)) { action(item.FileDescriptor); } } } } if (m_zombies.Any()) { // Now handle any timer zombies // This is going to be slow if we have many zombies foreach (NetMQTimer netMQTimer in m_zombies) { m_timers.Remove(netMQTimer); } m_zombies.Clear(); } } } finally { m_isStoppedEvent.Set(); foreach (var socket in m_sockets.ToList()) { RemoveSocket(socket); } } }
internal T GetSocketOptionX <T>(ZmqSocketOptions socketOptions) { return((T)ZMQ.GetSocketOptionX(m_socketHandle, socketOptions)); }
/// <summary> /// Connect the socket to an address /// </summary> /// <param name="address">Address to connect to</param> public void Connect(string address) { ZMQ.Connect(m_socketHandle, address); }
private Socket CreateSocket(ZMQ.SocketType socketType) { var zmqsocket = _context.Socket(socketType); var socket = new Socket(zmqsocket); return socket; }
protected override byte[][] TryGetInput(ZMQ.Socket[] subs) { return mGenEvent.WaitOne(TIMEOUT) ? new byte[0][] : null; }
/// <summary> /// Disconnect the socket from specific address /// </summary> /// <param name="address">The address to disconnect from</param> public void Disconnect(string address) { ZMQ.Disconnect(m_socketHandle, address); }
/// <summary> /// Bind the socket to a random free port /// </summary> /// <param name="address">The address of the socket, omit the port</param> /// <returns>Chosen port number</returns> public int BindRandomPort(string address) { return(ZMQ.BindRandomPort(m_socketHandle, address)); }
public Socket CreateSocket(ZMQ.SocketType socketType) { var zmqsocket = _zeromqContext.Socket(socketType); var socket = new Socket(zmqsocket); return socket; }