public static ZPollItem Create(ReceiveDelegate receiveMessage, SendDelegate sendMessage) { var pollItem = new ZPollItem((receiveMessage != null ? ZPoll.In : ZPoll.None) | (sendMessage != null ? ZPoll.Out : ZPoll.None)); pollItem.ReceiveMessage = receiveMessage; pollItem.SendMessage = sendMessage; return pollItem; }
unsafe internal static bool PollSingle( ZSocket socket, ZPollItem item, ZPoll pollEvents, out ZError error, TimeSpan? timeout = null) { error = default(ZError); bool result = false; int timeoutMs = !timeout.HasValue ? -1 : (int)timeout.Value.TotalMilliseconds; zmq_pollitem_posix_t* native = stackalloc zmq_pollitem_posix_t[1]; // fixed (zmq_pollitem_posix_t* native = managedArray) { native->SocketPtr = socket.SocketPtr; native->Events = (short)(item.Events & pollEvents); native->ReadyEvents = (short)ZPoll.None; while (!(result = (-1 != zmq.poll(native, 1, timeoutMs)))) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } break; } item.ReadyEvents = (ZPoll)native->ReadyEvents; //} return result; }
public static bool PollOut(this ZSocket socket, ZPollItem item, ZMessage outgoing, out ZError error, TimeSpan? timeout = null) { if (outgoing == null) { throw new ArgumentNullException("outgoing"); } return Poll(socket, item, ZPoll.Out, ref outgoing, out error, timeout); }
internal static bool PollSingleResult(ZSocket socket, ZPollItem item, ZPoll pollEvents, ref ZMessage message) { bool shouldReceive = item.ReceiveMessage != null && ((pollEvents & ZPoll.In) == ZPoll.In); bool shouldSend = item.SendMessage != null && ((pollEvents & ZPoll.Out) == ZPoll.Out); if (pollEvents == ZPoll.In) { if (!shouldReceive) { throw new InvalidOperationException("No ReceiveMessage delegate set for Poll.In"); } if (OnReceiveMessage(socket, item, out message)) { if (!shouldSend) { return true; } if (OnSendMessage(socket, item, message)) { return true; } } } else if (pollEvents == ZPoll.Out) { if (!shouldSend) { throw new InvalidOperationException("No SendMessage delegate set for Poll.Out"); } if (OnSendMessage(socket, item, message)) { if (!shouldReceive) { return true; } if (OnReceiveMessage(socket, item, out message)) { return true; } } } return false; }
public static bool Poll(this ZSocket socket, ZPollItem item, ZPoll pollEvents, ref ZMessage message, out ZError error, TimeSpan? timeout = null) { if (PollSingle(socket, item, pollEvents, out error, timeout)) { if (PollSingleResult(socket, item, pollEvents, ref message)) { return true; } error = ZError.EAGAIN; } return false; }
public static void MSPoller(string[] args) { // // Reading from multiple sockets // This version uses zmq_poll() // // Author: metadings // using (var context = new ZContext()) using (var receiver = new ZSocket(context, ZSocketType.PULL)) using (var subscriber = new ZSocket(context, ZSocketType.SUB)) { // Connect to task ventilator receiver.Connect("tcp://127.0.0.1:5557"); // Connect to weather server subscriber.Connect("tcp://127.0.0.1:5556"); subscriber.SetOption(ZSocketOption.SUBSCRIBE, "10001 "); var sockets = new ZSocket[] { receiver, subscriber }; var polls = new ZPollItem[] { ZPollItem.CreateReceiver(), ZPollItem.CreateReceiver() }; // Process messages from both sockets ZError error; ZMessage[] msg; while (true) { if (sockets.PollIn(polls, out msg, out error, TimeSpan.FromMilliseconds(64))) { if (msg[0] != null) { // Process task } if (msg[1] != null) { // Process weather update } } else { if (error == ZError.ETERM) return; // Interrupted if (error != ZError.EAGAIN) throw new ZException(error); } } } }
internal static bool PollSingleResult(ZSocket socket, ZPollItem item, ZPoll pollEvents, ref ZMessage message) { bool shouldReceive = item.ReceiveMessage != null && ((pollEvents & ZPoll.In) == ZPoll.In); bool shouldSend = item.SendMessage != null && ((pollEvents & ZPoll.Out) == ZPoll.Out); if (pollEvents == ZPoll.In) { if (!shouldReceive) { throw new InvalidOperationException("No ReceiveMessage delegate set for Poll.In"); } if (OnReceiveMessage(socket, item, out message)) { if (!shouldSend) { return(true); } if (OnSendMessage(socket, item, message)) { return(true); } } } else if (pollEvents == ZPoll.Out) { if (!shouldSend) { throw new InvalidOperationException("No SendMessage delegate set for Poll.Out"); } if (OnSendMessage(socket, item, message)) { if (!shouldReceive) { return(true); } if (OnReceiveMessage(socket, item, out message)) { return(true); } } } return(false); }
/// <summary> /// Construct authourization handler /// </summary> /// <param name="context"></param> /// <param name="pipe"></param> /// <param name="certStore"></param> private ZAuth(ZContext context, ZSocket pipe, ZCertStore certStore = null) { if (context != null) { sockets = new ZSocket[] { pipe, new ZSocket(context, ZSocketType.REP) }; } else { sockets = new ZSocket[] { pipe, new ZSocket(ZSocketType.REP) }; } sockets[HANDLER].Bind(ZAP_ENDPOINT); pollers = new ZPollItem[] { ZPollItem.CreateReceiver(), ZPollItem.CreateReceiver() }; allowAny = true; verbose = false; Terminated = false; this.certStore = certStore; }
internal static bool OnSendMessage(ZSocket socket, ZPollItem item, ZMessage message) { if (((ZPoll)item.ReadyEvents & ZPoll.Out) == ZPoll.Out) { ZError sendWorkerE; if (item.SendMessage == null) { // throw? } else if (item.SendMessage(socket, message, out sendWorkerE)) { // what to do? return(true); } } return(false); }
internal static bool OnReceiveMessage(ZSocket socket, ZPollItem item, out ZMessage message) { message = null; if (((ZPoll)item.ReadyEvents & ZPoll.In) == ZPoll.In) { ZError recvWorkerE; if (item.ReceiveMessage == null) { // throw? } else if (item.ReceiveMessage(socket, out message, out recvWorkerE)) { // what to do? return(true); } } return(false); }
internal static bool PollManyResult(IEnumerable <ZSocket> sockets, IEnumerable <ZPollItem> items, ZPoll pollEvents, ref ZMessage[] messages) { int count = items.Count(); int readyCount = 0; bool send = messages != null && ((pollEvents & ZPoll.Out) == ZPoll.Out); bool receive = ((pollEvents & ZPoll.In) == ZPoll.In); ZMessage[] incoming = null; if (receive) { incoming = new ZMessage[count]; } for (int i = 0; i < count; ++i) { ZSocket socket = sockets.ElementAt(i); ZPollItem item = items.ElementAt(i); ZMessage message = send ? messages[i] : null; if (ZPollItems.PollSingleResult(socket, item, pollEvents, ref message)) { ++readyCount; } if (receive) { incoming[i] = message; } } if (receive) { messages = incoming; } return(readyCount > 0); }
public static bool PollIn(this ZSocket socket, ZPollItem item, out ZMessage incoming, out ZError error, TimeSpan?timeout = null) { incoming = null; return(Poll(socket, item, ZPoll.In, ref incoming, out error, timeout)); }
/// <summary> /// Start the device in the current thread. Should be used by implementations of the <see cref="DeviceRunner.Start"/> method. /// </summary> /// <remarks> /// Initializes the sockets prior to starting the device with <see cref="Initialize"/>. /// </remarks> protected override void Run() { EnsureNotDisposed(); Initialize(); ZSocket[] sockets; ZPollItem[] polls; if (FrontendSocket != null && BackendSocket != null) { sockets = new ZSocket[] { FrontendSocket, BackendSocket }; polls = new ZPollItem[] { ZPollItem.Create(FrontendHandler), ZPollItem.Create(BackendHandler) }; } else if (FrontendSocket != null) { sockets = new ZSocket[] { FrontendSocket }; polls = new ZPollItem[] { ZPollItem.Create(FrontendHandler) }; } else { sockets = new ZSocket[] { BackendSocket }; polls = new ZPollItem[] { ZPollItem.Create(BackendHandler) }; } /* ZPollItem[] polls; * { * var pollItems = new List<ZPollItem>(); * switch (FrontendSocket.SocketType) * { * case ZSocketType.Code.ROUTER: * case ZSocketType.Code.XSUB: * case ZSocketType.Code.PUSH: * // case ZSocketType.Code.STREAM: * pollItems.Add(new ZPollItem(FrontendSocket, ZPoll.In) * { * ReceiveMessage = FrontendHandler * }); * * break; * } * switch (BackendSocket.SocketType) * { * case ZSocketType.Code.DEALER: * // case ZSocketType.Code.STREAM: * pollItems.Add(new ZPollItem(BackendSocket, ZPoll.In) * { * ReceiveMessage = BackendHandler * }); * * break; * } * polls = pollItems.ToArray(); * } */ // Because of using ZmqSocket.Forward, this field will always be null ZMessage[] lastMessageFrames = null; if (FrontendSetup != null) { FrontendSetup.BindConnect(); } if (BackendSetup != null) { BackendSetup.BindConnect(); } bool isValid = false; var error = default(ZError); try { while (_running) { if (!(isValid = sockets.Poll(polls, ZPoll.In, ref lastMessageFrames, out error, PollingInterval))) { if (error == ZError.EAGAIN) { Thread.Sleep(1); continue; } if (error == ZError.ETERM) { break; } // EFAULT throw new ZException(error); } } } catch (ZException) { // Swallow any exceptions thrown while stopping if (_running) { throw; } } if (FrontendSetup != null) { FrontendSetup.UnbindDisconnect(); } if (BackendSetup != null) { BackendSetup.UnbindDisconnect(); } if (error == ZError.ETERM) { Close(); } }
/// <summary> /// Start the device in the current thread. Should be used by implementations of the <see cref="DeviceRunner.Start"/> method. /// </summary> /// <remarks> /// Initializes the sockets prior to starting the device with <see cref="Initialize"/>. /// </remarks> protected override void Run() { EnsureNotDisposed(); Initialize(); ZSocket[] sockets; ZPollItem[] polls; if (FrontendSocket != null && BackendSocket != null) { sockets = new ZSocket[] { FrontendSocket, BackendSocket }; polls = new ZPollItem[] { ZPollItem.Create(FrontendHandler), ZPollItem.Create(BackendHandler) }; } else if (FrontendSocket != null) { sockets = new ZSocket[] { FrontendSocket }; polls = new ZPollItem[] { ZPollItem.Create(FrontendHandler) }; } else { sockets = new ZSocket[] { BackendSocket }; polls = new ZPollItem[] { ZPollItem.Create(BackendHandler) }; } /* ZPollItem[] polls; { var pollItems = new List<ZPollItem>(); switch (FrontendSocket.SocketType) { case ZSocketType.Code.ROUTER: case ZSocketType.Code.XSUB: case ZSocketType.Code.PUSH: // case ZSocketType.Code.STREAM: pollItems.Add(new ZPollItem(FrontendSocket, ZPoll.In) { ReceiveMessage = FrontendHandler }); break; } switch (BackendSocket.SocketType) { case ZSocketType.Code.DEALER: // case ZSocketType.Code.STREAM: pollItems.Add(new ZPollItem(BackendSocket, ZPoll.In) { ReceiveMessage = BackendHandler }); break; } polls = pollItems.ToArray(); } */ // Because of using ZmqSocket.Forward, this field will always be null ZMessage[] lastMessageFrames = null; if (FrontendSetup != null) FrontendSetup.BindConnect(); if (BackendSetup != null) BackendSetup.BindConnect(); bool isValid = false; var error = default(ZError); try { while (!Cancellor.IsCancellationRequested) { if (!(isValid = sockets.Poll(polls, ZPoll.In, ref lastMessageFrames, out error, PollingInterval))) { if (error == ZError.EAGAIN) { Thread.Sleep(1); continue; } if (error == ZError.ETERM) { break; } // EFAULT throw new ZException(error); } } } catch (ZException) { // Swallow any exceptions thrown while stopping if (!Cancellor.IsCancellationRequested) { throw; } } if (FrontendSetup != null) FrontendSetup.UnbindDisconnect(); if (BackendSetup != null) BackendSetup.UnbindDisconnect(); if (error == ZError.ETERM) { Close(); } }
internal static bool OnReceiveMessage(ZSocket socket, ZPollItem item, out ZMessage message) { message = null; if (((ZPoll)item.ReadyEvents & ZPoll.In) == ZPoll.In) { ZError recvWorkerE; if (item.ReceiveMessage == null) { // throw? } else if (item.ReceiveMessage(socket, out message, out recvWorkerE)) { // what to do? return true; } } return false; }
internal static bool OnSendMessage(ZSocket socket, ZPollItem item, ZMessage message) { if (((ZPoll)item.ReadyEvents & ZPoll.Out) == ZPoll.Out) { ZError sendWorkerE; if (item.SendMessage == null) { // throw? } else if (item.SendMessage(socket, message, out sendWorkerE)) { // what to do? return true; } } return false; }
public static bool PollIn(this ZSocket socket, ZPollItem item, out ZMessage incoming, out ZError error, TimeSpan? timeout = null) { incoming = null; return Poll(socket, item, ZPoll.In, ref incoming, out error, timeout); }