Search in sources :

Example 26 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project jMiniLang by bajdcc.

the class ModuleNetClient method run.

public void run() {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        if (addr.startsWith(":")) {
            addr = "127.0.0.1" + addr;
        }
        URI uri = new URI("http://" + addr);
        if (uri.getPort() < 0)
            throw new URISyntaxException(addr, "invalid address");
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetClientHandler(msgQueue));
            }
        });
        ChannelFuture f = b.connect(uri.getHost(), uri.getPort());
        CHANNEL_GROUP.add(f.channel());
        f = f.sync();
        if (f.isDone()) {
            status = Status.RUNNING;
        }
        this.addr = f.channel().localAddress().toString();
        f.channel().closeFuture().sync();
        status = Status.ERROR;
        error = "Client closed.";
    } catch (Exception e) {
        status = Status.ERROR;
        error = "Error: " + e.getMessage();
        if (error == null)
            error = e.getClass().getSimpleName();
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) URISyntaxException(java.net.URISyntaxException) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 27 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project jMiniLang by bajdcc.

the class ModuleNetServer method run.

public void run() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetServerHandler(msgQueue));
            }
        }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture f = b.bind(port);
        CHANNEL_GROUP.add(f.channel());
        f = f.sync();
        if (f.isDone()) {
            status = Status.RUNNING;
        }
        f.channel().closeFuture().sync();
        status = Status.ERROR;
        error = "Server closed.";
    } catch (Exception e) {
        status = Status.ERROR;
        error = "Error: " + e.getMessage();
        if (error == null)
            error = e.getClass().getSimpleName();
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 28 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project JaPS by JackWhite20.

the class ServerChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    try {
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
    // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(new Connection(jaPSServer, channel));
}
Also used : JSONObjectEncoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectEncoder) Connection(de.jackwhite20.japs.server.network.Connection) JSONObjectDecoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelException(io.netty.channel.ChannelException)

Example 29 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project JaPS by JackWhite20.

the class ClientChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    try {
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
    // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(nioSocketClient);
}
Also used : JSONObjectEncoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectEncoder) JSONObjectDecoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelException(io.netty.channel.ChannelException)

Example 30 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project netty by netty.

the class LengthFieldPrependerTest method testAdjustedLengthLessThanZero.

@Test
public void testAdjustedLengthLessThanZero() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -2));
    try {
        ch.writeOutbound(msg);
        fail(EncoderException.class.getSimpleName() + " must be raised.");
    } catch (EncoderException e) {
    // Expected
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) Test(org.junit.jupiter.api.Test)

Aggregations

LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)34 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)27 SocketChannel (io.netty.channel.socket.SocketChannel)14 ChannelPipeline (io.netty.channel.ChannelPipeline)13 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 Bootstrap (io.netty.bootstrap.Bootstrap)10 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)10 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)9 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)9 StringDecoder (io.netty.handler.codec.string.StringDecoder)9 StringEncoder (io.netty.handler.codec.string.StringEncoder)9 ChannelFuture (io.netty.channel.ChannelFuture)8 Channel (io.netty.channel.Channel)7 EventLoopGroup (io.netty.channel.EventLoopGroup)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 Test (org.junit.jupiter.api.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)4 IOException (java.io.IOException)4 VersionPrepender (org.neo4j.causalclustering.VersionPrepender)4