예제 #1
0
파일: Ctx.cs 프로젝트: wangkai2014/netmq
        /// <summary>
        /// This function is called when user invokes zmq_term. If there are
        /// no more sockets open it'll cause all the infrastructure to be shut
        /// down. If there are open sockets still, the deallocation happens
        /// after the last one is closed.
        /// </summary>
        public void Terminate()
        {
            m_disposed = true;

            Monitor.Enter(m_slotSync);

            if (!m_starting)
            {
                // Check whether termination was already underway, but interrupted and now
                // restarted.
                bool restarted = m_terminating;
                m_terminating = true;
                Monitor.Exit(m_slotSync);

                // First attempt to terminate the context.
                if (!restarted)
                {
                    // First send stop command to sockets so that any blocking calls
                    // can be interrupted. If there are no sockets we can ask reaper
                    // thread to stop.
                    Monitor.Enter(m_slotSync);
                    try
                    {
                        foreach (var socket in m_sockets)
                        {
                            socket.Stop();
                        }

                        if (m_sockets.Count == 0)
                        {
                            m_reaper.Stop();
                        }
                    }
                    finally
                    {
                        Monitor.Exit(m_slotSync);
                    }
                }

                // Wait till reaper thread closes all the sockets.
                Command cmd = m_termMailbox.Recv(-1);

                Debug.Assert(cmd != null);
                Debug.Assert(cmd.CommandType == CommandType.Done);
                Monitor.Enter(m_slotSync);
                Debug.Assert(m_sockets.Count == 0);
            }
            Monitor.Exit(m_slotSync);

            // Deallocate the resources.
            Destroy();
        }
예제 #2
0
        /// <summary>
        /// Handle input-ready events, by receiving and processing any commands
        /// that are waiting in the mailbox.
        /// </summary>
        public void InEvent()
        {
            while (true)
            {
                // Get the next command. If there is none, exit.
                Command cmd = m_mailbox.Recv(0);
                if (cmd == null)
                {
                    break;
                }

                // Process the command.
                cmd.Destination.ProcessCommand(cmd);
            }
        }