Search in sources :

Example 1 with ProtocolHeader

use of org.apache.qpid.server.protocol.v0_10.transport.ProtocolHeader in project qpid-broker-j by apache.

the class ServerInputHandler method received.

public void received(QpidByteBuffer buf) {
    int position = buf.position();
    List<ServerFrame> frames = new ArrayList<>();
    while (buf.hasRemaining() && _state != ERROR) {
        buf.mark();
        switch(_state) {
            case PROTO_HDR:
                if (buf.remaining() < 8) {
                    break;
                }
                if (buf.get() != 'A' || buf.get() != 'M' || buf.get() != 'Q' || buf.get() != 'P') {
                    buf.reset();
                    error("bad protocol header: %s", str(buf));
                    _state = ERROR;
                } else {
                    byte protoClass = buf.get();
                    byte instance = buf.get();
                    byte major = buf.get();
                    byte minor = buf.get();
                    _serverAssembler.init(new ProtocolHeader(protoClass, instance, major, minor));
                    _state = FRAME_HDR;
                }
                break;
            case FRAME_HDR:
                if (buf.remaining() < ServerFrame.HEADER_SIZE) {
                    buf.reset();
                } else {
                    flags = buf.get();
                    type = SegmentType.get(buf.get());
                    int size = (0xFFFF & buf.getShort());
                    size -= ServerFrame.HEADER_SIZE;
                    if (size < 0 || size > (_maxFrameSize - ServerFrame.HEADER_SIZE)) {
                        error("bad frame size: %d", size);
                        _state = ERROR;
                    } else {
                        // skip unused byte
                        buf.get();
                        byte b = buf.get();
                        if ((b & 0xF0) != 0) {
                            error("non-zero reserved bits in upper nibble of " + "frame header byte 5: '%x'", b);
                            _state = ERROR;
                        } else {
                            track = (byte) (b & 0xF);
                            channel = (0xFFFF & buf.getShort());
                            buf.position(buf.position() + 4);
                            if (size == 0) {
                                ServerFrame frame = new ServerFrame(flags, type, track, channel, EMPTY_BYTE_BUFFER.duplicate());
                                frames.add(frame);
                            } else if (buf.remaining() < size) {
                                buf.reset();
                            } else {
                                final QpidByteBuffer body = buf.slice();
                                body.limit(size);
                                ServerFrame frame = new ServerFrame(flags, type, track, channel, body);
                                frames.add(frame);
                                buf.position(buf.position() + size);
                            }
                        }
                    }
                }
                break;
            default:
                throw new IllegalStateException();
        }
        int newPosition = buf.position();
        if (position == newPosition) {
            break;
        } else {
            position = newPosition;
        }
    }
    _serverAssembler.received(frames);
}
Also used : ArrayList(java.util.ArrayList) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) ProtocolHeader(org.apache.qpid.server.protocol.v0_10.transport.ProtocolHeader)

Example 2 with ProtocolHeader

use of org.apache.qpid.server.protocol.v0_10.transport.ProtocolHeader in project qpid-broker-j by apache.

the class FrameDecoder method next.

private State next(int pos) {
    input.order(ByteOrder.BIG_ENDIAN);
    switch(_state) {
        case PROTO_HDR:
            if (input.get(pos) != 'A' && input.get(pos + 1) != 'M' && input.get(pos + 2) != 'Q' && input.get(pos + 3) != 'P') {
                error("bad protocol header: %s", str(input));
                return State.ERROR;
            }
            byte protoClass = input.get(pos + 4);
            byte instance = input.get(pos + 5);
            byte major = input.get(pos + 6);
            byte minor = input.get(pos + 7);
            _assembler.received(new ProtocolHeader(protoClass, instance, major, minor));
            _needed = Frame.HEADER_SIZE;
            return State.FRAME_HDR;
        case FRAME_HDR:
            _flags = input.get(pos);
            _type = SegmentType.get(input.get(pos + 1));
            int size = (0xFFFF & input.getShort(pos + 2));
            size -= Frame.HEADER_SIZE;
            _maxFrameSize = 64 * 1024;
            if (size < 0 || size > (_maxFrameSize - 12)) {
                error("bad frame size: %d", size);
                return State.ERROR;
            }
            byte b = input.get(pos + 5);
            if ((b & 0xF0) != 0) {
                error("non-zero reserved bits in upper nibble of " + "frame header byte 5: '%x'", b);
                return State.ERROR;
            } else {
                _track = (byte) (b & 0xF);
            }
            _channel = (0xFFFF & input.getShort(pos + 6));
            if (size == 0) {
                Frame frame = new Frame(_flags, _type, _track, _channel, EMPTY_BYTE_BUFFER);
                _assembler.received(frame);
                _needed = Frame.HEADER_SIZE;
                return State.FRAME_HDR;
            } else {
                _needed = size;
                return State.FRAME_BODY;
            }
        case FRAME_BODY:
            Frame frame = new Frame(_flags, _type, _track, _channel, input.slice());
            _assembler.received(frame);
            _needed = Frame.HEADER_SIZE;
            return State.FRAME_HDR;
        default:
            throw new IllegalStateException();
    }
}
Also used : Frame(org.apache.qpid.server.protocol.v0_10.transport.Frame) ProtocolHeader(org.apache.qpid.server.protocol.v0_10.transport.ProtocolHeader)

Aggregations

ProtocolHeader (org.apache.qpid.server.protocol.v0_10.transport.ProtocolHeader)2 ArrayList (java.util.ArrayList)1 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)1 Frame (org.apache.qpid.server.protocol.v0_10.transport.Frame)1