List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:com.emin.igwmp.skm.core.netty.SocksServer.java
License:Apache License
/** * ?socket?//www .j ava 2s . c o m * */ public void startServer() { mServerBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(mSocksServerInitializer); try { // mServerBootstrap.bind(PORT).sync().channel().closeFuture().sync(); mChannelFuture = mServerBootstrap.bind(PORT).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { final EventLoop loop = future.channel().eventLoop(); loop.schedule(new Runnable() { @Override public void run() { startServer(); } }, 1L, TimeUnit.SECONDS); } } }).sync(); mChannelFuture.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); stopServer(); } }
From source file:com.farsunset.cim.sdk.android.CIMConnectorManager.java
License:Apache License
public void connect(final String host, final int port) { if (!isNetworkConnected(context)) { Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED); intent.putExtra(Exception.class.getName(), NetworkDisabledException.class.getSimpleName()); context.sendBroadcast(intent);/*from www . j a v a2 s . c om*/ return; } if (isConnected() || !semaphore.tryAcquire()) { return; } executor.execute(new Runnable() { @Override public void run() { final InetSocketAddress remoteAddress = new InetSocketAddress(host, port); bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) { semaphore.release(); future.removeListener(this); if (!future.isSuccess() && future.cause() != null) { handleConnectFailure(future.cause(), remoteAddress); } if (future.isSuccess()) { channel = future.channel(); } } }); } }); }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
public void connect(final String host, final int port) { if (isConnected() || !semaphore.tryAcquire()) { return;/* www . java2 s . c o m*/ } executor.execute(new Runnable() { @Override public void run() { logger.info("****************CIM? " + host + ":" + port + "......"); final InetSocketAddress remoteAddress = new InetSocketAddress(host, port); bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { semaphore.release(); future.removeListener(this); if (!future.isSuccess() && future.cause() != null) { handleConnectFailure(future.cause(), remoteAddress); } if (future.isSuccess()) { channel = future.channel(); } } }); } }); }
From source file:com.flysoloing.learning.network.netty.proxy.HexDumpProxyBackendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) { inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read();/*from w ww. j a v a 2 s . c o m*/ } else { future.channel().close(); } } }); }
From source file:com.flysoloing.learning.network.netty.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 w w w. ja v a 2 s .c om f.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // connection complete start to read first data inboundChannel.read(); } else { // Close the connection if the connection attempt has failed. inboundChannel.close(); } } }); }
From source file:com.flysoloing.learning.network.netty.proxy.HexDumpProxyFrontendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) { if (outboundChannel.isActive()) { outboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // was able to flush out data, start to read the next chunk ctx.channel().read(); } else { future.channel().close(); }//from www. ja v a 2s .c o m } }); } }
From source file:com.flysoloing.learning.network.netty.redis.RedisClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w . ja va 2 s . c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new RedisDecoder()); p.addLast(new RedisBulkStringAggregator()); p.addLast(new RedisArrayAggregator()); p.addLast(new RedisEncoder()); p.addLast(new RedisClientHandler()); } }); // Start the connection attempt. Channel ch = b.connect(HOST, PORT).sync().channel(); // Read commands from the stdin. System.out.println("Enter Redis commands (quit to end)"); ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (;;) { final String input = in.readLine(); final String line = input != null ? input.trim() : null; if (line == null || "quit".equalsIgnoreCase(line)) { // EOF or "quit" ch.close().sync(); break; } else if (line.isEmpty()) { // skip `enter` or `enter` with spaces. continue; } // Sends the received line to the server. lastWriteFuture = ch.writeAndFlush(line); lastWriteFuture.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { System.err.print("write failed: "); future.cause().printStackTrace(System.err); } } }); } // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.sync(); } } finally { group.shutdownGracefully(); } }
From source file:com.friz.lobby.network.events.listeners.LobbyInitEventListener.java
License:Open Source License
@Override public void onEvent(LobbyInitRequestEvent event, LobbySessionContext context) { if (event.getType() == 14) { context.write(new LobbyInitResponseEvent(0)).addListener((ChannelFuture future) -> { if (future.isSuccess()) { context.getChannel().pipeline().remove(LobbyInitDecoder.class.getName()); context.getChannel().pipeline().remove(LobbyInitEncoder.class.getName()); context.getChannel().pipeline().addFirst(LoginDecoder.class.getName(), new LoginDecoder()); context.getChannel().pipeline().addFirst(LoginEncoder.class.getName(), new LoginEncoder()); }//from w w w.j a v a 2 s.c om }); } else if (event.getType() == 28) { context.getChannel().pipeline().remove(LobbyInitDecoder.class.getName()); context.getChannel().pipeline().remove(LobbyInitEncoder.class.getName()); context.getChannel().pipeline().addFirst(CreationDecoder.class.getName(), new CreationDecoder()); context.getChannel().pipeline().addFirst(CreationEncoder.class.getName(), new CreationEncoder()); } else if (event.getType() == 29) { context.write(new LobbyInitResponseEvent(0)).addListener((ChannelFuture future) -> { if (future.isSuccess()) { context.getChannel().pipeline().remove(LobbyInitDecoder.class.getName()); context.getChannel().pipeline().remove(LobbyInitEncoder.class.getName()); context.getChannel().pipeline().addFirst(SocialInitDecoder.class.getName(), new SocialInitDecoder()); context.getChannel().pipeline().addFirst(SocialInitEncoder.class.getName(), new SocialInitEncoder()); context.getChannel().pipeline().addFirst(SocialSeedEncoder.class.getName(), new SocialSeedEncoder()); } }); } }
From source file:com.friz.lobby.network.events.listeners.SocialInitEventListener.java
License:Open Source License
@Override public void onEvent(SocialInitRequestEvent event, LobbySessionContext context) { context.write(new SocialInitResponseEvent(event.getType(), context.getServer().getHashForChannel(context.getChannel()), event.getKeys())); context.write(new SocialSeedResponseEvent(event.getKeys())).addListener((ChannelFuture future) -> { if (future.isSuccess()) { context.getChannel().pipeline().remove(SocialInitDecoder.class.getName()); context.getChannel().pipeline().remove(SocialInitEncoder.class.getName()); context.getChannel().pipeline().remove(SocialSeedEncoder.class.getName()); context.getChannel().pipeline().addFirst(SocialDecoder.class.getName(), new SocialDecoder(event.getKeys())); context.getChannel().pipeline().addFirst(LoginEncoder.class.getName(), new LoginEncoder()); }//from w w w. j a v a2 s .co m }); }
From source file:com.friz.owari.network.server.listener.HandshakeListener.java
License:Open Source License
@Override public void onEvent(HandshakeEvent event, SessionContext<Server> context) { if (event.getStatus() == 72) { context.write(new HandshakeEvent(27)).addListener((ChannelFuture f) -> { if (f.isSuccess()) { context.getPipeline().remove(HandshakeEncoder.class); context.getPipeline().remove(HandshakeDecoder.class); context.getPipeline().addFirst(ExchangeDecoder.class.getName(), new ExchangeDecoder()); context.getPipeline().addFirst(ExchangeEncoder.class.getName(), new ExchangeEncoder()); context.write(new ExchangeTransferEvent(context.getBind().getPublicKey())); }/*ww w. j ava2 s .c o m*/ }); } else { context.close(); } }