private void fireWriteTimeOut(final ChannelHandlerContext ctx) { ctx.getPipeline().execute(new Runnable() { public void run() { try { writeTimedOut(ctx); } catch (Throwable t) { fireExceptionCaught(ctx, t); } } }); } }
private void fireReadTimedOut(final ChannelHandlerContext ctx) throws Exception { ctx.getPipeline().execute(new Runnable() { public void run() { try { readTimedOut(ctx); } catch (Throwable t) { fireExceptionCaught(ctx, t); } } }); } }
private void fireChannelIdle( final ChannelHandlerContext ctx, final IdleState state, final long lastActivityTimeMillis) { ctx.getPipeline().execute(new Runnable() { public void run() { try { channelIdle(ctx, state, lastActivityTimeMillis); } catch (Throwable t) { fireExceptionCaught(ctx, t); } } }); }
/** * Sends a {@code "exceptionCaught"} event to the * {@link ChannelUpstreamHandler} which is placed in the closest upstream * from the handler associated with the specified * {@link ChannelHandlerContext} once the io-thread runs again. */ public static ChannelFuture fireExceptionCaughtLater(final ChannelHandlerContext ctx, final Throwable cause) { return ctx.getPipeline().execute(new Runnable() { public void run() { fireExceptionCaught(ctx, cause); } }); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelOpen(ctx, e); serverConnectionHandler = ctx.getPipeline().get(ServerConnectionHandler.class); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelOpen(ctx, e); this.channelHandlerContext = ctx; serverHandler = ctx.getPipeline().get(ServerHandler.class); }
public void afterAdd(ChannelHandlerContext ctx) throws Exception { ChannelPipeline cp = ctx.getPipeline(); if (cp.get(WebSocketServerProtocolHandshakeHandler.class) == null) { // Add the WebSocketHandshakeHandler before this one. ctx.getPipeline().addBefore(ctx.getName(), WebSocketServerProtocolHandshakeHandler.class.getName(), new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols, allowExtensions)); } }
public void operationComplete(ChannelFuture future) throws Exception { logger.info("remove unused channel handlers after authentication is done successfully."); ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName()); ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName()); 0, TimeUnit.MILLISECONDS); ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateHandler.class.getName(), idleStateHandler); ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateAwareChannelHandler.class.getName(), idleStateAwareChannelHandler);
public void beforeAdd(ChannelHandlerContext ctx) throws Exception { if (ctx.getPipeline().isAttached()) { // channelOpen event has been fired already, which means // this.channelOpen() will not be invoked. // We have to initialize here instead. initialize(ctx); } else { // channelOpen event has not been fired yet. // this.channelOpen() will be invoked and initialization will occur there. } }
public void beforeAdd(ChannelHandlerContext ctx) throws Exception { if (ctx.getPipeline().isAttached()) { // channelOpen event has been fired already, which means // this.channelOpen() will not be invoked. // We have to initialize here instead. initialize(ctx); } else { // channelOpen event has not been fired yet. // this.channelOpen() will be invoked and initialization will occur there. } }
/** * Passes the join complete message to the client, and marks the entities joining as successful. * @param channelHandlerContext * @param joinComplete */ private void completeJoin(ChannelHandlerContext channelHandlerContext, NetData.JoinCompleteMessage joinComplete) { logger.info("Join complete received"); server.setClientId(joinComplete.getClientId()); channelHandlerContext.getPipeline().remove(this); channelHandlerContext.getPipeline().get(ClientHandler.class).joinComplete(server); joinStatus.setComplete(); }
/** * Invoked when an exception was raised by an I/O thread or a * {@link ChannelHandler}. */ public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { if (this == ctx.getPipeline().getLast()) { logger.warn( "EXCEPTION, please implement " + getClass().getName() + ".exceptionCaught() for proper handling.", e.getCause()); } ctx.sendUpstream(e); }
/** * Process the handshake verification, checking that both the server and client have attempted it. If successful marks the channel as Authenticated. * @param handshakeVerification * @param ctx Channel Handler Context. */ private void processHandshakeVerification(NetData.HandshakeVerification handshakeVerification, ChannelHandlerContext ctx) { logger.info("Received server verification"); if (serverHello == null || clientHello == null) { logger.error("Received server verification without requesting it: cancelling authentication"); joinStatus.setErrorMessage(AUTHENTICATION_FAILURE); ctx.getChannel().close(); return; } if (!serverCertificate.verify(HandshakeCommon.getSignatureData(serverHello, clientHello), handshakeVerification.getSignature().toByteArray())) { logger.error("Server failed verification: cancelling authentication"); joinStatus.setErrorMessage(AUTHENTICATION_FAILURE); ctx.getChannel().close(); return; } // And we're authenticated. ctx.getPipeline().remove(this); channelAuthenticated(ctx); }
/** * Replace this {@link FrameDecoder} in the {@link ChannelPipeline} with the given {@link ChannelHandler}. All * remaining bytes in the {@link ChannelBuffer} will get send to the new {@link ChannelHandler} that was used * as replacement * */ public void replace(String handlerName, ChannelHandler handler) { if (ctx == null) { throw new IllegalStateException( "Replace cann only be called once the FrameDecoder is added to the ChannelPipeline"); } ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addAfter(ctx.getName(), handlerName, handler); try { if (cumulation != null) { Channels.fireMessageReceived(ctx, cumulation.readBytes(actualReadableBytes())); } } finally { pipeline.remove(this); } }
private void processClientHandshake(NetData.HandshakeHello clientHello, NetData.HandshakeVerification handshakeVerification, ChannelHandlerContext ctx) { logger.info("Received client certificate"); PublicIdentityCertificate clientCert = NetMessageUtil.convert(clientHello.getCertificate()); if (!clientCert.verifySignedBy(config.getSecurity().getServerPublicCertificate())) { logger.error("Received invalid client certificate, ending connection attempt"); ctx.getChannel().close(); return; } byte[] clientSignature = handshakeVerification.getSignature().toByteArray(); byte[] signatureData = HandshakeCommon.getSignatureData(serverHello, clientHello); if (!clientCert.verify(signatureData, clientSignature)) { logger.error("Received invalid verification signature, ending connection attempt"); ctx.getChannel().close(); return; } logger.info("Sending server verification"); byte[] serverSignature = config.getSecurity().getServerPrivateCertificate().sign(signatureData); ctx.getChannel().write(NetData.NetMessage.newBuilder() .setHandshakeVerification(NetData.HandshakeVerification.newBuilder() .setSignature(ByteString.copyFrom(serverSignature))).build()); // Identity has been established, inform the server handler and withdraw from the pipeline ctx.getPipeline().remove(this); serverConnectionHandler.channelAuthenticated(clientCert); }
/** * Add all {@link ChannelHandler}'s that are needed for HTTP. */ protected void addHttpHandlers(ChannelHandlerContext ctx) { ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder()); pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder()); pipeline.addLast("httpChunkAggregator", new HttpChunkAggregator(maxHttpContentLength)); pipeline.addLast("httpRequestHandler", createHttpRequestHandlerForHttp()); }
private void receivedConnect(NetData.JoinMessage message) { logger.info("Received Start Join"); NetClient client = new NetClient(channelHandlerContext.getChannel(), networkSystem, identity); client.setPreferredName(message.getName()); client.setColor(new Color(message.getColor().getRgba())); client.setViewDistanceMode(ViewDistance.forIndex(message.getViewDistanceLevel())); channelHandlerContext.getPipeline().remove(this); serverHandler.connectionComplete(client); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, State state) throws Exception { switch (state) { case CHECK_PROTOCOL_VERSION: { version = SocksMessage.ProtocolVersion.fromByte(buffer.readByte()); if (version != SocksMessage.ProtocolVersion.SOCKS5) { break; } checkpoint(State.READ_PREFFERED_AUTH_TYPE); } case READ_PREFFERED_AUTH_TYPE: { authScheme = SocksMessage.AuthScheme.fromByte(buffer.readByte()); msg = new SocksInitResponse(authScheme); break; } } ctx.getPipeline().remove(this); return msg; }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, State state) throws Exception { switch (state) { case CHECK_PROTOCOL_VERSION: { version = SocksMessage.SubnegotiationVersion.fromByte(buffer.readByte()); if (version != SocksMessage.SubnegotiationVersion.AUTH_PASSWORD) { break; } checkpoint(State.READ_AUTH_RESPONSE); } case READ_AUTH_RESPONSE: { authStatus = SocksMessage.AuthStatus.fromByte(buffer.readByte()); msg = new SocksAuthResponse(authStatus); } } ctx.getPipeline().remove(this); return msg; }
/** * Add all {@link ChannelHandler}'s that are needed for SPDY with the given version. */ protected void addSpdyHandlers(ChannelHandlerContext ctx, SpdyVersion version) { ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addLast("spdyFrameCodec", new SpdyFrameCodec(version)); pipeline.addLast("spdySessionHandler", new SpdySessionHandler(version, true)); pipeline.addLast("spdyHttpEncoder", new SpdyHttpEncoder(version)); pipeline.addLast("spdyHttpDecoder", new SpdyHttpDecoder(version, maxSpdyContentLength)); pipeline.addLast("spdyStreamIdHandler", new SpdyHttpResponseStreamIdHandler()); pipeline.addLast("httpRequestHandler", createHttpRequestHandlerForSpdy()); }