Example usage for io.netty.channel ChannelFuture isSuccess

List of usage examples for io.netty.channel ChannelFuture isSuccess

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture isSuccess.

Prototype

boolean isSuccess();

Source Link

Document

Returns true if and only if the I/O operation was completed successfully.

Usage

From source file:me.bigteddy98.mcproxy.protocol.handlers.ClientSideHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    incomingChannel = ctx.channel();/*ww  w . jav  a 2  s  .c o m*/
    networkManager.clientsidePipeline = ctx.pipeline();

    Bootstrap bootstrab = new Bootstrap();
    bootstrab.group(incomingChannel.eventLoop());
    bootstrab.channel(ctx.channel().getClass());
    bootstrab.handler(serverboundConnectionInitializer = new ServerboundConnectionInitializer(networkManager,
            incomingChannel));
    bootstrab.option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = bootstrab.connect(hostname, port);

    outgoingChannel = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                incomingChannel.read();
            } else {
                incomingChannel.close();
            }
        }
    });
}

From source file:me.bigteddy98.mcproxy.protocol.handlers.ClientSideHandler.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    if (outgoingChannel.isActive()) {
        ByteBuf bufferOriginal = (ByteBuf) msg;
        ByteBuf bufferClone = Unpooled.copiedBuffer(bufferOriginal);
        final List<Packet> packets = this.networkManager.handleServerBoundPackets((ByteBuf) msg, bufferClone);
        bufferClone.release();//  w ww . j a v a 2 s.com
        outgoingChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                for (Packet packet : packets) {
                    packet.onSend(networkManager);
                }
                if (future.isSuccess()) {
                    ctx.channel().read();
                } else {
                    future.channel().close();
                }
            }
        });
    }
}

From source file:me.bigteddy98.mcproxy.protocol.handlers.ServerSideHandler.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf bufferOriginal = (ByteBuf) msg;
    ByteBuf bufferClone = Unpooled.copiedBuffer(bufferOriginal);
    final List<Packet> packets = this.networkManager.handleClientBoundPackets((ByteBuf) msg, bufferClone);
    bufferClone.release();//from  ww  w.  jav a2 s  . c  o m

    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            for (Packet packet : packets) {
                packet.onSend(networkManager);
            }
            if (future.isSuccess()) {
                ctx.channel().read();
            } else {
                future.channel().close();
            }
        }
    });
}

From source file:me.bigteddy98.movingmotd.ClientSideConnection.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    this.networkManager.incomingChannel = ctx.channel();
    this.networkManager.clientsidePipeline = ctx.pipeline();

    Bootstrap bootstrab = new Bootstrap();
    bootstrab.group(networkManager.incomingChannel.eventLoop());
    bootstrab.channel(ctx.channel().getClass());
    bootstrab.handler(new ServerSideConnectionInitialization(networkManager));
    bootstrab.option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = bootstrab.connect(this.toHostname, this.toPort);
    f.addListener(new ChannelFutureListener() {
        @Override//ww w .ja v  a2  s  .com
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                networkManager.incomingChannel.read();
            } else {
                networkManager.incomingChannel.close();
            }
        }
    });
    this.outgoingChannel = f.channel();
}

From source file:me.bigteddy98.movingmotd.ClientSideConnection.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf clonedBuf = Unpooled.copiedBuffer((ByteBuf) msg);
    ByteBuf originalBuf = (ByteBuf) msg;

    int packetSize = PacketUtils.readVarInt(originalBuf);
    if (originalBuf.readableBytes() < packetSize) {
        System.out.println("Packet was smaller than the lenght.");
    }//from   w w w  .j  a  va2 s . c o m
    int id = PacketUtils.readVarInt(originalBuf);
    if (this.stage == Stage.HANDSHAKE) {
        if (id != 0x00) {
            System.out.println("Handshake ID was not equal to 0x00.");
        }
        // followed by varint, string, unsigned short and another varint
        PacketUtils.readVarInt(originalBuf);
        PacketUtils.readString(originalBuf);
        originalBuf.readUnsignedShort();
        int nextStage = PacketUtils.readVarInt(originalBuf);
        this.stage = Stage.fromId(nextStage);
    } else if (this.stage == Stage.LOGIN) {
        // just sent it
    } else if (this.stage == Stage.STATUS) {
        // TODO someone is pinging!

        originalBuf.release();
        return;
    }
    originalBuf.release();
    this.outgoingChannel.writeAndFlush(clonedBuf).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ctx.channel().read();
            } else {
                future.channel().close();
            }
        }
    });
}

From source file:me.bigteddy98.movingmotd.ServerSideConnection.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    this.incomingChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override//from   ww w. ja  va2s .co  m
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ctx.channel().read();
            } else {
                future.channel().close();
            }
        }
    });
}

From source file:me.bigteddy98.slimeportal.protocol.handlers.ClientSideHandler.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    if (outgoingChannel.isActive()) {
        ByteBuf bufferClone = Unpooled.copiedBuffer((ByteBuf) msg);
        final List<Packet> packets = this.networkManager.handleServerBoundPackets(bufferClone);
        bufferClone.release();/*from w w  w.j a v a  2  s .  c  o  m*/
        if (!packets.isEmpty()) {
            for (final Packet packet : packets) {
                PacketReceiveEvent event = new PacketReceiveEvent();
                packet.onReceive(this.networkManager, event);
                if (event.isCancelled()) {
                    ctx.channel().read();
                    continue;
                }

                PacketDataWrapper excludingSize = new PacketDataWrapper(Unpooled.buffer());
                excludingSize.writeVarInt(packet.getId());
                packet.write(excludingSize);

                final PacketDataWrapper includingSize = new PacketDataWrapper(Unpooled.buffer());
                includingSize.writeVarInt(excludingSize.readableBytes());
                includingSize.writeBytes(excludingSize.getBuffer());
                excludingSize.getBuffer().release();

                outgoingChannel.writeAndFlush(includingSize.getBuffer())
                        .addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                packet.onSend(networkManager);
                                if (future.isSuccess()) {
                                    ctx.channel().read();
                                } else {
                                    future.channel().close();
                                }
                            }
                        });
            }
            ((ByteBuf) msg).release();
        } else {
            outgoingChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        ctx.channel().read();
                    } else {
                        future.channel().close();
                    }
                }
            });
        }
    }
}

From source file:me.bigteddy98.slimeportal.protocol.handlers.ServerSideHandler.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf bufferClone = Unpooled.copiedBuffer((ByteBuf) msg);
    final List<Packet> packets = this.networkManager.handleClientBoundPackets(bufferClone);
    bufferClone.release();//w  w  w. j  a v a 2s .  c  o  m

    if (!packets.isEmpty()) {
        for (final Packet packet : packets) {
            PacketReceiveEvent event = new PacketReceiveEvent();
            packet.onReceive(this.networkManager, event);
            if (event.isCancelled()) {
                ctx.channel().read();
                continue;
            }

            PacketDataWrapper excludingSize = new PacketDataWrapper(Unpooled.buffer());
            excludingSize.writeVarInt(packet.getId());
            packet.write(excludingSize);

            final PacketDataWrapper includingSize = new PacketDataWrapper(Unpooled.buffer());
            includingSize.writeVarInt(excludingSize.readableBytes());
            includingSize.writeBytes(excludingSize.getBuffer());

            excludingSize.getBuffer().release();
            inboundChannel.writeAndFlush(includingSize.getBuffer()).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    packet.onSend(networkManager);
                    if (future.isSuccess()) {
                        ctx.channel().read();
                    } else {
                        future.channel().close();
                    }
                }
            });
        }
        ((ByteBuf) msg).release();
    } else {
        inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    ctx.channel().read();
                } else {
                    future.channel().close();
                }
            }
        });
    }
}

From source file:me.binf.socks5.client.proxy.HexDumpProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new HexDumpProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = b.connect(remoteHost, remotePort);

    outboundChannel = f.channel();//from   ww w .  ja v  a  2  s . c  om
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                proxyService.noticeView("?" + remoteHost + ":" + remotePort + "?!");
                inboundChannel.read();
            } else {
                proxyService.noticeView("?" + remoteHost + ":" + remotePort + "!");
                inboundChannel.close();
            }
        }
    });
}

From source file:me.ferrybig.javacoding.teamspeakconnector.TeamspeakApi.java

License:Open Source License

public Future<TeamspeakConnection> connect(SocketAddress addr) {
    Promise<TeamspeakConnection> prom = this.group.next().newPromise();
    ChannelFuture channel = openChannel(addr, new TeamspeakConnectionInitizer(prom, 20000), 20000);
    channel.addListener(future -> {//from   w w w.j av a  2 s.  c  o  m
        if (!channel.isSuccess()) {
            prom.setFailure(new TeamspeakException("Connection failed", channel.cause()));
        }
    });
    return prom;
}