List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.eucalyptus.ws.IoHandlers.java
License:Open Source License
public static Map<String, ChannelHandler> channelMonitors(final TimeUnit unit, final long timeout) { final Map<String, ChannelHandler> monitors = Maps.newHashMap(); monitors.put("idlehandler", new IdleStateHandler(0L, 0L, timeout, unit)); monitors.put("idlecloser", new ChannelInboundHandlerAdapter() { @Override//w ww. j av a 2 s. c o m public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.ALL_IDLE) { ctx.channel().close(); } } } }); monitors.putAll(extraMonitors); return monitors; }
From source file:com.example.iimp_znxj_new2014.netty.ServerHandler.java
License:Apache License
private void loginResult(ChannelHandlerContext ctx, String msg) throws IOException { System.err.println("?client?" + msg); UserInfo userInfo = JsonUtils.decode(msg, UserInfo.class); msg = "??:" + userInfo.getUsername() + " ?:" + userInfo.getPassword() + "----" + ctx.channel().remoteAddress() + "----" + msg; Log.d("tag", msg); System.out.println(ctx.channel().id()); ctx.writeAndFlush("1\n"); //ctx.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n'); }
From source file:com.facebook.nifty.core.NettyThriftDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { out.add(getTransport(ctx.channel(), in)); }
From source file:com.facebook.nifty.core.NiftyDispatcher.java
License:Apache License
private void writeResponse(ChannelHandlerContext ctx, TNiftyTransport messageTransport, int responseSequenceId) { // Ensure responses to requests are written in the same order the requests // were received. synchronized (responseMap) { ByteBuf response = messageTransport.getOutputBuffer(); ThriftTransportType transportType = messageTransport.getTransportType(); int currentResponseId = lastResponseWrittenId.get() + 1; if (responseSequenceId != currentResponseId) { // This response is NOT next in line of ordered responses, save it to // be sent later, after responses to all earlier requests have been // sent. responseMap.put(responseSequenceId, response); } else {//from w w w. java 2 s.c om // This response was next in line, write this response now, and see if // there are others next in line that should be sent now as well. do { if (response.isReadable()) { response = addFraming(ctx, response, transportType); //ctx.nextOutboundByteBuffer().writeBytes(response); ctx.channel().write(response); //response.release(); } lastResponseWrittenId.incrementAndGet(); ++currentResponseId; response = responseMap.remove(currentResponseId); } while (null != response); ctx.channel().flush(); // Now that we've written some responses, check if reads should be unblocked if (isChannelReadBlocked(ctx)) { int lastRequestSequenceId = dispatcherSequenceId.get(); if (lastRequestSequenceId <= lastResponseWrittenId.get() + queuedResponseLimit) { unblockChannelReads(ctx); } } } } }
From source file:com.facebook.nifty.core.NiftyDispatcher.java
License:Apache License
private void closeChannel(ChannelHandlerContext ctx) { if (ctx.channel().isOpen()) { ctx.channel().close(); } }
From source file:com.facebook.nifty.core.NiftyDispatcher.java
License:Apache License
private void blockChannelReads(ChannelHandlerContext ctx) { // Remember that reads are blocked (there is no Channel.getReadable()) ctx.attr(READ_BLOCKED_STATE_KEY).set(ReadBlockedState.BLOCKED); // NOTE: this shuts down reads, but isn't a 100% guarantee we won't get any more messages. // It sets up the channel so that the polling loop will not report any new read events // and netty won't read any more data from the socket, but any messages already fully read // from the socket before this ran may still be decoded and arrive at this handler. Thus // the limit on queued messages before we block reads is more of a guidance than a hard // limit./* w w w. j av a 2 s . c o m*/ ctx.channel().config().setAutoRead(false); }
From source file:com.facebook.nifty.core.NiftyDispatcher.java
License:Apache License
private void unblockChannelReads(ChannelHandlerContext ctx) { // Remember that reads are unblocked (there is no Channel.getReadable()) ctx.attr(READ_BLOCKED_STATE_KEY).set(ReadBlockedState.NOT_BLOCKED); ctx.channel().config().setAutoRead(true); }
From source file:com.facebook.nifty.core.ThriftFrameDecoder.java
License:Apache License
private TTransport tryDecodeFramedMessage(ChannelHandlerContext ctx, ByteBuf buffer) { // Framed messages are prefixed by the size of the frame (which doesn't include the // framing itself). int messageStartReaderIndex = buffer.readerIndex(); // Read the i32 frame contents size int messageContentsLength = buffer.getInt(messageStartReaderIndex); // The full message is larger by the size of the frame size prefix int messageLength = messageContentsLength + MESSAGE_FRAME_SIZE; if (messageContentsLength > maxFrameSize) { ctx.fireExceptionCaught(//from w ww. j av a 2 s . c o m new TooLongFrameException("Maximum frame size of " + maxFrameSize + " exceeded")); } int messageContentsOffset = messageStartReaderIndex + MESSAGE_FRAME_SIZE; if (messageLength == 0) { // Zero-sized frame: just ignore it and return nothing buffer.readerIndex(messageContentsOffset); return null; } else if (buffer.readableBytes() < messageLength) { // Full message isn't available yet, return nothing for now return null; } else { // Full message is available, return it ByteBuf messageBuffer = extractFrame(buffer, messageContentsOffset, messageContentsLength); ThriftMessage message = new ThriftMessage(messageBuffer, ThriftTransportType.FRAMED); buffer.readerIndex(messageStartReaderIndex + messageLength); return new TNiftyTransport(ctx.channel(), message); } }
From source file:com.facebook.nifty.core.ThriftFrameDecoder.java
License:Apache License
private TTransport tryDecodeUnframedMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws TException { // Perform a trial decode, skipping through // the fields, to see whether we have an entire message available. int messageLength = 0; int messageStartReaderIndex = buffer.readerIndex(); try {// www.j ava 2s .c o m TNiftyTransport decodeAttemptTransport = new TNiftyTransport(ctx.channel(), buffer); TProtocol inputProtocol = this.inputProtocolFactory.getProtocol(decodeAttemptTransport); // Skip through the message inputProtocol.readMessageBegin(); TProtocolUtil.skip(inputProtocol, TType.STRUCT); inputProtocol.readMessageEnd(); messageLength = buffer.readerIndex() - messageStartReaderIndex; } catch (IndexOutOfBoundsException e) { // No complete message was decoded: ran out of bytes return null; } finally { if (buffer.readerIndex() - messageStartReaderIndex > maxFrameSize) { ctx.fireExceptionCaught( new TooLongFrameException("Maximum frame size of " + maxFrameSize + " exceeded")); } buffer.readerIndex(messageStartReaderIndex); } if (messageLength <= 0) { return null; } // We have a full message in the read buffer, slice it off ByteBuf messageBuffer = extractFrame(buffer, messageStartReaderIndex, messageLength); ThriftMessage message = new ThriftMessage(messageBuffer, ThriftTransportType.UNFRAMED); buffer.readerIndex(messageStartReaderIndex + messageLength); return new TNiftyTransport(ctx.channel(), message); }
From source file:com.fanavard.challenge.client.websocket.WebSocketClientHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, Object msg) { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess();//ww w .j av a2s .c om 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; System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }