Ejemplo n.º 1
0
        public override void ChannelRead(ChannelHandlerContext ctx, object msg)
        {
            try
            {
                if (!(msg is ByteBuf))
                {
                    // we know it is HTTP as we only have HTTP (for Websocket) and TCP handlers installed.
                    _log.warn("Unsupported connection type: 'HTTP'. Bolt protocol only operates over a TCP connection or WebSocket.");
                    ctx.close();
                    return;
                }
                ByteBuf buf = ( ByteBuf )msg;

                AssertEncryptedIfRequired();

                // try to fill out handshake buffer
                _handshakeBuffer.writeBytes(buf, Math.Min(buf.readableBytes(), _handshakeBuffer.writableBytes()));

                // we filled up the handshake buffer
                if (_handshakeBuffer.writableBytes() == 0)
                {
                    if (VerifyBoltPreamble())
                    {
                        // let's handshake
                        if (PerformHandshake())
                        {
                            // announce selected protocol to the client
                            ctx.writeAndFlush(ctx.alloc().buffer(4).writeInt((int)_protocol.version()));

                            // install related protocol handlers into the pipeline
                            _protocol.install();
                            ctx.pipeline().remove(this);

                            // if we somehow end up with more data in the incoming buffers, let's send them
                            // down to the pipeline for the chosen protocol handlers to handle whatever they
                            // are.
                            if (buf.readableBytes() > 0)
                            {
                                ctx.fireChannelRead(buf.readRetainedSlice(buf.readableBytes()));
                            }
                        }
                        else
                        {
                            ctx.writeAndFlush(ctx.alloc().buffer().writeBytes(new sbyte[] { 0, 0, 0, 0 })).addListener(ChannelFutureListener.CLOSE);
                        }
                    }
                    else
                    {
                        ctx.close();
                    }
                }
            }
            finally
            {
                ReferenceCountUtil.release(msg);
            }
        }