Exemple #1
0
        public StreamEngine(AsyncSocket handle, Options options, String endpoint)
        {
            m_handle         = handle;
            m_insize         = 0;
            m_ioEnabled      = false;
            m_sendingState   = SendState.Idle;
            m_receivingState = ReceiveState.Idle;
            m_outsize        = 0;
            m_session        = null;
            m_options        = options;
            m_plugged        = false;
            m_endpoint       = endpoint;
            m_socket         = null;
            m_encoder        = null;
            m_decoder        = null;
            m_actionsQueue   = new Queue <StateMachineAction>();

            //  Set the socket buffer limits for the underlying socket.
            if (m_options.SendBuffer != 0)
            {
                m_handle.SendBufferSize = m_options.SendBuffer;
            }
            if (m_options.ReceiveBuffer != 0)
            {
                m_handle.ReceiveBufferSize = m_options.ReceiveBuffer;
            }
        }
        public StreamEngine(AsyncSocket handle, Options options, string endpoint)
        {
            m_handle = handle;
            m_insize = 0;
            m_ioEnabled = false;
            m_sendingState = SendState.Idle;
            m_receivingState = ReceiveState.Idle;
            m_outsize = 0;
            m_session = null;
            m_options = options;
            m_plugged = false;
            m_endpoint = endpoint;
            m_socket = null;
            m_encoder = null;
            m_decoder = null;
            m_actionsQueue = new Queue<StateMachineAction>();

            //  Set the socket buffer limits for the underlying socket.
            if (m_options.SendBuffer != 0)
            {
                m_handle.SendBufferSize = m_options.SendBuffer;
            }
            if (m_options.ReceiveBuffer != 0)
            {
                m_handle.ReceiveBufferSize = m_options.ReceiveBuffer;
            }
        }
        private void HandleHandshake(Action action, SocketError socketError, int bytesTransferred)
        {
            int bytesSent;
            int bytesReceived;

            switch (m_handshakeState)
            {
                case HandshakeState.Closed:
                    switch (action)
                    {
                        case Action.Start:
                            //  Send the 'length' and 'flags' fields of the identity message.
                            //  The 'length' field is encoded in the long format.

                            m_greetingOutputBuffer[m_outsize++] = ((byte)0xff);
                            m_greetingOutputBuffer.PutLong(m_options.Endian, (long)m_options.IdentitySize + 1, 1);
                            m_outsize += 8;
                            m_greetingOutputBuffer[m_outsize++] = ((byte)0x7f);

                            m_outpos = new ByteArraySegment(m_greetingOutputBuffer);

                            m_handshakeState = HandshakeState.SendingGreeting;

                            BeginWrite(m_outpos, m_outsize);
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                case HandshakeState.SendingGreeting:
                    switch (action)
                    {
                        case Action.OutCompleted:
                            bytesSent = EndWrite(socketError, bytesTransferred);

                            if (bytesSent == -1)
                            {
                                Error();
                            }
                            else
                            {
                                m_outpos.AdvanceOffset(bytesSent);
                                m_outsize -= bytesSent;

                                if (m_outsize > 0)
                                {
                                    BeginWrite(m_outpos, m_outsize);
                                }
                                else
                                {
                                    m_greetingBytesRead = 0;

                                    var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);

                                    m_handshakeState = HandshakeState.ReceivingGreeting;

                                    BeginRead(greetingSegment, PreambleSize);
                                }
                            }
                            break;
                        case Action.ActivateIn:
                        case Action.ActivateOut:
                            // nothing to do
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                case HandshakeState.ReceivingGreeting:
                    switch (action)
                    {
                        case Action.InCompleted:
                            bytesReceived = EndRead(socketError, bytesTransferred);

                            if (bytesReceived == -1)
                            {
                                Error();
                            }
                            else
                            {
                                m_greetingBytesRead += bytesReceived;

                                // check if it is an unversion protocol
                                if (m_greeting[0] != 0xff || (m_greetingBytesRead == 10 && (m_greeting[9] & 0x01) == 0))
                                {
                                    m_encoder = new V1Encoder(Config.OutBatchSize, m_options.Endian);
                                    m_encoder.SetMsgSource(m_session);

                                    m_decoder = new V1Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_options.Endian);
                                    m_decoder.SetMsgSink(m_session);

                                    //  We have already sent the message header.
                                    //  Since there is no way to tell the encoder to
                                    //  skip the message header, we simply throw that
                                    //  header data away.
                                    int headerSize = m_options.IdentitySize + 1 >= 255 ? 10 : 2;
                                    var tmp = new byte[10];
                                    var bufferp = new ByteArraySegment(tmp);

                                    int bufferSize = headerSize;

                                    m_encoder.GetData(ref bufferp, ref bufferSize);

                                    Debug.Assert(bufferSize == headerSize);

                                    //  Make sure the decoder sees the data we have already received.
                                    m_inpos = new ByteArraySegment(m_greeting);
                                    m_insize = m_greetingBytesRead;

                                    //  To allow for interoperability with peers that do not forward
                                    //  their subscriptions, we inject a phony subscription
                                    //  message into the incoming message stream. To put this
                                    //  message right after the identity message, we temporarily
                                    //  divert the message stream from session to ourselves.
                                    if (m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub)
                                        m_decoder.SetMsgSink(this);

                                    ActivateOut();
                                }
                                else if (m_greetingBytesRead < 10)
                                {
                                    var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);
                                    BeginRead(greetingSegment, PreambleSize - m_greetingBytesRead);
                                }
                                else
                                {
                                    //  The peer is using versioned protocol.
                                    //  Send the rest of the greeting.
                                    m_outpos[m_outsize++] = 1; // Protocol version
                                    m_outpos[m_outsize++] = (byte)m_options.SocketType;

                                    m_handshakeState = HandshakeState.SendingRestOfGreeting;

                                    BeginWrite(m_outpos, m_outsize);
                                }
                            }
                            break;
                        case Action.ActivateIn:
                        case Action.ActivateOut:
                            // nothing to do
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                case HandshakeState.SendingRestOfGreeting:
                    switch (action)
                    {
                        case Action.OutCompleted:
                            bytesSent = EndWrite(socketError, bytesTransferred);

                            if (bytesSent == -1)
                            {
                                Error();
                            }
                            else
                            {
                                m_outpos.AdvanceOffset(bytesSent);
                                m_outsize -= bytesSent;

                                if (m_outsize > 0)
                                {
                                    BeginWrite(m_outpos, m_outsize);
                                }
                                else
                                {
                                    var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);

                                    m_handshakeState = HandshakeState.ReceivingRestOfGreeting;
                                    BeginRead(greetingSegment, GreetingSize - m_greetingBytesRead);
                                }
                            }
                            break;
                        case Action.ActivateIn:
                        case Action.ActivateOut:
                            // nothing to do
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                case HandshakeState.ReceivingRestOfGreeting:
                    switch (action)
                    {
                        case Action.InCompleted:
                            bytesReceived = EndRead(socketError, bytesTransferred);

                            if (bytesReceived == -1)
                            {
                                Error();
                            }
                            else
                            {
                                m_greetingBytesRead += bytesReceived;

                                if (m_greetingBytesRead < GreetingSize)
                                {
                                    var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);
                                    BeginRead(greetingSegment, GreetingSize);
                                }
                                else
                                {
                                    if (m_greeting[VersionPos] == 0)
                                    {
                                        //  ZMTP/1.0 framing.
                                        m_encoder = new V1Encoder(Config.OutBatchSize, m_options.Endian);
                                        m_encoder.SetMsgSource(m_session);

                                        m_decoder = new V1Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_options.Endian);
                                        m_decoder.SetMsgSink(m_session);
                                    }
                                    else
                                    {
                                        //  v1 framing protocol.
                                        m_encoder = new V2Encoder(Config.OutBatchSize, m_session, m_options.Endian);
                                        m_decoder = new V2Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_session, m_options.Endian);
                                    }

                                    // handshake is done
                                    Activate();
                                }
                            }
                            break;
                        case Action.ActivateIn:
                        case Action.ActivateOut:
                            // nothing to do
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }
        }
        private void Handle(Action action, SocketError socketError, int bytesTransferred)
        {
            switch (m_state)
            {
                case State.Closed:
                    switch (action)
                    {
                        case Action.Start:
                            if (m_options.RawSocket)
                            {
                                m_encoder = new RawEncoder(Config.OutBatchSize, m_session, m_options.Endian);
                                m_decoder = new RawDecoder(Config.InBatchSize, m_options.MaxMessageSize, m_session, m_options.Endian);

                                Activate();
                            }
                            else
                            {
                                m_state = State.Handshaking;
                                m_handshakeState = HandshakeState.Closed;
                                HandleHandshake(action, socketError, bytesTransferred);
                            }

                            break;
                    }
                    break;
                case State.Handshaking:
                    HandleHandshake(action, socketError, bytesTransferred);
                    break;
                case State.Active:
                    switch (action)
                    {
                        case Action.InCompleted:
                            m_insize = EndRead(socketError, bytesTransferred);

                            ProcessInput();
                            break;
                        case Action.ActivateIn:

                            // if we stuck let's continue, other than that nothing to do
                            if (m_receivingState == ReceiveState.Stuck)
                            {
                                m_receivingState = ReceiveState.Active;
                                ProcessInput();
                            }
                            break;
                        case Action.OutCompleted:
                            int bytesSent = EndWrite(socketError, bytesTransferred);

                            //  IO error has occurred. We stop waiting for output events.
                            //  The engine is not terminated until we detect input error;
                            //  this is necessary to prevent losing incoming messages.
                            if (bytesSent == -1)
                            {
                                m_sendingState = SendState.Error;
                            }
                            else
                            {
                                m_outpos.AdvanceOffset(bytesSent);
                                m_outsize -= bytesSent;

                                BeginSending();
                            }
                            break;
                        case Action.ActivateOut:
                            // if we idle we start sending, other than do nothing
                            if (m_sendingState == SendState.Idle)
                            {
                                m_sendingState = SendState.Active;
                                BeginSending();
                            }
                            break;
                        default:
                            Debug.Assert(false);
                            break;
                    }
                    break;
                case State.Stalled:
                    switch (action)
                    {
                        case Action.ActivateIn:
                            //  There was an input error but the engine could not
                            //  be terminated (due to the stalled decoder).
                            //  Flush the pending message and terminate the engine now.
                            m_decoder.ProcessBuffer(m_inpos, 0);
                            Debug.Assert(!m_decoder.Stalled());
                            m_session.Flush();
                            Error();
                            break;
                        case Action.ActivateOut:
                            break;
                    }
                    break;
            }
        }
Exemple #5
0
        private void HandleHandshake(Action action, SocketError socketError, int bytesTransferred)
        {
            int bytesSent;
            int bytesReceived;

            switch (m_handshakeState)
            {
            case HandshakeState.Closed:
                switch (action)
                {
                case Action.Start:
                    //  Send the 'length' and 'flags' fields of the identity message.
                    //  The 'length' field is encoded in the long format.

                    m_greetingOutputBuffer[m_outsize++] = ((byte)0xff);
                    m_greetingOutputBuffer.PutLong(m_options.Endian, (long)m_options.IdentitySize + 1, 1);
                    m_outsize += 8;
                    m_greetingOutputBuffer[m_outsize++] = ((byte)0x7f);

                    m_outpos = new ByteArraySegment(m_greetingOutputBuffer);

                    m_handshakeState = HandshakeState.SendingGreeting;

                    BeginWrite(m_outpos, m_outsize);
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            case HandshakeState.SendingGreeting:
                switch (action)
                {
                case Action.OutCompleted:
                    bytesSent = EndWrite(socketError, bytesTransferred);

                    if (bytesSent == -1)
                    {
                        Error();
                    }
                    else
                    {
                        m_outpos.AdvanceOffset(bytesSent);
                        m_outsize -= bytesSent;

                        if (m_outsize > 0)
                        {
                            BeginWrite(m_outpos, m_outsize);
                        }
                        else
                        {
                            m_greetingBytesRead = 0;

                            var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);

                            m_handshakeState = HandshakeState.ReceivingGreeting;

                            BeginRead(greetingSegment, PreambleSize);
                        }
                    }
                    break;

                case Action.ActivateIn:
                case Action.ActivateOut:
                    // nothing to do
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            case HandshakeState.ReceivingGreeting:
                switch (action)
                {
                case Action.InCompleted:
                    bytesReceived = EndRead(socketError, bytesTransferred);

                    if (bytesReceived == -1)
                    {
                        Error();
                    }
                    else
                    {
                        m_greetingBytesRead += bytesReceived;

                        // check if it is an unversion protocol
                        if (m_greeting[0] != 0xff || (m_greetingBytesRead == 10 && (m_greeting[9] & 0x01) == 0))
                        {
                            m_encoder = new V1Encoder(Config.OutBatchSize, m_options.Endian);
                            m_encoder.SetMsgSource(m_session);

                            m_decoder = new V1Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_options.Endian);
                            m_decoder.SetMsgSink(m_session);

                            //  We have already sent the message header.
                            //  Since there is no way to tell the encoder to
                            //  skip the message header, we simply throw that
                            //  header data away.
                            int headerSize = m_options.IdentitySize + 1 >= 255 ? 10 : 2;
                            var tmp        = new byte[10];
                            var bufferp    = new ByteArraySegment(tmp);

                            int bufferSize = headerSize;

                            m_encoder.GetData(ref bufferp, ref bufferSize);

                            Debug.Assert(bufferSize == headerSize);

                            //  Make sure the decoder sees the data we have already received.
                            m_inpos  = new ByteArraySegment(m_greeting);
                            m_insize = m_greetingBytesRead;

                            //  To allow for interoperability with peers that do not forward
                            //  their subscriptions, we inject a phony subscription
                            //  message into the incoming message stream. To put this
                            //  message right after the identity message, we temporarily
                            //  divert the message stream from session to ourselves.
                            if (m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub)
                            {
                                m_decoder.SetMsgSink(this);
                            }

                            ActivateOut();
                        }
                        else if (m_greetingBytesRead < 10)
                        {
                            var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);
                            BeginRead(greetingSegment, PreambleSize - m_greetingBytesRead);
                        }
                        else
                        {
                            //  The peer is using versioned protocol.
                            //  Send the rest of the greeting.
                            m_outpos[m_outsize++] = 1;         // Protocol version
                            m_outpos[m_outsize++] = (byte)m_options.SocketType;

                            m_handshakeState = HandshakeState.SendingRestOfGreeting;

                            BeginWrite(m_outpos, m_outsize);
                        }
                    }
                    break;

                case Action.ActivateIn:
                case Action.ActivateOut:
                    // nothing to do
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            case HandshakeState.SendingRestOfGreeting:
                switch (action)
                {
                case Action.OutCompleted:
                    bytesSent = EndWrite(socketError, bytesTransferred);

                    if (bytesSent == -1)
                    {
                        Error();
                    }
                    else
                    {
                        m_outpos.AdvanceOffset(bytesSent);
                        m_outsize -= bytesSent;

                        if (m_outsize > 0)
                        {
                            BeginWrite(m_outpos, m_outsize);
                        }
                        else
                        {
                            var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);

                            m_handshakeState = HandshakeState.ReceivingRestOfGreeting;
                            BeginRead(greetingSegment, GreetingSize - m_greetingBytesRead);
                        }
                    }
                    break;

                case Action.ActivateIn:
                case Action.ActivateOut:
                    // nothing to do
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            case HandshakeState.ReceivingRestOfGreeting:
                switch (action)
                {
                case Action.InCompleted:
                    bytesReceived = EndRead(socketError, bytesTransferred);

                    if (bytesReceived == -1)
                    {
                        Error();
                    }
                    else
                    {
                        m_greetingBytesRead += bytesReceived;

                        if (m_greetingBytesRead < GreetingSize)
                        {
                            var greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);
                            BeginRead(greetingSegment, GreetingSize);
                        }
                        else
                        {
                            if (m_greeting[VersionPos] == 0)
                            {
                                //  ZMTP/1.0 framing.
                                m_encoder = new V1Encoder(Config.OutBatchSize, m_options.Endian);
                                m_encoder.SetMsgSource(m_session);

                                m_decoder = new V1Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_options.Endian);
                                m_decoder.SetMsgSink(m_session);
                            }
                            else
                            {
                                //  v1 framing protocol.
                                m_encoder = new V2Encoder(Config.OutBatchSize, m_session, m_options.Endian);
                                m_decoder = new V2Decoder(Config.InBatchSize, m_options.MaxMessageSize, m_session, m_options.Endian);
                            }

                            // handshake is done
                            Activate();
                        }
                    }
                    break;

                case Action.ActivateIn:
                case Action.ActivateOut:
                    // nothing to do
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            default:
                Debug.Assert(false);
                break;
            }
        }
Exemple #6
0
        private void Handle(Action action, SocketError socketError, int bytesTransferred)
        {
            switch (m_state)
            {
            case State.Closed:
                switch (action)
                {
                case Action.Start:
                    if (m_options.RawSocket)
                    {
                        m_encoder = new RawEncoder(Config.OutBatchSize, m_session, m_options.Endian);
                        m_decoder = new RawDecoder(Config.InBatchSize, m_options.MaxMessageSize, m_session, m_options.Endian);

                        Activate();
                    }
                    else
                    {
                        m_state          = State.Handshaking;
                        m_handshakeState = HandshakeState.Closed;
                        HandleHandshake(action, socketError, bytesTransferred);
                    }

                    break;
                }
                break;

            case State.Handshaking:
                HandleHandshake(action, socketError, bytesTransferred);
                break;

            case State.Active:
                switch (action)
                {
                case Action.InCompleted:
                    m_insize = EndRead(socketError, bytesTransferred);

                    ProcessInput();
                    break;

                case Action.ActivateIn:

                    // if we stuck let's continue, other than that nothing to do
                    if (m_receivingState == ReceiveState.Stuck)
                    {
                        m_receivingState = ReceiveState.Active;
                        ProcessInput();
                    }
                    break;

                case Action.OutCompleted:
                    int bytesSent = EndWrite(socketError, bytesTransferred);

                    //  IO error has occurred. We stop waiting for output events.
                    //  The engine is not terminated until we detect input error;
                    //  this is necessary to prevent losing incoming messages.
                    if (bytesSent == -1)
                    {
                        m_sendingState = SendState.Error;
                    }
                    else
                    {
                        m_outpos.AdvanceOffset(bytesSent);
                        m_outsize -= bytesSent;

                        BeginSending();
                    }
                    break;

                case Action.ActivateOut:
                    // if we idle we start sending, other than do nothing
                    if (m_sendingState == SendState.Idle)
                    {
                        m_sendingState = SendState.Active;
                        BeginSending();
                    }
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
                break;

            case State.Stalled:
                switch (action)
                {
                case Action.ActivateIn:
                    //  There was an input error but the engine could not
                    //  be terminated (due to the stalled decoder).
                    //  Flush the pending message and terminate the engine now.
                    m_decoder.ProcessBuffer(m_inpos, 0);
                    Debug.Assert(!m_decoder.Stalled());
                    m_session.Flush();
                    Error();
                    break;

                case Action.ActivateOut:
                    break;
                }
                break;
            }
        }