Пример #1
0
        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);
        }
Пример #2
0
 public static bool Connect(SocketBase s, String addr)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     return s.Connect(addr);
 }
Пример #3
0
 public static void Close(SocketBase s)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     s.Close();
 }
Пример #4
0
        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> ();
        }
Пример #5
0
        public static bool SocketMonitor(SocketBase s, String addr, SocketEvent events)
        {
            if (s == null || !s.CheckTag())
            {
                throw new InvalidOperationException();
            }

            return s.Monitor(addr, events);
        }
Пример #6
0
        public static void SetSocketOption(SocketBase s, ZmqSocketOptions option, Object optval)
        {
            if (s == null || !s.CheckTag())
            {
                throw new InvalidOperationException();
            }

            s.SetSocketOption(option, optval);
        }
Пример #7
0
 public RouterSession(IOThread ioThread, bool connect,
     SocketBase socket, Options options,
     Address addr)
     : base(ioThread, connect, socket, options, addr)
 {
 }
Пример #8
0
 public PollItem(SocketBase socket, PollEvents events)
 {
     Socket = socket;
     Events = events;
     FileDescriptor = null;
 }
Пример #9
0
        // 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;
        }
Пример #10
0
 public static int GetSocketOption(SocketBase s, ZmqSocketOptions opt)
 {
     return s.GetSocketOption(opt);
 }
Пример #11
0
        public static Object GetSocketOptionX(SocketBase s, ZmqSocketOptions option)
        {
            if (s == null || !s.CheckTag())
            {
                throw new InvalidOperationException();
            }

            return s.GetSocketOptionX(option);
        }
Пример #12
0
        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;
        }
Пример #13
0
        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);
        }
Пример #14
0
 protected void StopMonitor()
 {
     if (m_monitorSocket != null)
     {
         m_monitorSocket.Close();
         m_monitorSocket = null;
         m_monitorEvents = 0;
     }
 }
Пример #15
0
 public ReqSession(IOThread ioThread, bool connect,
     SocketBase socket, Options options,
     Address addr)
     : base(ioThread, connect, socket, options, addr)
 {
     m_state = State.Identity;
 }
Пример #16
0
 public XPublisherSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
Пример #17
0
 public static bool Unbind(SocketBase s, String addr)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     return s.TermEndpoint(addr);
 }
Пример #18
0
        // 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;
        }
Пример #19
0
        // 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;
        }
Пример #20
0
 public SubscriberSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
Пример #21
0
 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;
 }
Пример #22
0
 public RouterSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
Пример #23
0
 public static Msg RecvMsg(SocketBase s, SendRecieveOptions flags)
 {
     return s.Recv(flags);
 }
Пример #24
0
        protected override void ProcessReap(SocketBase socket)
        {
            //  Add the socket to the poller.
            socket.StartReaping (m_poller);

            ++m_sockets;
        }
Пример #25
0
 public IpcListener(IOThread ioThread, SocketBase socket, Options options)
     : base(ioThread, socket, options)
 {
     m_address = new IpcAddress();
 }
Пример #26
0
 // 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);
 }
Пример #27
0
 public DealerSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
Пример #28
0
        public static int Send(SocketBase s, Msg msg, SendRecieveOptions flags)
        {
            int rc = SendMsg(s, msg, flags);
            if (rc < 0)
            {
                return -1;
            }

            return rc;
        }
Пример #29
0
    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;
    }
Пример #30
0
        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;
        }