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; if (options.RawSocket) { m_identitySent = true; m_identityReceived = true; } m_terminatingPipes = new HashSet<Pipe>(); }
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 PgmListener(IOThread ioThread, SocketBase socket, Options options) : base(ioThread, options) { m_socket = socket; m_ioObject = new IOObject(ioThread); }
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 static void Close(SocketBase s) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } s.Close(); }
public TcpListener(IOThread ioThread, SocketBase socket, Options options) : base(ioThread, options) { m_ioObject = new IOObject(ioThread); m_address = new TcpAddress(); m_handle = null; m_socket = socket; }
public static void Connect(SocketBase s, String addr) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } s.Connect(addr); }
public static int BindRandomPort(SocketBase s, String addr) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } return s.BindRandomPort(addr); }
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(Endianness.Little, 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(Endianness.Little, pos); } else if (flag == ValueChannel) { IntPtr value; if (SizeOfIntPtr == 4) { value = new IntPtr(data.GetInteger(Endianness.Little, pos)); } else { value = new IntPtr(data.GetLong(Endianness.Little, pos)); } GCHandle handle = GCHandle.FromIntPtr(value); Socket socket = null; if (handle.IsAllocated) { socket = handle.Target as Socket; } handle.Free(); arg = socket; } return new MonitorEvent(@event, addr, arg); }
public static SessionBase Create([NotNull] IOThread ioThread, bool connect, [NotNull] SocketBase socket, [NotNull] Options options, [NotNull] Address addr) { switch (options.SocketType) { case ZmqSocketType.Req: return(new Req.ReqSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Dealer: return(new Dealer.DealerSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Rep: return(new Rep.RepSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Router: return(new Router.RouterSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Pub: return(new Pub.PubSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Xpub: return(new XPub.XPubSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Sub: return(new Sub.SubSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Xsub: return(new XSub.XSubSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Push: return(new Push.PushSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Pull: return(new Pull.PullSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Pair: return(new Pair.PairSession(ioThread, connect, socket, options, addr)); case ZmqSocketType.Stream: return(new Stream.StreamSession(ioThread, connect, socket, options, addr)); default: throw new InvalidException("SessionBase.Create called with invalid SocketType of " + options.SocketType); } }
public void Plug(IOThread ioThread, SessionBase session) { Debug.Assert(!m_plugged); m_plugged = true; // Connect to session object. Debug.Assert(m_session == null); Debug.Assert(session != null); m_session = session; m_socket = m_session.Socket; m_ioObject = new IOObject(null); m_ioObject.SetHandler(this); // Connect to I/O threads poller object. m_ioObject.Plug(ioThread); m_ioObject.AddFd(m_handle); m_ioEnabled = true; if (m_options.RawSocket) { m_encoder = new RawEncoder(Config.OutBatchSize, session, m_options.Endian); m_decoder = new RawDecoder(Config.InBatchSize, m_options.Maxmsgsize, session, m_options.Endian); m_handshaking = false; } else { // Send the 'length' and 'flags' fields of the identity message. // The 'length' field is encoded in the long format. m_greetingOutputBuffer[m_outsize++] = ((byte)0xff); m_greetingOutputBuffer.PutLong(m_options.Endian, (long)m_options.IdentitySize + 1, 1); m_outsize += 8; m_greetingOutputBuffer[m_outsize++] = ((byte)0x7f); m_outpos = new ByteArraySegment(m_greetingOutputBuffer); } m_ioObject.SetPollin(m_handle); m_ioObject.SetPollout(m_handle); // Flush all the data that may have been already received downstream. InEvent(); }
protected SocketBase(Ctx parent, int threadId, int sid) : base(parent, threadId) { 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); }
public TcpConnecter(IOThread ioThread, SessionBase session, Options options, Address addr, bool delayedStart) : base(ioThread, options) { m_ioObject = new IOObject(ioThread); m_addr = addr; m_handle = null; m_s = null; m_handleValid = false; m_delayedStart = delayedStart; m_timerStarted = false; m_session = session; m_currentReconnectIvl = m_options.ReconnectIvl; Debug.Assert(m_addr != null); m_endpoint = m_addr.ToString(); m_socket = session.Socket; }
// Receiving functions. public static Msg Recv(SocketBase s, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } 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); }
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 void UnregisterEndpoint(string addr, SocketBase socket) { lock (m_endpointsSync) { Endpoint endpoint; if (m_endpoints.TryGetValue(addr, out endpoint)) { if (socket != endpoint.Socket) { throw NetMQException.Create(ErrorCode.ENOENT); } m_endpoints.Remove(addr); } else { throw NetMQException.Create(ErrorCode.ENOENT); } } }
public void Plug(IOThread ioThread, SessionBase session) { m_session = session; m_socket = session.Socket; m_ioObject = new IOObject(null); m_ioObject.SetHandler(this); m_ioObject.Plug(ioThread); m_ioObject.AddFd(m_handle); m_ioObject.SetPollin(m_handle); DropSubscriptions(); var msg = new Msg(); msg.InitEmpty(); // push message to the session because there is no identity message with pgm session.PushMsg(ref msg); }
public bool UnregisterEndpoint(string addr, SocketBase socket) { lock (m_endpointsSync) { Endpoint endpoint; if (m_endpoints.TryGetValue(addr, out endpoint)) { if (socket != endpoint.Socket) { return(false); } m_endpoints.Remove(addr); return(true); } else { return(false); } } }
public void DestroySocket(SocketBase socket) { // Free the associated thread slot. lock (m_slotSync) { int threadId = socket.ThreadId; m_emptySlots.Push(threadId); m_slots[threadId].Close(); m_slots[threadId] = null; // Remove the socket from the list of sockets. m_sockets.Remove(socket); // If zmq_term() was already called and there are no more socket // we can ask reaper thread to terminate. if (m_terminating && m_sockets.Count == 0) { m_reaper.Stop(); } } //LOG.debug("Released Slot [" + socket_ + "] "); }
public static bool Unbind(SocketBase s, String addr) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } return s.TermEndpoint(addr); }
public static void SetSocketOption(SocketBase s, ZmqSocketOptions option, Object optval) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } s.SetSocketOption(option, optval); }
public static void Send(SocketBase s, Msg msg, SendReceiveOptions flags) { SendMsg(s, msg, flags); }
public static Msg RecvMsg(SocketBase s, SendReceiveOptions flags) { return s.Recv(flags); }
// The proxy functionality public static bool Proxy(SocketBase frontend_, SocketBase backend_, SocketBase control_) { if (frontend_ == null || backend_ == null) { throw NetMQException.Create(ErrorCode.EFAULT); } return NetMQ.zmq.Proxy.CreateProxy( frontend_, backend_, control_); }
public static int GetSocketOption(SocketBase s, ZmqSocketOptions opt) { return s.GetSocketOption(opt); }
public static bool CreateProxy(SocketBase frontend, SocketBase backend, SocketBase capture) { // The algorithm below assumes ratio of requests and replies processed // under full load to be 1:1. int more; int rc; Msg msg; PollItem[] items = new PollItem[2]; items[0] = new PollItem(frontend, PollEvents.PollIn); items[1] = new PollItem(backend, PollEvents.PollIn); while (true) { // Wait while there are either requests or replies to process. rc = ZMQ.Poll(items, -1); if (rc < 0) { return(false); } // Process a request. if ((items[0].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn) { while (true) { try { msg = frontend.Recv(0); } catch (TerminatingException) { return(false); } if (msg == null) { return(false); } more = frontend.GetSocketOption(ZmqSocketOptions.ReceiveMore); if (more < 0) { return(false); } // Copy message to capture socket if any if (capture != null) { Msg ctrl = new Msg(msg); capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0); } backend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0); if (more == 0) { break; } } } // Process a reply. if ((items[1].ResultEvent & PollEvents.PollIn) == PollEvents.PollIn) { while (true) { try { msg = backend.Recv(0); } catch (TerminatingException) { return(false); } if (msg == null) { return(false); } more = backend.GetSocketOption(ZmqSocketOptions.ReceiveMore); if (more < 0) { return(false); } // Copy message to capture socket if any if (capture != null) { Msg ctrl = new Msg(msg); capture.Send(ctrl, more > 0 ? SendReceiveOptions.SendMore : 0); } frontend.Send(msg, more > 0 ? SendReceiveOptions.SendMore : 0); if (more == 0) { break; } } } } }
protected bool UnregisterEndpoint([NotNull] string addr, [NotNull] SocketBase socket) { return(m_ctx.UnregisterEndpoint(addr, socket)); }
protected void DestroySocket([NotNull] SocketBase socket) { m_ctx.DestroySocket(socket); }
public PollItem(SocketBase socket, PollEvents events) { Socket = socket; Events = events; FileDescriptor = null; }
protected void SendReap([NotNull] SocketBase socket) { SendCommand(new Command(m_ctx.GetReaper(), CommandType.Reap, socket)); }
// 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 void SendIOv(SocketBase s, byte[][] a, int count, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } Msg msg; for (int i = 0; i < count; ++i) { msg = new Msg(a[i]); if (i == count - 1) flags = flags & ~SendReceiveOptions.SendMore; SendMsg(s, msg, flags); } }
public Endpoint([NotNull] SocketBase socket, [NotNull] Options options) { Socket = socket; Options = options; }
public SocketBase CreateSocket(ZmqSocketType type) { SocketBase s = null; lock (m_slotSync) { if (m_starting) { m_starting = false; // Initialise the array of mailboxes. Additional three slots are for // zmq_term thread and reaper thread. int ios; int mazmq; lock (m_optSync) { mazmq = m_maxSockets; ios = m_ioThreadCount; } m_slotCount = mazmq + ios + 2; m_slots = new Mailbox[m_slotCount]; //alloc_Debug.Assert(slots); // Initialise the infrastructure for zmq_term thread. m_slots[TermTid] = m_termMailbox; // Create the reaper thread. m_reaper = new Reaper(this, ReaperTid); //alloc_Debug.Assert(reaper); m_slots[ReaperTid] = m_reaper.Mailbox; m_reaper.Start(); // Create I/O thread objects and launch them. for (int i = 2; i != ios + 2; i++) { IOThread ioThread = new IOThread(this, i); //alloc_Debug.Assert(io_thread); m_ioThreads.Add(ioThread); m_slots[i] = ioThread.Mailbox; ioThread.Start(); } // In the unused part of the slot array, create a list of empty slots. for (int i = (int)m_slotCount - 1; i >= (int)ios + 2; i--) { m_emptySlots.Push(i); m_slots[i] = null; } } // Once zmq_term() was called, we can't create new sockets. if (m_terminating) { throw TerminatingException.Create(); } // If max_sockets limit was reached, return error. if (m_emptySlots.Count == 0) { throw NetMQException.Create(ErrorCode.EMFILE); } // Choose a slot for the socket. int slot = m_emptySlots.Pop(); // Generate new unique socket ID. int sid = Interlocked.Increment(ref s_maxSocketId); // Create the socket and register its mailbox. s = SocketBase.Create(type, this, slot, sid); if (s == null) { m_emptySlots.Push(slot); return(null); } m_sockets.Add(s); m_slots[slot] = s.Mailbox; //LOG.debug("NEW Slot [" + slot + "] " + s); } return(s); }
protected void UnregisterEndpoints(SocketBase socket) { m_ctx.UnregisterEndpoints (socket); }
public Endpoint(SocketBase socket, Options options) { Socket = socket; Options = options; }
public static Object GetSocketOptionX(SocketBase s, ZmqSocketOptions option) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } return s.GetSocketOptionX(option); }
public SubscriberSocket(SocketBase socketHandle) : base(socketHandle) { }
// Receiving functions. public static Msg Recv(SocketBase s, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } 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; }
public SubSession(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) : base(ioThread, connect, socket, options, addr) { }
// Sending functions. public static void Send(SocketBase s, String str, SendReceiveOptions flags) { byte[] data = Encoding.ASCII.GetBytes(str); Send(s, data, data.Length, flags); }
public SocketBase CreateSocket(ZmqSocketType type) { lock (m_slotSync) { if (m_starting) { m_starting = false; // Initialise the array of mailboxes. Additional three slots are for // zmq_term thread and reaper thread. int ios; int mazmq; lock (m_optSync) { mazmq = m_maxSockets; ios = m_ioThreadCount; } m_slotCount = mazmq + ios + 2; m_slots = new IMailbox[m_slotCount]; //alloc_Debug.Assert(slots); // Initialise the infrastructure for zmq_term thread. m_slots[TermTid] = m_termMailbox; // Create the reaper thread. m_reaper = new Reaper(this, ReaperTid); //alloc_Debug.Assert(reaper); m_slots[ReaperTid] = m_reaper.Mailbox; m_reaper.Start(); // Create I/O thread objects and launch them. for (int i = 2; i != ios + 2; i++) { var ioThread = new IOThread(this, i); //alloc_Debug.Assert(io_thread); m_ioThreads.Add(ioThread); m_slots[i] = ioThread.Mailbox; ioThread.Start(); } // In the unused part of the slot array, create a list of empty slots. for (int i = m_slotCount - 1; i >= ios + 2; i--) { m_emptySlots.Push(i); m_slots[i] = null; } } // Once zmq_term() was called, we can't create new sockets. if (m_terminating) { string xMsg = String.Format("Ctx.CreateSocket({0}), cannot create new socket while terminating.", type); throw new TerminatingException(innerException: null, message: xMsg); } // If max_sockets limit was reached, return error. if (m_emptySlots.Count == 0) { #if DEBUG string xMsg = String.Format("Ctx.CreateSocket({0}), max number of sockets {1} reached.", type, m_maxSockets); throw NetMQException.Create(xMsg, ErrorCode.TooManyOpenSockets); #else throw NetMQException.Create(ErrorCode.TooManyOpenSockets); #endif } // Choose a slot for the socket. int slot = m_emptySlots.Pop(); // Generate new unique socket ID. int socketId = Interlocked.Increment(ref s_maxSocketId); // Create the socket and register its mailbox. SocketBase s = SocketBase.Create(type, this, slot, socketId); m_sockets.Add(s); m_slots[slot] = s.Mailbox; //LOG.debug("NEW Slot [" + slot + "] " + s); return(s); } }
public static void Send(SocketBase s, byte[] buf, int len, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } Msg msg = new Msg(len); msg.Put(buf, 0, len); SendMsg(s, msg, flags); }
protected void DestroySocket(SocketBase socket) { m_ctx.DestroySocket(socket); }
public static void SocketMonitor(SocketBase s, String addr, SocketEvent events) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } s.Monitor(addr, events); }
protected void SendReap(SocketBase socket) { Command cmd = new Command(m_ctx.GetReaper(), CommandType.Reap, socket); SendCommand(cmd); }
// 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, SendReceiveOptions flags) { if (s == null || !s.CheckTag()) { throw NetMQException.Create(ErrorCode.EFAULT); } 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; }
protected virtual void ProcessReap(SocketBase socket) { throw new NotSupportedException(); }
private static void SendMsg(SocketBase s, Msg msg, SendReceiveOptions flags) { s.Send(msg, flags); }
public static SessionBase Create(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) { SessionBase s; switch (options.SocketType) { case ZmqSocketType.Req: s = new Req.ReqSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Dealer: s = new Dealer.DealerSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Rep: s = new Rep.RepSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Router: s = new Router.RouterSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Pub: s = new Pub.PubSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Xpub: s = new XPub.XPubSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Sub: s = new Sub.SubSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Xsub: s = new XSub.XSubSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Push: s = new Push.PushSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Pull: s = new Pull.PullSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Pair: s = new Pair.PairSession(ioThread, connect, socket, options, addr); break; case ZmqSocketType.Stream: s = new Stream.StreamSession(ioThread, connect, socket, options, addr); break; default: throw InvalidException.Create("type=" + options.SocketType); } return(s); }
public IpcListener(IOThread ioThread, SocketBase socket, Options options) : base(ioThread, socket, options) { m_address = new IpcAddress(); }
protected bool UnregisterEndpoint(string addr, SocketBase socket) { return(m_ctx.UnregisterEndpoint(addr, socket)); }
/// <summary> /// Create a new StreamSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal StreamSocket(SocketBase socketHandle) : base(socketHandle) { }
public PushSocket(SocketBase socketHandle) : base(socketHandle) { }
public static int GetSocketOption(SocketBase s, ZmqSocketOptions opt) { return(s.GetSocketOption(opt)); }
public static Msg RecvMsg(SocketBase s, SendReceiveOptions flags) { return(s.Recv(flags)); }
public PublisherSocket(SocketBase socketHandle) : base(socketHandle) { }
protected void UnregisterEndpoint(string addr, SocketBase socket) { m_ctx.UnregisterEndpoint(addr, socket); }
public PairSession(IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) : base(ioThread, connect, socket, options, addr) { }
protected void UnregisterEndpoints(SocketBase socket) { m_ctx.UnregisterEndpoints(socket); }