Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:dorkbox.network.connection.registration.local.RegistrationLocalHandlerClient.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) throws Exception {
    // the "server" bounces back the registration message when it's valid.
    ReferenceCountUtil.release(message);

    Channel channel = context.channel();
    MetaChannel metaChannel = channel.attr(META_CHANNEL).getAndSet(null);

    // have to setup new listeners
    if (metaChannel != null) {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.remove(this);

        // Event though a local channel is XOR with everything else, we still have to make the client clean up it's state.
        registrationWrapper.startNextProtocolRegistration();

        ConnectionImpl connection = registrationWrapper.connection0(metaChannel, null);

        // have to setup connection handler
        pipeline.addLast(CONNECTION_HANDLER, connection);
        registrationWrapper.connectionConnected0(connection);
    } else {/*from   w  w w. ja v a 2s. c o  m*/
        // this should NEVER happen!
        logger.error("Error registering LOCAL channel! MetaChannel is null!");
        shutdown(channel, 0);
    }
}

From source file:dorkbox.network.connection.registration.local.RegistrationLocalHandlerServer.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) throws Exception {
    Channel channel = context.channel();
    ChannelPipeline pipeline = channel.pipeline();

    if (!(message instanceof Registration)) {
        logger.error("Expected registration message was [{}] instead!", message.getClass());
        shutdown(channel, 0);/* w  w w  . j  av  a  2  s  .  c om*/
        ReferenceCountUtil.release(message);
        return;
    }

    MetaChannel metaChannel = channel.attr(META_CHANNEL).get();

    if (metaChannel == null) {
        logger.error("Server MetaChannel was null. It shouldn't be.");
        shutdown(channel, 0);
        ReferenceCountUtil.release(message);
        return;
    }

    Registration registration = (Registration) message;

    // verify the class ID registration details.
    // the client will send their class registration data. VERIFY IT IS CORRECT!
    STATE state = registrationWrapper.verifyClassRegistration(metaChannel, registration);
    if (state == STATE.ERROR) {
        // abort! There was an error
        shutdown(channel, 0);
        return;
    } else if (state == STATE.WAIT) {
        return;
    }
    // else, continue.

    // have to remove the pipeline FIRST, since if we don't, and we expect to receive a message --- when we REMOVE "this" from the pipeline,
    // we will ALSO REMOVE all it's messages, which we want to receive!
    pipeline.remove(this);

    registration.payload = null;

    // we no longer need the meta channel, so remove it
    channel.attr(META_CHANNEL).set(null);
    channel.writeAndFlush(registration);

    ReferenceCountUtil.release(registration);
    logger.trace("Sent registration");

    ConnectionImpl connection = registrationWrapper.connection0(metaChannel, null);

    if (connection != null) {
        // have to setup connection handler
        pipeline.addLast(CONNECTION_HANDLER, connection);
        registrationWrapper.connectionConnected0(connection);
    }
}

From source file:fixio.handlers.FixApplicationAdapter.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
    try {/* w w w  .j  a v  a  2 s .  co  m*/
        if (msg instanceof FixMessage) {
            onMessage(ctx, (FixMessage) msg, out);
        } else if (msg instanceof LogonEvent) {
            onLogon(ctx, (LogonEvent) msg);
        } else if (msg instanceof LogoutEvent) {
            onLogout(ctx, (LogoutEvent) msg);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:gedi.oml.remote.Pipeline.Receiver.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof RemotePipelineData)) {
        ctx.fireChannelRead(msg);/*w w  w . j  a  va 2 s.c  o m*/
        return;
    }

    RemotePipelineData data = (RemotePipelineData) msg;

    boolean release = true;
    try {

        synchronized (location) {
            if (data.getId().equals(id))
                answer.offer(data);
            else {
                release = false;
                ctx.fireChannelRead(msg);
            }

        }

    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
    }
}

From source file:gedi.oml.remote.Pipeline.ReceiverGroup.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    RemotePipelineData data = (RemotePipelineData) msg;

    boolean release = false;
    try {//w  w  w.j  a va2  s  .co  m
        synchronized (location) {
            boolean answerCorrect = data.getData().getReference().equals(location.getReference())
                    && data.getData().getRegion().equals(location.getRegion());
            if (answerCorrect)
                ctx.fireChannelRead(msg);
            else
                release = true;
            // noone is interested in this answer anymore (i.e. the corresponding thread has been interrupted already)
        }
    } finally {
        if (release)
            ReferenceCountUtil.release(msg);
    }

}

From source file:gedi.oml.remote.TracksServer.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    boolean release = false;

    try {/* w ww .  j  a va  2 s.  c  o  m*/

        if (msg instanceof String) {
            if (((String) msg).startsWith("E:")) {
                for (Sender s : idToSender.values())
                    s.setDisabled(true);
                for (String disabled : StringUtils.split(((String) msg).substring(2), ','))
                    idToSender.get(disabled).setDisabled(false);

            } else {

                // pipeline oml
                Pipeline pipeline = new OmlNodeExecutor().execute(new OmlReader().parse((String) msg));
                log.log(Level.INFO, pipeline.getPetriNet().getTransitions().size() + " transitions found!");
                dataManager = new TracksDataManager(pipeline.getPetriNet());
                for (Sender sender : pipeline.getObjects(Sender.class)) {
                    sender.setChannelHandlerContext(ctx);
                    idToSender.put(sender.getInput(), sender);
                }
                release = true;
            }
        } else if (msg instanceof RemotePipelineData) {
            RemotePipelineData data = (RemotePipelineData) msg;
            dataManager.setLocation((PixelBasepairMapper) data.getData().getData(),
                    data.getData().getReference(), data.getData().getRegion(), (x) -> {
                    });
            release = true;
        } else {
            ctx.fireChannelRead(msg);
            release = false;
        }

    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
    }
}

From source file:herddb.network.netty.DataMessageDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;/*ww w. j  a  va  2s . c  o m*/
    try {
        ctx.fireChannelRead(MessageUtils.decodeMessage(in));
    } finally {
        ReferenceCountUtil.release(msg);
    }

}

From source file:herddb.proto.Pdu.java

License:Apache License

@Override
public void close() {
    ReferenceCountUtil.release(buffer);
    buffer = null;
    messageId = 0;
    handle.recycle(this);
}

From source file:io.advantageous.conekt.http.impl.ClientConnection.java

License:Open Source License

NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, client.getSslHelper(), true, metrics,
            metric);/* w  w  w.  j  av  a 2 s .  c  om*/
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.remove("codec");
    pipeline.replace("handler", "handler", new ConektNetHandler(connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            client.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            client.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });
    return socket;
}

From source file:io.advantageous.conekt.http.impl.ServerConnection.java

License:Open Source License

NetSocket createNetSocket() {
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, server.getSslHelper(), false, metrics,
            metric);//from  w w  w.j a  va2s. co  m
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }

    channel.pipeline().replace("handler", "handler", new ConektNetHandler(connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });

    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
        channel.pipeline().remove("httpEncoder");
    } else {
        lastWriteFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                channel.pipeline().remove("httpEncoder");
            }
        });
    }
    return socket;
}