コード例 #1
0
ファイル: MonitorEvent.cs プロジェクト: knocte/netmq
        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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static bool Connect(SocketBase s, String addr)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     return s.Connect(addr);
 }
コード例 #3
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static void Close(SocketBase s)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     s.Close();
 }
コード例 #4
0
ファイル: SessionBase.cs プロジェクト: knocte/netmq
        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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        public static void SetSocketOption(SocketBase s, ZmqSocketOptions option, Object optval)
        {
            if (s == null || !s.CheckTag())
            {
                throw new InvalidOperationException();
            }

            s.SetSocketOption(option, optval);
        }
コード例 #7
0
ファイル: Router.cs プロジェクト: knocte/netmq
 public RouterSession(IOThread ioThread, bool connect,
     SocketBase socket, Options options,
     Address addr)
     : base(ioThread, connect, socket, options, addr)
 {
 }
コード例 #8
0
ファイル: PollItem.cs プロジェクト: knocte/netmq
 public PollItem(SocketBase socket, PollEvents events)
 {
     Socket = socket;
     Events = events;
     FileDescriptor = null;
 }
コード例 #9
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        // 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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static int GetSocketOption(SocketBase s, ZmqSocketOptions opt)
 {
     return s.GetSocketOption(opt);
 }
コード例 #11
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        public static Object GetSocketOptionX(SocketBase s, ZmqSocketOptions option)
        {
            if (s == null || !s.CheckTag())
            {
                throw new InvalidOperationException();
            }

            return s.GetSocketOptionX(option);
        }
コード例 #12
0
ファイル: SocketBase.cs プロジェクト: knocte/netmq
        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
ファイル: SocketBase.cs プロジェクト: knocte/netmq
        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
ファイル: SocketBase.cs プロジェクト: knocte/netmq
 protected void StopMonitor()
 {
     if (m_monitorSocket != null)
     {
         m_monitorSocket.Close();
         m_monitorSocket = null;
         m_monitorEvents = 0;
     }
 }
コード例 #15
0
ファイル: Req.cs プロジェクト: knocte/netmq
 public ReqSession(IOThread ioThread, bool connect,
     SocketBase socket, Options options,
     Address addr)
     : base(ioThread, connect, socket, options, addr)
 {
     m_state = State.Identity;
 }
コード例 #16
0
ファイル: XPublisherSocket.cs プロジェクト: knocte/netmq
 public XPublisherSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
コード例 #17
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static bool Unbind(SocketBase s, String addr)
 {
     if (s == null || !s.CheckTag())
     {
         throw new InvalidOperationException();
     }
     return s.TermEndpoint(addr);
 }
コード例 #18
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        // 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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        // 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
ファイル: SubscriberSocket.cs プロジェクト: knocte/netmq
 public SubscriberSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
コード例 #21
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 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
ファイル: RouterSocket.cs プロジェクト: knocte/netmq
 public RouterSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
コード例 #23
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 public static Msg RecvMsg(SocketBase s, SendRecieveOptions flags)
 {
     return s.Recv(flags);
 }
コード例 #24
0
ファイル: Reaper.cs プロジェクト: knocte/netmq
        protected override void ProcessReap(SocketBase socket)
        {
            //  Add the socket to the poller.
            socket.StartReaping (m_poller);

            ++m_sockets;
        }
コード例 #25
0
ファイル: IpcListener.cs プロジェクト: knocte/netmq
 public IpcListener(IOThread ioThread, SocketBase socket, Options options)
     : base(ioThread, socket, options)
 {
     m_address = new IpcAddress();
 }
コード例 #26
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
 // 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
ファイル: DealerSocket.cs プロジェクト: knocte/netmq
 public DealerSocket(SocketBase socketHandle)
     : base(socketHandle)
 {
 }
コード例 #28
0
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        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
ファイル: Proxy.cs プロジェクト: knocte/netmq
    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
ファイル: ZMQ.cs プロジェクト: knocte/netmq
        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;
        }