コード例 #1
0
        /// <summary>
        /// Receive a ZreMsgOriginal from the socket.
        /// </summary>
        public void Receive(IReceivingSocket input)
        {
            bool more;

            if (input is RouterSocket)
            {
                Msg routingIdMsg = new Msg();
                routingIdMsg.InitEmpty();

                try
                {
                    input.Receive(ref routingIdMsg);

                    if (!routingIdMsg.HasMore)
                    {
                        throw new MessageException("No routing id");
                    }

                    if (m_routingId == null || m_routingId.Length == routingIdMsg.Size)
                    {
                        m_routingId = new byte[routingIdMsg.Size];
                    }

                    Buffer.BlockCopy(routingIdMsg.Data, 0, m_routingId, 0, m_routingId.Length);
                }
                finally
                {
                    routingIdMsg.Close();
                }
            }
            else
            {
                RoutingId = null;
            }

            Msg msg = new Msg();

            msg.InitEmpty();

            try
            {
                input.Receive(ref msg);

                m_offset = 0;
                m_buffer = msg.Data;
                more     = msg.HasMore;

                UInt16 signature = GetNumber2();

                if (signature != (0xAAA0 | 1))
                {
                    throw new MessageException("Invalid signature");
                }

                //  Get message id and parse per message type
                Id = (MessageId)GetNumber1();

                switch (Id)
                {
                case MessageId.Hello:
                    Hello.Read(this);
                    break;

                case MessageId.Whisper:
                    Whisper.Read(this);
                    break;

                case MessageId.Shout:
                    Shout.Read(this);
                    break;

                case MessageId.Join:
                    Join.Read(this);
                    break;

                case MessageId.Leave:
                    Leave.Read(this);
                    break;

                case MessageId.Ping:
                    Ping.Read(this);
                    break;

                case MessageId.PingOk:
                    PingOk.Read(this);
                    break;

                default:
                    throw new MessageException("Bad message id");
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }
コード例 #2
0
        /// <summary>
        /// Receive a ZreMsg from the socket.
        /// Message is ignored if the message signature doesn't start with %xAA %xA1 (returns false).
        /// It appears "real" input will always be a RouterSocket, but DealerSocket is used during unit testing by zeromq/zyre and by this implementation.
        /// </summary>
        /// <param name="input">the socket</param>
        /// <returns>true if successful</returns>
        public bool Receive(IReceivingSocket input)
        {
            if (input is RouterSocket)
            {
                Msg routingIdMsg = new Msg();
                routingIdMsg.InitEmpty();
                try
                {
                    input.Receive(ref routingIdMsg);

                    if (!routingIdMsg.HasMore)
                    {
                        throw new MessageException("No routing id");
                    }

                    if (_routingId == null || _routingId.Length == routingIdMsg.Size)
                    {
                        _routingId = new byte[routingIdMsg.Size];
                    }

                    Buffer.BlockCopy(routingIdMsg.Data, 0, _routingId, 0, _routingId.Length);
                }
                finally
                {
                    routingIdMsg.Close();
                }
            }
            else
            {
                RoutingId = null;
            }
            Msg msg = new Msg();

            msg.InitEmpty();

            try
            {
                input.Receive(ref msg);

                _offset = 0;
                _buffer = msg.Data;

                UInt16 signature = GetNumber2();

                if (signature != (0xAAA0 | 1))
                {
                    // spec:36 A node SHALL silently discard any message received that does not start with these two octets.
                    return(false);
                }

                //  Get message id and parse per message type
                Id = (MessageId)GetNumber1();

                switch (Id)
                {
                case MessageId.Hello:
                    Hello.Read(this);
                    break;

                case MessageId.Whisper:
                    Whisper.Read(this);
                    break;

                case MessageId.Shout:
                    Shout.Read(this);
                    break;

                case MessageId.Join:
                    Join.Read(this);
                    break;

                case MessageId.Leave:
                    Leave.Read(this);
                    break;

                case MessageId.Ping:
                    Ping.Read(this);
                    break;

                case MessageId.PingOk:
                    PingOk.Read(this);
                    break;

                default:
                    throw new MessageException("Bad message id");
                }

                // Receive message content for types with content
                switch (Id)
                {
                case MessageId.Whisper:
                    Whisper.Content = input.ReceiveMultipartMessage();
                    break;

                case MessageId.Shout:
                    Shout.Content = input.ReceiveMultipartMessage();
                    break;
                }
            }
            finally
            {
                _buffer = null;
                msg.Close();
            }
            return(true);
        }