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; }
/// <summary> /// Sends HARD bytes from a byte[n]. Please don't use SendBytes, use instead SendFrame. /// </summary> public bool SendBytes(byte[] buffer, int offset, int count, ZSocketFlags flags, out ZError error) { EnsureNotDisposed(); error = ZError.None; // int zmq_send (void *socket, void *buf, size_t len, int flags); var pin = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr pinPtr = pin.AddrOfPinnedObject() + offset; int length; while (-1 == (length = zmq.send(SocketPtr, pinPtr, count, (int)flags))) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } pin.Free(); return(false); } pin.Free(); return(true); }
/// <summary> /// Shutdown the ZeroMQ context. /// </summary> public bool Shutdown(out ZError error) { error = default(ZError); if (_contextPtr == IntPtr.Zero) { return(true); } while (-1 == zmq.ctx_shutdown(_contextPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } // Maybe ZError.EFAULT return(false); } return(true); }
public virtual bool SendFrame(ZFrame frame, ZSocketFlags flags, out ZError error) { EnsureNotDisposed(); if (frame.IsDismissed) { throw new ObjectDisposedException("frame"); } error = default(ZError); while (-1 == zmq.msg_send(frame.Ptr, _socketPtr, (int)flags)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } return(false); } // Tell IDisposable to not unallocate zmq_msg frame.Dismiss(); return(true); }
/// <summary> /// Terminate the ZeroMQ context. /// </summary> public bool Terminate(out ZError error) { error = ZError.None; if (_contextPtr == IntPtr.Zero) { return(true); } while (-1 == zmq.ctx_term(_contextPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } // Maybe ZError.EFAULT return(false); } _contextPtr = IntPtr.Zero; return(true); }
internal static DispoIntPtr CreateNative(int size) { var msg = DispoIntPtr.Alloc(zmq.sizeof_zmq_msg_t); ZError error; while (-1 == zmq.msg_init_size(msg, size)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } msg.Dispose(); if (error == ZError.ENOMEM) { throw new OutOfMemoryException("zmq_msg_init_size"); } throw new ZException(error, "zmq_msg_init_size"); } return(msg); }
internal static unsafe bool PollSingle( ZSocket socket, ZPollItem item, ZPollEvent pollEvents, out ZError error, TimeSpan?timeout = null) { error = default(ZError); bool result = false; int timeoutMs = !timeout.HasValue ? -1 : (int)timeout.Value.TotalMilliseconds; zmq_pollitem_windows_t *native = stackalloc zmq_pollitem_windows_t[1]; // fixed (zmq_pollitem_windows_t* native = managedArray) { native->SocketPtr = socket.SocketPtr; native->Events = (short)(item.Events & pollEvents); native->ReadyEvents = (short)ZPollEvent.None; while (!(result = (-1 != zmq.poll(native, 1, timeoutMs)))) { error = ZError.GetLastErr(); /* if (error == ZmqError.EINTR) * { * error = default(ZmqError); * continue; * } */ break; } item.ReadyEvents = (ZPollEvent)native->ReadyEvents; // } return(result); }
public override void Close() { if (framePtr == null) { return; } if (framePtr.Ptr == IntPtr.Zero) { Dismiss(); return; } ZError error; while (-1 == zmq.msg_close(framePtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } if (error == ZError.EFAULT) { // Ignore: Invalid message. break; } return; } // Go unallocating the HGlobal Dismiss(); }
static string MakeMessage(ZError error, string additionalMessage) { return(error != null ? (string.IsNullOrEmpty(additionalMessage) ? error.ToString() : string.Format("{0}: {1}", error, additionalMessage)) : additionalMessage); }
static string MakeMessage(ZError error, string additionalMessage) { return(error != null ? (string.IsNullOrEmpty(additionalMessage) ? error.ToString() : $"{error}: {additionalMessage}") : additionalMessage); }
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); }
/// <summary> /// 一次Pool /// </summary> /// <returns></returns> public bool Poll() { error = null; zmq_pollitem_windows_t *natives = (zmq_pollitem_windows_t *)Ptr.Ptr; var state = zmq.poll(natives, Size, TimeoutMs); return(state > 0); }
protected bool Initialize(out ZError error) { error = default(ZError); if (IntPtr.Zero == (_socketPtr = zmq.socket(_context.ContextPtr, (Int32)_socketType))) { error = ZError.GetLastErr(); return(false); } return(true); }
public ZFrame ReceiveFrame(ZSocketFlags flags, out ZError error) { IEnumerable <ZFrame> frames = ReceiveFrames(1, flags & ~ZSocketFlags.More, out error); if (frames != null) { foreach (ZFrame frame in frames) { return(frame); } } return(null); }
/// <summary> /// Create a <see cref="ZSocket"/> instance. /// </summary> /// <returns><see cref="ZSocket"/></returns> public static ZSocket Create(ZContext context, ZSocketType socketType, out ZError error) { var socket = new ZSocket(); socket._context = context; socket._socketType = socketType; if (!socket.Initialize(out error)) { return(default(ZSocket)); } return(socket); }
public Int32 GetOption(ZFrameOption property, out ZError error) { error = ZError.None; int result; if (-1 == (result = zmq.msg_get(this.framePtr, (Int32)property))) { error = ZError.GetLastErr(); return(-1); } return(result); }
public static ZError FromErrno(int num) { // TODO: this can be made more efficient ZError symbol = Find("E", num).OfType <ZError>().FirstOrDefault(); if (symbol != null) { return(symbol); } // unexpected error return(new ZError(num)); }
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; }
static bool ReceiveMsg(ZSocket sock, ref ZMessage message, out string address, out ZError error) { error = ZError.None; // address = IPAddress.None; address = string.Empty; // STREAM: read frames: identity, body // read the ip4 address from (ZFrame)frame.GetOption("Peer-Address") int receiveCount = 2; do { var frame = ZFrame.CreateEmpty(); while (-1 == zmq.msg_recv(frame.Ptr, sock.SocketPtr, (int)(/* ZSocketFlags.DontWait | */ ZSocketFlags.More))) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } frame.Dispose(); return(false); } if (message == null) { message = new ZMessage(); } message.Add(frame); if (receiveCount == 2) { if (default(string) == (address = frame.GetOption("Peer-Address", out error))) { // just ignore error = default(ZError); address = string.Empty; } } } while (--receiveCount > 0); return(true); }
private bool GetOption(ZSocketOption option, IntPtr optionValue, ref int optionLength) { EnsureNotDisposed(); using (var optionLengthP = DispoIntPtr.Alloc(IntPtr.Size)) { if (IntPtr.Size == 4) { Marshal.WriteInt32(optionLengthP.Ptr, optionLength); } else if (IntPtr.Size == 8) { Marshal.WriteInt64(optionLengthP.Ptr, (long)optionLength); } else { throw new PlatformNotSupportedException(); } ZError error; while (-1 == zmq.getsockopt(this._socketPtr, (int)option, optionValue, optionLengthP.Ptr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } throw new ZException(error); } if (IntPtr.Size == 4) { optionLength = Marshal.ReadInt32(optionLengthP.Ptr); } else if (IntPtr.Size == 8) { optionLength = (int)Marshal.ReadInt64(optionLengthP.Ptr); } else { throw new PlatformNotSupportedException(); } } return(true); }
internal static unsafe bool PollMany( IEnumerable <ZSocket> sockets, IEnumerable <ZPollItem> items, ZPollEvent pollEvents, out ZError error, TimeSpan?timeout = null) { error = default(ZError); bool result = false; int count = items.Count(); int timeoutMs = !timeout.HasValue ? -1 : (int)timeout.Value.TotalMilliseconds; zmq_pollitem_windows_t *natives = stackalloc zmq_pollitem_windows_t[count]; // fixed (zmq_pollitem_windows_t* natives = managedArray) { for (int i = 0; i < count; ++i) { ZSocket socket = sockets.ElementAt(i); ZPollItem item = items.ElementAt(i); zmq_pollitem_windows_t *native = natives + i; native->SocketPtr = socket.SocketPtr; native->Events = (short)(item.Events & pollEvents); native->ReadyEvents = (short)ZPollEvent.None; } while (!(result = (-1 != zmq.poll(natives, count, timeoutMs)))) { error = ZError.GetLastErr(); // No Signalling on Windows /* if (error == ZmqError.EINTR) { * error = ZmqError.DEFAULT; * continue; * } */ break; } for (int i = 0; i < count; ++i) { ZPollItem item = items.ElementAt(i); zmq_pollitem_windows_t *native = natives + i; item.ReadyEvents = (ZPollEvent)native->ReadyEvents; } // } return(result); }
public ZException(ZError errorSymbol, string message, Exception inner) : base(default(string), inner) { if (errorSymbol != null) { this._errno = errorSymbol.Number; this._errname = errorSymbol.Name; this._errtext = errorSymbol.Text; } else { this._errno = -1; } _message = message; }
static ZSocket LPClient_CreateZSocket(ZContext context, string name, out ZError error) { // Helper function that returns a new configured socket // connected to the Lazy Pirate queue var requester = new ZSocket(context, ZSocketType.REQ); requester.IdentityString = name; requester.Linger = TimeSpan.FromMilliseconds(1); if (!requester.Connect("tcp://127.0.0.1:5555", out error)) { return null; } return requester; }
public static bool Poll(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZPoll pollEvents, ref ZMessage[] messages, out ZError error, TimeSpan? timeout = null) { if (PollMany(sockets, items, pollEvents, out error, timeout)) { if (PollManyResult(sockets, items, pollEvents, ref messages)) { return true; } error = ZError.EAGAIN; } return false; }
/// <summary> /// 准备 /// </summary> /// <param name="sockets"></param> /// <param name="events"></param> public void Prepare(ZPollEvent events, params ZSocket[] sockets) { Sockets = sockets; error = null; Size = sockets.Length; Ptr = DispoIntPtr.Alloc(sizeof(zmq_pollitem_windows_t) * sockets.Length); zmq_pollitem_windows_t *natives = (zmq_pollitem_windows_t *)Ptr.Ptr; for (int i = 0; i < Size; ++i) { zmq_pollitem_windows_t *native = natives + i; native->SocketPtr = sockets[i].SocketPtr; native->Events = (short)(events); native->ReadyEvents = (short)ZPollEvent.None; } }
/// <summary> /// Close the current socket. /// </summary> public bool Close(out ZError error) { error = ZError.None; if (_socketPtr == IntPtr.Zero) { return(true); } if (-1 == zmq.close(_socketPtr)) { error = ZError.GetLastErr(); return(false); } _socketPtr = IntPtr.Zero; return(true); }
unsafe internal static bool PollMany( IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZPoll pollEvents, out ZError error, TimeSpan? timeout = null) { error = default(ZError); bool result = false; int count = items.Count(); int timeoutMs = !timeout.HasValue ? -1 : (int)timeout.Value.TotalMilliseconds; zmq_pollitem_posix_t* natives = stackalloc zmq_pollitem_posix_t[count]; // fixed (zmq_pollitem_posix_t* natives = managedArray) { for (int i = 0; i < count; ++i) { ZSocket socket = sockets.ElementAt(i); ZPollItem item = items.ElementAt(i); zmq_pollitem_posix_t* native = natives + i; native->SocketPtr = socket.SocketPtr; native->Events = (short)(item.Events & pollEvents); native->ReadyEvents = (short)ZPoll.None; } while (!(result = (-1 != zmq.poll(natives, count, timeoutMs)))) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } break; } for (int i = 0; i < count; ++i) { ZPollItem item = items.ElementAt(i); zmq_pollitem_posix_t* native = natives + i; item.ReadyEvents = (ZPoll)native->ReadyEvents; } // } return result; }
public static bool ProxySteerable(ZSocket frontend, ZSocket backend, ZSocket capture, ZSocket control, out ZError error) { error = ZError.None; while (-1 == zmq.proxy_steerable(frontend.SocketPtr, backend.SocketPtr, capture == null ? IntPtr.Zero : capture.SocketPtr, control == null ? IntPtr.Zero : control.SocketPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } return(false); } return(true); }
/// <summary> /// Forwards requests from the frontend socket to the backend socket. /// </summary> protected override bool FrontendHandler(ZSocket sock, out ZMessage message, out ZError error) { error = default(ZError); message = null; // receiving scope // STREAM: get 2 frames, identity and body ZMessage incoming = null; // IPAddress address = null; string address; if (!ReceiveMsg(sock, ref incoming, out address, out error)) { return(false); } // sending scope // DEALER: forward using (incoming) { if (incoming[1].Length == 0) { return(true); // Ignore the Empty one } // Prepend empty delimiter between Identity frame and Data frame incoming.Insert(1, new ZFrame()); // Prepend Peer-Address incoming.Insert(2, new ZFrame(address)); try { if (!BackendSocket.Send(incoming, /* ZSocketFlags.DontWait, */ out error)) { return(false); } } finally { incoming.Dismiss(); } } return(true); }
public void SetOption(ZContextOption option, int optionValue) { EnsureNotDisposed(); int rc = zmq.ctx_set(_contextPtr, (Int32)option, optionValue); if (rc == -1) { var error = ZError.GetLastErr(); if (error == ZError.EINVAL) { throw new ArgumentOutOfRangeException( string.Format("The requested option optionName \"{0}\" is invalid.", option)); } throw new ZException(error); } }
public int GetOption(ZContextOption option) { EnsureNotDisposed(); int rc = zmq.ctx_get(_contextPtr, (Int32)option); if (rc != -1) { return(rc); } var error = ZError.GetLastErr(); if (Equals(error, ZError.EINVAL)) { throw new ArgumentOutOfRangeException( $"The requested option optionName \"{option}\" is invalid."); } throw new ZException(error); }
public List <ZPollItem> Poll(long timeout) { int rc = zmq.poll(natItems, items.Count, timeout); if (rc < 0) { throw new ZException(ZError.GetLastErr()); } List <ZPollItem> res = new List <ZPollItem>(); for (int i = 0; i < items.Count; i++) { if (natItems[i].ReadyEvents > 0) { res.Add(new ZPollItem(items[i].socket, natItems[i].ReadyEvents)); } } return(res); }
private bool SetOption(ZSocketOption option, IntPtr optionValue, int optionLength) { EnsureNotDisposed(); ZError error; while (-1 == zmq.setsockopt(this._socketPtr, (int)option, optionValue, optionLength)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } return(false); } return(true); }
public string GetOption(string property, out ZError error) { error = ZError.None; string result = null; using (var propertyPtr = DispoIntPtr.AllocString(property)) { IntPtr resultPtr; if (IntPtr.Zero == (resultPtr = zmq.msg_gets(this.framePtr, propertyPtr))) { error = ZError.GetLastErr(); return(null); } else { result = Marshal.PtrToStringAnsi(resultPtr); } } return(result); }
public void CopyZeroTo(ZFrame other) { // zmq.msg_copy(dest, src) ZError error; while (-1 == zmq.msg_copy(other.framePtr, framePtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } if (error == ZError.EFAULT) { // Invalid message. } throw new ZException(error, "zmq_msg_copy"); } }
/// <summary> /// Disconnect the specified endpoint. /// </summary> /// <param name="endpoint">A string consisting of a transport and an address, formatted as <c><em>transport</em>://<em>address</em></c>.</param> public bool Disconnect(string endpoint, out ZError error) { EnsureNotDisposed(); error = default(ZError); if (string.IsNullOrWhiteSpace(endpoint)) { throw new ArgumentException("IsNullOrWhiteSpace", "endpoint"); } using (var endpointPtr = DispoIntPtr.AllocString(endpoint)) { if (-1 == zmq.disconnect(_socketPtr, endpointPtr)) { error = ZError.GetLastErr(); return(false); } } return(true); }
static ZSocket PPWorker_CreateZSocket(ZContext context, string name, out ZError error) { // Helper function that returns a new configured socket // connected to the Paranoid Pirate queue var worker = new ZSocket(context, ZSocketType.DEALER); worker.IdentityString = name; if (!worker.Connect("tcp://127.0.0.1:5556", out error)) { return null; // Interrupted } // Tell queue we're ready for work using (var outgoing = new ZFrame(Worker.PPP_READY)) { worker.Send(outgoing); } Console.WriteLine("I: worker ready"); return worker; }
public static bool PollIn(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, out ZMessage[] incoming, out ZError error, TimeSpan? timeout = null) { incoming = null; return Poll(sockets, items, ZPoll.In, ref incoming, out error, timeout); }
public static bool PollOut(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZMessage[] outgoing, out ZError error, TimeSpan? timeout = null) { if (outgoing == null) { throw new ArgumentNullException("outgoing"); } return Poll(sockets, items, ZPoll.Out, ref outgoing, out error, timeout); }
public static bool DefaultReceiveMessage(ZSocket socket, out ZMessage message, out ZError error) { message = null; return socket.ReceiveMessage(ref message, out error); }
public static bool Proxy(ZSocket frontend, ZSocket backend, out ZError error) { return Proxy(frontend, backend, null, out error); }
/// <summary> /// Invoked when a message has been received by the frontend socket. /// </summary> /// <param name="args">A <see cref="SocketEventArgs"/> object containing the poll event args.</param> protected abstract bool FrontendHandler(ZSocket socket, out ZMessage message, out ZError error);
/// <summary> /// Invoked when a message has been received by the backend socket. /// </summary> /// <param name="args">A <see cref="SocketEventArgs"/> object containing the poll event args.</param> protected abstract bool BackendHandler(ZSocket args, out ZMessage message, out ZError error);
protected virtual bool Initialize(ZSocketType frontendType, ZSocketType backendType, out ZError error) { error = default(ZError); /* if (frontendType == ZSocketType.None && backendType == ZSocketType.None) { throw new InvalidOperationException(); } /**/ if (frontendType != ZSocketType.None) { if (null == (FrontendSocket = ZSocket.Create(Context, frontendType, out error))) { return false; } FrontendSetup = new ZSocketSetup(FrontendSocket); } if (backendType != ZSocketType.None) { if (null == (BackendSocket = ZSocket.Create(Context, backendType, out error))) { return false; } BackendSetup = new ZSocketSetup(BackendSocket); } return true; }
public Int32 GetOption(ZFrameOption property, out ZError error) { error = ZError.None; int result; if (-1 == (result = zmq.msg_get(this._framePtr, (Int32)property))) { error = ZError.GetLastErr(); return -1; } return result; }
public static bool ProxySteerable(ZSocket frontend, ZSocket backend, ZSocket capture, ZSocket control, out ZError error) { error = ZError.None; while (-1 == zmq.proxy_steerable(frontend.SocketPtr, backend.SocketPtr, capture == null ? IntPtr.Zero : capture.SocketPtr, control == null ? IntPtr.Zero : control.SocketPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } return false; } return true; }
public static bool ProxySteerable(ZSocket frontend, ZSocket backend, ZSocket control, out ZError error) { return ProxySteerable(frontend, backend, null, control, out error); }
/// <summary> /// Initializes a new instance of the <see cref="ZException"/> class. /// </summary> /// <param name="errorCode">The error code returned by the ZeroMQ library call.</param> public ZException(ZError errorSymbol, string message) : this(errorSymbol, message, default(Exception)) { }
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); }
public static bool DefaultSendMessage(ZSocket socket, ZMessage message, out ZError error) { return socket.Send(message, out error); }
/// <summary> /// Terminate the ZeroMQ context. /// </summary> public bool Terminate(out ZError error) { error = ZError.None; if (_contextPtr == IntPtr.Zero) return true; while (-1 == zmq.ctx_term(_contextPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } // Maybe ZError.EFAULT return false; } _contextPtr = IntPtr.Zero; return true; }
public string GetOption(string property, out ZError error) { error = ZError.None; string result = null; using (var propertyPtr = DispoIntPtr.AllocString(property)) { IntPtr resultPtr; if (IntPtr.Zero == (resultPtr = zmq.msg_gets(this._framePtr, propertyPtr))) { error = ZError.GetLastErr(); return null; } else { result = Marshal.PtrToStringAnsi(resultPtr); } } return result; }
/// <summary> /// Shutdown the ZeroMQ context. /// </summary> public bool Shutdown(out ZError error) { error = default(ZError); if (_contextPtr == IntPtr.Zero) return true; while (-1 == zmq.ctx_shutdown(_contextPtr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } // Maybe ZError.EFAULT return false; } // don't _contextPtr = IntPtr.Zero; return true; }
/// <summary> /// Initializes a new instance of the <see cref="ZException"/> class. /// </summary> /// <param name="errorCode">The error code returned by the ZeroMQ library call.</param> public ZException(ZError errorSymbol) : this(errorSymbol, default(string), default(Exception)) { }