public static MonitorEvent Read(SocketBase s) { Msg msg = s.Recv(0); if (msg == null) return null; int pos = 0; ByteArraySegment data = msg.Data; SocketEvent @event =(SocketEvent) data.GetInteger(pos); pos += 4; int len = (int)data[pos++]; string addr = data.GetString(len, pos); pos += len; int flag = (int)data[pos++]; Object arg = null; if (flag == ValueInteger) { arg = data.GetInteger(pos); } else if (flag == ValueChannel) { if (SizeOfIntPtr == 4) { arg = new IntPtr(data.GetInteger(pos)); } else { arg = new IntPtr(data.GetLong(pos)); } } return new MonitorEvent(@event, addr, arg); }
public static bool Connect(SocketBase s, String addr) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } return s.Connect(addr); }
public static void Close(SocketBase s) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } s.Close(); }
public SessionBase(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) : base(ioThread, options) { m_ioObject = new IOObject(ioThread); m_connect = connect; m_pipe = null; m_incompleteIn = false; m_pending = false; m_engine = null; m_socket = socket; m_ioThread = ioThread; m_hasLingerTimer = false; m_identitySent = false; m_identityReceived = false; m_addr = addr; m_terminatingPipes = new HashSet <Pipe> (); }
public static bool SocketMonitor(SocketBase s, String addr, SocketEvent events) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } return s.Monitor(addr, events); }
public static void SetSocketOption(SocketBase s, ZmqSocketOptions option, Object optval) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } s.SetSocketOption(option, optval); }
public RouterSession(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) : base(ioThread, connect, socket, options, addr) { }
public PollItem(SocketBase socket, PollEvents events) { Socket = socket; Events = events; FileDescriptor = null; }
// Send multiple messages. // // If flag bit ZMQ_SNDMORE is set the vector is treated as // a single multi-part message, i.e. the last message has // ZMQ_SNDMORE bit switched off. // public int SendIOv(SocketBase s, byte[][] a, int count, SendRecieveOptions flags) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } int rc = 0; Msg msg; for (int i = 0; i < count; ++i) { msg = new Msg(a[i]); if (i == count - 1) flags = flags & ~SendRecieveOptions.SendMore; rc = SendMsg(s, msg, flags); if (rc < 0) { rc = -1; break; } } return rc; }
public static int GetSocketOption(SocketBase s, ZmqSocketOptions opt) { return s.GetSocketOption(opt); }
public static Object GetSocketOptionX(SocketBase s, ZmqSocketOptions option) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } return s.GetSocketOptionX(option); }
public bool Monitor(String addr, SocketEvent events) { bool rc; if (m_ctxTerminated) { ZError.ErrorNumber = (ErrorNumber.ETERM); return false; } // Support deregistering monitoring endpoints as well if (addr == null) { StopMonitor(); return true; } // Parse addr_ string. Uri uri; try { uri = new Uri(addr); } catch (UriFormatException ex) { ZError.ErrorNumber = (ErrorNumber.EINVAL); throw new ArgumentException(addr, ex); } String protocol = uri.Scheme; String address = uri.Authority; String path = uri.AbsolutePath; if (string.IsNullOrEmpty(address)) address = path; CheckProtocol(protocol); // Event notification only supported over inproc:// if (!protocol.Equals("inproc")) { ZError.ErrorNumber = (ErrorNumber.EPROTONOSUPPORT); return false; } // Register events to monitor m_monitorEvents = events; m_monitorSocket = Ctx.CreateSocket(ZmqSocketType.Pair); if (m_monitorSocket == null) return false; // Never block context termination on pending event messages int linger = 0; rc = m_monitorSocket.SetSocketOption(ZmqSocketOptions.Linger, linger); if (!rc) StopMonitor(); // Spawn the monitor socket endpoint rc = m_monitorSocket.Bind(addr); if (!rc) StopMonitor(); return rc; }
protected SocketBase(Ctx parent, int tid, int sid) : base(parent, tid) { m_tag = 0xbaddecaf; m_ctxTerminated = false; m_destroyed = false; m_lastTsc = 0; m_ticks = 0; m_rcvMore = false; m_monitorSocket = null; m_monitorEvents = 0; m_options.SocketId = sid; m_endpoints = new Dictionary<string, Own>(); m_pipes = new List<Pipe>(); m_mailbox = new Mailbox("socket-" + sid); }
protected void StopMonitor() { if (m_monitorSocket != null) { m_monitorSocket.Close(); m_monitorSocket = null; m_monitorEvents = 0; } }
public ReqSession(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) : base(ioThread, connect, socket, options, addr) { m_state = State.Identity; }
public XPublisherSocket(SocketBase socketHandle) : base(socketHandle) { }
public static bool Unbind(SocketBase s, String addr) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } return s.TermEndpoint(addr); }
// Receiving functions. public static Msg Recv(SocketBase s, SendRecieveOptions flags) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } Msg msg = RecvMsg(s, flags); if (msg == null) { return null; } // At the moment an oversized message is silently truncated. // TODO: Build in a notification mechanism to report the overflows. //int to_copy = nbytes < len_ ? nbytes : len_; return msg; }
// Receive a multi-part message // // Receives up to *count_ parts of a multi-part message. // Sets *count_ to the actual number of parts read. // ZMQ_RCVMORE is set to indicate if a complete multi-part message was read. // Returns number of message parts read, or -1 on error. // // Note: even if -1 is returned, some parts of the message // may have been read. Therefore the client must consult // *count_ to retrieve message parts successfully read, // even if -1 is returned. // // The iov_base* buffers of each iovec *a_ filled in by this // function may be freed using free(). // // Implementation note: We assume zmq::msg_t buffer allocated // by zmq::recvmsg can be freed by free(). // We assume it is safe to steal these buffers by simply // not closing the zmq::msg_t. // public int RecvIOv(SocketBase s, byte[][] a, int count, SendRecieveOptions flags) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } int nread = 0; bool recvmore = true; for (int i = 0; recvmore && i < count; ++i) { // Cheat! We never close any msg // because we want to steal the buffer. Msg msg = RecvMsg(s, flags); if (msg == null) { nread = -1; break; } // Cheat: acquire zmq_msg buffer. a[i] = msg.Data; // Assume zmq_socket ZMQ_RVCMORE is properly set. recvmore = msg.HasMore; } return nread; }
public SubscriberSocket(SocketBase socketHandle) : base(socketHandle) { }
private static int SendMsg(SocketBase s, Msg msg, SendRecieveOptions flags) { int sz = MsgSize(msg); bool rc = s.Send(msg, flags); if (!rc) return -1; return sz; }
public RouterSocket(SocketBase socketHandle) : base(socketHandle) { }
public static Msg RecvMsg(SocketBase s, SendRecieveOptions flags) { return s.Recv(flags); }
protected override void ProcessReap(SocketBase socket) { // Add the socket to the poller. socket.StartReaping (m_poller); ++m_sockets; }
public IpcListener(IOThread ioThread, SocketBase socket, Options options) : base(ioThread, socket, options) { m_address = new IpcAddress(); }
// Sending functions. public static int Send(SocketBase s, String str, SendRecieveOptions flags) { byte[] data = Encoding.ASCII.GetBytes(str); return Send(s, data, data.Length, flags); }
public DealerSocket(SocketBase socketHandle) : base(socketHandle) { }
public static int Send(SocketBase s, Msg msg, SendRecieveOptions flags) { int rc = SendMsg(s, msg, flags); if (rc < 0) { return -1; } return rc; }
public static bool proxy(SocketBase frontend_, SocketBase backend_, SocketBase capture_) { // The algorithm below assumes ratio of requests and replies processed // under full load to be 1:1. // TODO: The current implementation drops messages when // any of the pipes becomes full. bool success = true; int rc; long more; Msg msg; PollItem[] items = new PollItem[2]; items[0] = new PollItem (frontend_, ZMQ.ZmqPollin ); items[1] = new PollItem (backend_, ZMQ.ZmqPollin ); Selector selector; try { selector = Selector.open(); } catch (IOException e) { throw new ZError.IOException(e); } try { while (!Thread.currentThread().isInterrupted()) { // Wait while there are either requests or replies to process. rc = ZMQ.zmq_poll (selector, items, -1); if (rc < 0) return false; // Process a request. if (items [0].isReadable()) { while (true) { msg = frontend_.Recv (0); if (msg == null) { return false; } more = frontend_.getsockopt (ZMQ.ZMQ_RCVMORE); if (more < 0) return false; // Copy message to capture socket if any if (capture_ != null) { Msg ctrl = new Msg (msg); success = capture_.Send (ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE: 0); if (!success) return false; } success = backend_.Send (msg, more > 0 ? ZMQ.ZMQ_SNDMORE: 0); if (!success) return false; if (more == 0) break; } } // Process a reply. if (items [1].isReadable()) { while (true) { msg = backend_.Recv (0); if (msg == null) { return false; } more = backend_.getsockopt (ZMQ.ZMQ_RCVMORE); if (more < 0) return false; // Copy message to capture socket if any if (capture_ != null) { Msg ctrl = new Msg (msg); success = capture_.Send (ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE: 0); if (!success) return false; } success = frontend_.Send (msg, more > 0 ? ZMQ.ZMQ_SNDMORE: 0); if (!success) return false; if (more == 0) break; } } } } finally { try { selector.close(); } catch (Exception e) { } } return true; }
public static int Send(SocketBase s, byte[] buf, int len, SendRecieveOptions flags) { if (s == null || !s.CheckTag()) { throw new InvalidOperationException(); } Msg msg = new Msg(len); msg.Put(buf, 0, len); int rc = SendMsg(s, msg, flags); if (rc < 0) { return -1; } return rc; }