List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:club.lovety.xy.netty.test.UptimeClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { if (startTime < 0) { startTime = System.currentTimeMillis(); }/*from w w w .jav a2 s. com*/ println("Connected to: " + ctx.channel().remoteAddress()); }
From source file:club.lovety.xy.netty.test.UptimeClientHandler.java
License:Apache License
@Override public void channelInactive(final ChannelHandlerContext ctx) { println("Disconnected from: " + ctx.channel().remoteAddress()); }
From source file:club.lovety.xy.netty.test.UptimeClientHandler.java
License:Apache License
@Override public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception { println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's'); final EventLoop loop = ctx.channel().eventLoop(); loop.schedule(new Runnable() { @Override/*from w w w .j a v a 2s . c o m*/ public void run() { println("Reconnecting to: " + UptimeClient.HOST + ':' + UptimeClient.PORT); UptimeClient.connect(UptimeClient.configureBootstrap(new Bootstrap(), loop)); } }, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS); }
From source file:cn.david.socks.DirectClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { ctx.pipeline().remove(this); promise.setSuccess(ctx.channel()); }
From source file:cn.david.socks.SocksServerConnectHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception { Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new GenericFutureListener<Future<Channel>>() { @Override/*from w ww .jav a 2s .c om*/ public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType())) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel() .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.host(), request.port()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel() .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); }
From source file:cn.david.socks.SocksServerConnectHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { SocksServerUtils.closeOnFlush(ctx.channel()); }
From source file:cn.david.socks.SocksServerHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) { throwable.printStackTrace();//w w w. j a va2s .co m SocksServerUtils.closeOnFlush(ctx.channel()); }
From source file:cn.easyplay.proxy.WebSocketClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), (FullHttpResponse) msg); LOGGER.debug("WebSocket Client connected!"); handshakeFuture.setSuccess();//from w ww . j a v a 2 s . c o m return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; // frame telnetChannel.writeAndFlush(textFrame.text()); LOGGER.debug("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof PongWebSocketFrame) { LOGGER.debug("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { LOGGER.debug("WebSocket Client received closing"); ctx.close(); telnetChannel.close(); } }
From source file:cn.ifengkou.hestia.client.MessageSendHandler.java
License:Apache License
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { super.channelRegistered(ctx); this.channel = ctx.channel(); }
From source file:cn.jpush.api.common.connection.HttpResponseHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception { Integer streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()); if (streamId == null) { System.err.println("HttpResponseHandler unexpected message received: " + msg); return;//www . ja v a2s.c om } else { LOG.debug("HttpResponseHandler response message received: " + msg); } Entry<ChannelFuture, ChannelPromise> entry = streamidPromiseMap.get(streamId); List<String> list = new ArrayList<String>(); list.add(msg.status().code() + ""); if (entry == null) { System.err.println("Message received for unknown stream id " + streamId); } else { // Do stuff with the message (for now just print it) ByteBuf byteBuf = msg.content(); if (byteBuf.isReadable()) { int contentLength = byteBuf.readableBytes(); byte[] arr = new byte[contentLength]; byteBuf.readBytes(arr); String content = new String(arr, 0, contentLength, CharsetUtil.UTF_8); list.add(content); System.out.println("Protocol version: " + msg.protocolVersion()); System.out.println("status: " + list.get(0) + " response content: " + list.get(1)); LOG.debug("Protocol version: " + msg.protocolVersion()); LOG.debug("status: " + list.get(0) + " response content: " + list.get(1)); } mNettyHttp2Client.setResponse(streamId + ctx.channel().id().asShortText(), list); if (null != mCallback) { ResponseWrapper wrapper = new ResponseWrapper(); wrapper.responseCode = Integer.valueOf(list.get(0)); if (list.size() > 1) { wrapper.responseContent = list.get(1); } mCallback.onSucceed(wrapper); } entry.getValue().setSuccess(); if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; System.err.println("content: " + content.content().toString(CharsetUtil.UTF_8)); System.err.flush(); if (content instanceof LastHttpContent) { System.err.println(" end of content"); // ctx.close(); } } } }