Пример #1
0
        public void Monitor(String addr, SocketEvent events)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            // Support deregistering monitoring endpoints as well
            if (addr == null)
            {
                StopMonitor();
                return;
            }

            string address;
            string protocol;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            // Event notification only supported over inproc://
            if (!protocol.Equals("inproc"))
            {
                throw NetMQException.Create(ErrorCode.EPROTONOSUPPORT);
            }

            // Register events to monitor
            m_monitorEvents = events;

            m_monitorSocket = Ctx.CreateSocket(ZmqSocketType.Pair);
            if (m_monitorSocket == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }

            // Never block context termination on pending event messages
            int linger = 0;

            try
            {
                m_monitorSocket.SetSocketOption(ZmqSocketOptions.Linger, linger);
            }
            catch (NetMQException)
            {
                StopMonitor();
                throw;
            }

            // Spawn the monitor socket endpoint
            try
            {
                m_monitorSocket.Bind(addr);
            }
            catch (NetMQException)
            {
                StopMonitor();
                throw;
            }
        }
Пример #2
0
        //  Processes commands sent to this socket (if any). If timeout is -1,
        //  returns only after at least one command was processed.
        //  If throttle argument is true, commands are processed at most once
        //  in a predefined time period.
        private void ProcessCommands(int timeout, bool throttle)
        {
            Command cmd;

            if (timeout != 0)
            {
                //  If we are asked to wait, simply ask mailbox to wait.
                cmd = m_mailbox.Recv(timeout);
            }
            else
            {
                //  If we are asked not to wait, check whether we haven't processed
                //  commands recently, so that we can throttle the new commands.

                //  Get the CPU's tick counter. If 0, the counter is not available.
                long tsc = Clock.Rdtsc();

                //  Optimised version of command processing - it doesn't have to check
                //  for incoming commands each time. It does so only if certain time
                //  elapsed since last command processing. Command delay varies
                //  depending on CPU speed: It's ~1ms on 3GHz CPU, ~2ms on 1.5GHz CPU
                //  etc. The optimisation makes sense only on platforms where getting
                //  a timestamp is a very cheap operation (tens of nanoseconds).
                if (tsc != 0 && throttle)
                {
                    //  Check whether TSC haven't jumped backwards (in case of migration
                    //  between CPU cores) and whether certain time have elapsed since
                    //  last command processing. If it didn't do nothing.
                    if (tsc >= m_lastTsc && tsc - m_lastTsc <= Config.MaxCommandDelay)
                    {
                        return;
                    }
                    m_lastTsc = tsc;
                }

                //  Check whether there are any commands pending for this thread.
                cmd = m_mailbox.Recv(0);
            }

            //  Process all the commands available at the moment.
            while (true)
            {
                if (cmd == null)
                {
                    break;
                }

                cmd.Destination.ProcessCommand(cmd);
                cmd = m_mailbox.Recv(0);
            }
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }
        }
Пример #3
0
        public Object GetSocketOptionX(ZmqSocketOptions option)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            if (option == ZmqSocketOptions.ReceiveMore)
            {
                return(m_rcvMore ? 1 : 0);
            }

            if (option == ZmqSocketOptions.FD)
            {
                return(m_mailbox.FD);
            }

            if (option == ZmqSocketOptions.Events)
            {
                try
                {
                    ProcessCommands(0, false);
                }
                catch (NetMQException ex)
                {
                    Debug.Assert(false);

                    if (ex.ErrorCode == ErrorCode.EINTR || ex.ErrorCode == ErrorCode.ETERM)
                    {
                        return(-1);
                    }
                    else
                    {
                        throw;
                    }
                }

                int val = 0;
                if (HasOut())
                {
                    val |= ZMQ.ZmqPollout;
                }
                if (HasIn())
                {
                    val |= ZMQ.ZmqPollin;
                }
                return(val);
            }
            //  If the socket type doesn't support the option, pass it to
            //  the generic option parser.
            return(m_options.GetSocketOption(option));
        }
Пример #4
0
        public int GetSocketOption(ZmqSocketOptions option)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            if (option == ZmqSocketOptions.ReceiveMore)
            {
                return(m_rcvMore ? 1 : 0);
            }
            if (option == ZmqSocketOptions.Events)
            {
                try
                {
                    ProcessCommands(0, false);
                }
                catch (NetMQException ex)
                {
                    if (ex.ErrorCode == ErrorCode.EINTR || ex.ErrorCode == ErrorCode.ETERM)
                    {
                        return(-1);
                    }
                    else
                    {
                        Debug.Assert(false);

                        throw;
                    }
                }

                PollEvents val = 0;
                if (HasOut())
                {
                    val |= PollEvents.PollOut;
                }
                if (HasIn())
                {
                    val |= PollEvents.PollIn;
                }
                return((int)val);
            }

            return((int)GetSocketOptionX(option));
        }
Пример #5
0
        public bool TermEndpoint(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether endpoint address passed to the function is valid.
            if (addr == null)
            {
                throw InvalidException.Create();
            }

            //  Process pending commands, if any, since there could be pending unprocessed process_own()'s
            //  (from launch_child() for example) we're asked to terminate now.
            ProcessCommands(0, false);

            if (!m_endpoints.ContainsKey(addr))
            {
                return(false);
            }

            foreach (var e in m_endpoints)
            {
                TermChild(e.Value);
            }

            m_endpoints.Clear();

            //  Find the endpoints range (if any) corresponding to the addr_ string.
            //Iterator<Entry<String, Own>> it = endpoints.entrySet().iterator();

            //while(it.hasNext()) {
            //    Entry<String, Own> e = it.next();
            //    term_child(e.getValue());
            //    it.remove();
            //}
            return(true);
        }
Пример #6
0
        public void SetSocketOption(ZmqSocketOptions option, Object optval)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  First, check whether specific socket type overloads the option.

            try
            {
                XSetSocketOption(option, optval);
                return;
            }
            catch (InvalidException)
            {
            }

            //  If the socket type doesn't support the option, pass it to
            //  the generic option parser.
            m_options.SetSocketOption(option, optval);
        }
Пример #7
0
        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);
        }
Пример #8
0
        public Msg Recv(SendReceiveOptions flags)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            Msg msg;

            //  Get the message.
            bool isMessageAvailable = XRecv(flags, out msg);

            //  Once every inbound_poll_rate messages check for signals and process
            //  incoming commands. This happens only if we are not polling altogether
            //  because there are messages available all the time. If poll occurs,
            //  ticks is set to zero and thus we avoid this code.
            //
            //  Note that 'recv' uses different command throttling algorithm (the one
            //  described above) from the one used by 'send'. This is because counting
            //  ticks is more efficient than doing RDTSC all the time.
            if (++m_ticks == Config.InboundPollRate)
            {
                ProcessCommands(0, false);
                m_ticks = 0;
            }

            //  If we have the message, return immediately.
            if (isMessageAvailable && msg != null)
            {
                ExtractFlags(msg);
                return(msg);
            }

            //  If the message cannot be fetched immediately, there are two scenarios.
            //  For non-blocking recv, commands are processed in case there's an
            //  activate_reader command already waiting int a command pipe.
            //  If it's not, return EAGAIN.
            if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.ReceiveTimeout == 0)
            {
                ProcessCommands(0, false);
                m_ticks = 0;

                isMessageAvailable = XRecv(flags, out msg);
                if (!isMessageAvailable)
                {
                    throw AgainException.Create();
                }
                else if (msg == null)
                {
                    return(null);
                }
                ExtractFlags(msg);
                return(msg);
            }

            //  Compute the time when the timeout should occur.
            //  If the timeout is infite, don't care.
            int  timeout = m_options.ReceiveTimeout;
            long end     = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  In blocking scenario, commands are processed over and over again until
            //  we are able to fetch a message.
            bool block = (m_ticks != 0);

            while (true)
            {
                ProcessCommands(block ? timeout : 0, false);

                isMessageAvailable = XRecv(flags, out msg);
                if (isMessageAvailable && msg != null)
                {
                    m_ticks = 0;
                    break;
                }

                block = true;
                if (timeout > 0)
                {
                    timeout = (int)(end - Clock.NowMs());
                    if (timeout <= 0)
                    {
                        throw AgainException.Create();
                    }
                }
            }

            ExtractFlags(msg);
            return(msg);
        }
Пример #9
0
        public void Send(Msg msg, SendReceiveOptions flags)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether message passed to the function is valid.
            if (msg == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }

            //  Process pending commands, if any.
            ProcessCommands(0, true);

            //  Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            //  At this point we impose the flags on the message.
            if ((flags & SendReceiveOptions.SendMore) > 0)
            {
                msg.SetFlags(MsgFlags.More);
            }

            //  Try to send the message.

            bool isMessageSent = XSend(msg, flags);

            if (isMessageSent)
            {
                return;
            }


            //  In case of non-blocking send we'll simply propagate
            //  the error - including EAGAIN - up the stack.
            if ((flags & SendReceiveOptions.DontWait) > 0 || m_options.SendTimeout == 0)
            {
                throw AgainException.Create();
            }

            //  Compute the time when the timeout should occur.
            //  If the timeout is infite, don't care.
            int  timeout = m_options.SendTimeout;
            long end     = timeout < 0 ? 0 : (Clock.NowMs() + timeout);

            //  Oops, we couldn't send the message. Wait for the next
            //  command, process it and try to send the message again.
            //  If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeout, false);

                isMessageSent = XSend(msg, flags);
                if (isMessageSent)
                {
                    break;
                }

                if (timeout > 0)
                {
                    timeout = (int)(end - Clock.NowMs());
                    if (timeout <= 0)
                    {
                        throw AgainException.Create();
                    }
                }
            }
        }
Пример #10
0
        public void Connect(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Process pending commands, if any.
            ProcessCommands(0, false);


            string address;
            string protocol;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol.Equals("inproc"))
            {
                //  TODO: inproc connect is specific with respect to creating pipes
                //  as there's no 'reconnect' functionality implemented. Once that
                //  is in place we should follow generic pipe creation algorithm.

                //  Find the peer endpoint.
                Ctx.Endpoint peer = FindEndpoint(addr);

                // The total HWM for an inproc connection should be the sum of
                // the binder's HWM and the connector's HWM.
                int sndhwm;
                int rcvhwm;
                if (m_options.SendHighWatermark == 0 || peer.Options.ReceiveHighWatermark == 0)
                {
                    sndhwm = 0;
                }
                else
                {
                    sndhwm = m_options.SendHighWatermark + peer.Options.ReceiveHighWatermark;
                }
                if (m_options.ReceiveHighWatermark == 0 || peer.Options.SendHighWatermark == 0)
                {
                    rcvhwm = 0;
                }
                else
                {
                    rcvhwm = m_options.ReceiveHighWatermark + peer.Options.SendHighWatermark;
                }

                //  Create a bi-directional pipe to connect the peers.
                ZObject[] parents = { this, peer.Socket };
                int[]     hwms    = { sndhwm, rcvhwm };
                bool[]    delays  = { m_options.DelayOnDisconnect, m_options.DelayOnClose };
                Pipe[]    pipes   = Pipe.PipePair(parents, hwms, delays);

                //  Attach local end of the pipe to this socket object.
                AttachPipe(pipes[0]);

                //  If required, send the identity of the peer to the local socket.
                if (peer.Options.RecvIdentity)
                {
                    Msg id = new Msg(peer.Options.IdentitySize);
                    id.Put(peer.Options.Identity, 0, peer.Options.IdentitySize);
                    id.SetFlags(MsgFlags.Identity);
                    bool written = pipes[0].Write(id);
                    Debug.Assert(written);
                    pipes[0].Flush();
                }

                //  If required, send the identity of the local socket to the peer.
                if (m_options.RecvIdentity)
                {
                    Msg id = new Msg(m_options.IdentitySize);
                    id.Put(m_options.Identity, 0, m_options.IdentitySize);
                    id.SetFlags(MsgFlags.Identity);
                    bool written = pipes[1].Write(id);
                    Debug.Assert(written);
                    pipes[1].Flush();
                }

                //  Attach remote end of the pipe to the peer socket. Note that peer's
                //  seqnum was incremented in find_endpoint function. We don't need it
                //  increased here.
                SendBind(peer.Socket, pipes[1], false);

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }

            //  Choose the I/O thread to run the session in.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
            {
                throw NetMQException.Create("Empty IO Thread", ErrorCode.EMTHREAD);
            }
            Address paddr = new Address(protocol, address);

            //  Resolve address (if needed by the protocol)
            if (protocol.Equals("tcp"))
            {
                paddr.Resolved = (new TcpAddress());
                paddr.Resolved.Resolve(
                    address, m_options.IPv4Only);
            }
            else if (protocol.Equals("Ipc"))
            {
                paddr.Resolved = (new IpcAddress());
                paddr.Resolved.Resolve(address, true);
            }
            else if (protocol.Equals("pgm") || protocol.Equals("epgm"))
            {
                if (m_options.SocketType == ZmqSocketType.Sub || m_options.SocketType == ZmqSocketType.Xsub)
                {
                    Bind(addr);
                    return;
                }

                paddr.Resolved = new PgmAddress();
                paddr.Resolved.Resolve(address, m_options.IPv4Only);
            }

            //  Create session.
            SessionBase session = SessionBase.Create(ioThread, true, this, m_options, paddr);

            Debug.Assert(session != null);

            //  PGM does not support subscription forwarding; ask for all data to be
            //  sent to this pipe.
            bool icanhasall = protocol.Equals("pgm") || protocol.Equals("epgm");

            if (!m_options.DelayAttachOnConnect || icanhasall)
            {
                //  Create a bi-directional pipe.
                ZObject[] parents = { this, session };
                int[]     hwms    = { m_options.SendHighWatermark, m_options.ReceiveHighWatermark };
                bool[]    delays  = { m_options.DelayOnDisconnect, m_options.DelayOnClose };
                Pipe[]    pipes   = Pipe.PipePair(parents, hwms, delays);

                //  Attach local end of the pipe to the socket object.
                AttachPipe(pipes[0], icanhasall);

                //  Attach remote end of the pipe to the session object later on.
                session.AttachPipe(pipes[1]);
            }

            // Save last endpoint URI
            m_options.LastEndpoint = paddr.ToString();

            AddEndpoint(addr, session);
            return;
        }
Пример #11
0
        public void Bind(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Process pending commands, if any.
            ProcessCommands(0, false);

            string protocol;
            string address;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol.Equals("inproc"))
            {
                Ctx.Endpoint endpoint = new Ctx.Endpoint(this, m_options);

                RegisterEndpoint(addr, endpoint);

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }
            if ((protocol.Equals("pgm") || protocol.Equals("epgm")) && (
                    m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub))
            {
                //  For convenience's sake, bind can be used interchageable with
                //  connect for PGM and EPGM transports.
                Connect(addr);
                return;
            }

            //  Remaining trasnports require to be run in an I/O thread, so at this
            //  point we'll choose one.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
            {
                throw NetMQException.Create(ErrorCode.EMTHREAD);
            }

            if (protocol.Equals("tcp"))
            {
                TcpListener listener = new TcpListener(
                    ioThread, this, m_options);

                try
                {
                    listener.SetAddress(address);
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals("pgm") || protocol.Equals("epgm"))
            {
                PgmListener listener = new PgmListener(ioThread, this, m_options);

                try
                {
                    listener.Init(address);
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                m_options.LastEndpoint = addr;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals("ipc"))
            {
                IpcListener listener = new IpcListener(
                    ioThread, this, m_options);

                try
                {
                    listener.SetAddress(address);
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);
                return;
            }

            Debug.Assert(false);
            throw NetMQException.Create(ErrorCode.EFAULT);
        }
Пример #12
0
        public void TermEndpoint(String addr)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            //  Check whether endpoint address passed to the function is valid.
            if (addr == null)
            {
                throw InvalidException.Create();
            }

            //  Process pending commands, if any, since there could be pending unprocessed process_own()'s
            //  (from launch_child() for example) we're asked to terminate now.
            ProcessCommands(0, false);

            string protocol;
            string address;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol == Address.InProcProtocol)
            {
                try
                {
                    UnregisterEndpoint(addr, this);
                    return;
                }
                catch (NetMQException ex)
                {
                    if (ex.ErrorCode != ErrorCode.ENOENT)
                    {
                        throw;
                    }
                }

                Pipe pipe;

                if (m_inprocs.TryGetValue(addr, out pipe))
                {
                    pipe.Terminate(true);
                    m_inprocs.Remove(addr);
                }
                else
                {
                    throw NetMQException.Create(ErrorCode.ENOENT);
                }
            }
            else
            {
                Own endpoint;

                if (m_endpoints.TryGetValue(addr, out endpoint))
                {
                    TermChild(endpoint);

                    m_endpoints.Remove(addr);
                }
                else
                {
                    throw NetMQException.Create(ErrorCode.ENOENT);
                }
            }
        }