Exemplo n.º 1
0
        /// <summary>
        ///     This returns a batch of binary data. The data
        ///     are filled to a supplied buffer. If no buffer is supplied (data_
        ///     points to NULL) decoder object will provide buffer of its own.
        /// </summary>
        public void GetData(ref ByteArraySegment data, ref int size)
        {
            int offset = -1;

            this.GetData(ref data, ref size, ref offset);
        }
Exemplo n.º 2
0
        public void GetData(ref ByteArraySegment data, ref int size, ref int offset)
        {
            ByteArraySegment buffer = data ?? new ByteArraySegment(this.m_buffer);
            int bufferSize          = data == null ? this.m_bufferSize : size;

            int pos = 0;

            while (pos < bufferSize)
            {
                // If there are no more data to return, run the state machine.
                // If there are still no data, return what we already have
                // in the buffer.
                if (this.m_toWrite == 0)
                {
                    // If we are to encode the beginning of a new message,
                    // adjust the message offset.

                    if (this.m_beginning)
                    {
                        if (offset == -1)
                        {
                            offset = pos;
                        }
                    }

                    if (!this.Next())
                    {
                        break;
                    }
                }

                // If there are no data in the buffer yet and we are able to
                // fill whole buffer in a single go, let's use zero-copy.
                // There's no disadvantage to it as we cannot stuck multiple
                // messages into the buffer anyway. Note that subsequent
                // write(s) are non-blocking, thus each single write writes
                // at most SO_SNDBUF bytes at once not depending on how large
                // is the chunk returned from here.
                // As a consequence, large messages being sent won't block
                // other engines running in the same I/O thread for excessive
                // amounts of time.
                if (pos == 0 && data == null && this.m_toWrite >= bufferSize)
                {
                    data = this.m_writePos;
                    size = this.m_toWrite;

                    this.m_writePos = null;
                    this.m_toWrite  = 0;
                    return;
                }

                // Copy data to the buffer. If the buffer is full, return.
                int toCopy = Math.Min(this.m_toWrite, bufferSize - pos);

                if (toCopy != 0)
                {
                    this.m_writePos.CopyTo(0, buffer, pos, toCopy);
                    pos += toCopy;
                    this.m_writePos.AdvanceOffset(toCopy);
                    this.m_toWrite -= toCopy;
                }
            }

            data = buffer;
            size = pos;
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Write the bytes of this ByteArraySegment to the specified destination-ByteArraySegment.
 /// </summary>
 /// <param name="fromOffset">an offset within this source buffer to start copying from</param>
 /// <param name="dest">the destination-ByteArraySegment</param>
 /// <param name="destOffset">an offset within the destination buffer to start copying to</param>
 /// <param name="toCopy">the number of bytes to copy</param>
 public void CopyTo(int fromOffset, [NotNull] ByteArraySegment dest, int destOffset, int toCopy)
 {
     Buffer.BlockCopy(this.m_innerBuffer, this.Offset + fromOffset, dest.m_innerBuffer, dest.Offset + destOffset, toCopy);
 }
Exemplo n.º 4
0
 /// <summary>
 ///     Create a new ByteArraySegment that is a shallow-copy of the given ByteArraySegment (with a reference to the same
 ///     buffer)
 ///     but with a different offset.
 /// </summary>
 /// <param name="otherSegment">the source-ByteArraySegment to make a copy of</param>
 /// <param name="offset">a value for the Offset property that is distinct from that of the source ByteArraySegment</param>
 public ByteArraySegment([NotNull] ByteArraySegment otherSegment, int offset)
 {
     this.m_innerBuffer = otherSegment.m_innerBuffer;
     this.Offset        = otherSegment.Offset + offset;
 }
Exemplo n.º 5
0
 /// <summary>
 ///     Write the bytes of this ByteArraySegment to the specified destination-ByteArraySegment.
 /// </summary>
 /// <param name="otherSegment">the destination-ByteArraySegment</param>
 /// <param name="toCopy">the number of bytes to copy</param>
 public void CopyTo([NotNull] ByteArraySegment otherSegment, int toCopy)
 {
     this.CopyTo(0, otherSegment, 0, toCopy);
 }
Exemplo n.º 6
0
        private void ProcessInput()
        {
            bool disconnection = false;
            int  processed;

            if (this.m_insize == -1)
            {
                this.m_insize = 0;
                disconnection = true;
            }

            if (this.m_options.RawSocket)
            {
                if (this.m_insize == 0 || !this.m_decoder.MessageReadySize(this.m_insize))
                {
                    processed = 0;
                }
                else
                {
                    processed = this.m_decoder.ProcessBuffer(this.m_inpos, this.m_insize);
                }
            }
            else
            {
                // Push the data to the decoder.
                processed = this.m_decoder.ProcessBuffer(this.m_inpos, this.m_insize);
            }

            if (processed == -1)
            {
                disconnection = true;
            }
            else
            {
                // Stop polling for input if we got stuck.
                if (processed < this.m_insize)
                {
                    this.m_receivingState = ReceiveState.Stuck;

                    this.m_inpos.AdvanceOffset(processed);
                    this.m_insize -= processed;
                }
                else
                {
                    this.m_inpos  = null;
                    this.m_insize = 0;
                }
            }

            // Flush all messages the decoder may have produced.
            this.m_session.Flush();

            // An input error has occurred. If the last decoded message
            // has already been accepted, we terminate the engine immediately.
            // Otherwise, we stop waiting for socket events and postpone
            // the termination until after the message is accepted.
            if (disconnection)
            {
                if (this.m_decoder.Stalled())
                {
                    this.m_ioObject.RemoveSocket(this.m_handle);
                    this.m_ioEnabled = false;
                    this.m_state     = State.Stalled;
                }
                else
                {
                    this.Error();
                }
            }
            else if (this.m_receivingState != ReceiveState.Stuck)
            {
                this.m_decoder.GetBuffer(out this.m_inpos, out this.m_insize);
                this.BeginRead(this.m_inpos, this.m_insize);
            }
        }
Exemplo n.º 7
0
        private void HandleHandshake(Action action, SocketError socketError, int bytesTransferred)
        {
            int bytesSent;
            int bytesReceived;

            switch (this.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.

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

                    this.m_outpos = new ByteArraySegment(this.m_greetingOutputBuffer);

                    this.m_handshakeState = HandshakeState.SendingGreeting;

                    this.BeginWrite(this.m_outpos, this.m_outsize);
                    break;

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

                break;

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

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

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

                            ByteArraySegment greetingSegment = new ByteArraySegment(this.m_greeting, this.m_greetingBytesRead);

                            this.m_handshakeState = HandshakeState.ReceivingGreeting;

                            this.BeginRead(greetingSegment, StreamEngine.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 = StreamEngine.EndRead(socketError, bytesTransferred);

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

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

                            this.m_decoder = new V1Decoder(Config.InBatchSize, this.m_options.MaxMessageSize, this.m_options.Endian);
                            this.m_decoder.SetMsgSink(this.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 = this.m_options.IdentitySize + 1 >= 255 ? 10 : 2;
                            byte[]           tmp        = new byte[10];
                            ByteArraySegment bufferp    = new ByteArraySegment(tmp);

                            int bufferSize = headerSize;

                            this.m_encoder.GetData(ref bufferp, ref bufferSize);

                            Debug.Assert(bufferSize == headerSize);

                            // Make sure the decoder sees the data we have already received.
                            this.m_inpos  = new ByteArraySegment(this.m_greeting);
                            this.m_insize = this.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 (this.m_options.SocketType == ZmqSocketType.Pub || this.m_options.SocketType == ZmqSocketType.Xpub)
                            {
                                this.m_decoder.SetMsgSink(this);
                            }

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

                            this.m_handshakeState = HandshakeState.SendingRestOfGreeting;

                            this.BeginWrite(this.m_outpos, this.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 = StreamEngine.EndWrite(socketError, bytesTransferred);

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

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

                            this.m_handshakeState = HandshakeState.ReceivingRestOfGreeting;
                            this.BeginRead(greetingSegment, StreamEngine.GreetingSize - this.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 = StreamEngine.EndRead(socketError, bytesTransferred);

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

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

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

                            // handshake is done
                            this.Activate();
                        }
                    }

                    break;

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

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

                break;

            default:
                Debug.Assert(false);
                break;
            }
        }
Exemplo n.º 8
0
 protected void NextStep(ByteArraySegment readPos, int toRead, int state)
 {
     this.m_readPos = readPos;
     this.m_toRead  = toRead;
     this.State     = state;
 }
Exemplo n.º 9
0
        /// <summary>
        ///     Processes the data in the buffer previously allocated using
        ///     get_buffer function. size argument specifies the number of bytes
        ///     actually filled into the buffer. Function returns number of
        ///     bytes actually processed.
        /// </summary>
        public int ProcessBuffer(ByteArraySegment data, int size)
        {
            // Check if we had an error in previous attempt.
            if (this.State < 0)
            {
                return(-1);
            }

            // In case of zero-copy simply adjust the pointers, no copying
            // is required. Also, run the state machine in case all the data
            // were processed.
            if (data != null && data.Equals(this.m_readPos))
            {
                this.m_readPos.AdvanceOffset(size);
                this.m_toRead -= size;

                while (this.m_toRead == 0)
                {
                    if (!this.Next())
                    {
                        if (this.State < 0)
                        {
                            return(-1);
                        }

                        return(size);
                    }
                }

                return(size);
            }

            int pos = 0;

            while (true)
            {
                // Try to get more space in the message to fill in.
                // If none is available, return.
                while (this.m_toRead == 0)
                {
                    if (!this.Next())
                    {
                        if (this.State < 0)
                        {
                            return(-1);
                        }

                        return(pos);
                    }
                }

                // If there are no more data in the buffer, return.
                if (pos == size)
                {
                    return(pos);
                }

                // Copy the data from buffer to the message.
                int toCopy = Math.Min(this.m_toRead, size - pos);
                data.CopyTo(pos, this.m_readPos, 0, toCopy);
                this.m_readPos.AdvanceOffset(toCopy);
                pos           += toCopy;
                this.m_toRead -= toCopy;
            }
        }