@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelOpen(ctx, e); serverConnectionHandler = ctx.getPipeline().get(ServerConnectionHandler.class); }
/** * Upgrades the connection and send the handshake response. */ protected ChannelFuture writeHandshakeResponse( Channel channel, HttpResponse res, ChannelHandler encoder, ChannelHandler decoder) { final ChannelPipeline p = channel.getPipeline(); if (p.get(HttpChunkAggregator.class) != null) { p.remove(HttpChunkAggregator.class); } final String httpEncoderName = p.getContext(HttpResponseEncoder.class).getName(); p.addAfter(httpEncoderName, "wsencoder", encoder); p.get(HttpRequestDecoder.class).replace("wsdecoder", decoder); final ChannelFuture future = channel.write(res); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { p.remove(httpEncoderName); } }); return future; }
/** * Invoked when an exception was raised by an I/O thread or a * {@link ChannelHandler}. */ public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { ChannelPipeline pipeline = ctx.getPipeline(); ChannelHandler last = pipeline.getLast(); if (!(last instanceof ChannelUpstreamHandler) && ctx instanceof DefaultChannelPipeline) { // The names comes in the order of which they are insert when using DefaultChannelPipeline List<String> names = ctx.getPipeline().getNames(); for (int i = names.size() - 1; i >= 0; i--) { ChannelHandler handler = ctx.getPipeline().get(names.get(i)); if (handler instanceof ChannelUpstreamHandler) { // find the last handler last = handler; break; } } } if (this == last) { logger.warn( "EXCEPTION, please implement " + getClass().getName() + ".exceptionCaught() for proper handling.", e.getCause()); } ctx.sendUpstream(e); }
public ServerImpl(NetworkSystemImpl system, Channel channel) { this.channel = channel; metricsSource = (NetMetricSource) channel.getPipeline().get(MetricRecordingHandler.NAME); this.networkSystem = system; this.time = (EngineTime) CoreRegistry.get(Time.class); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { super.channelOpen(ctx, e); this.channelHandlerContext = ctx; serverHandler = ctx.getPipeline().get(ServerHandler.class); }
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) { String protocol = "ws"; if (cp.get(SslHandler.class) != null) { // SSL in use so use Secure WebSockets protocol = "wss"; } return protocol + "://" + req.headers().get(HttpHeaders.Names.HOST) + path; }
public Future<ServerInfoMessage> requestInfo(final String address, final int port) { return pool.submit(() -> { InetSocketAddress remoteAddress = new InetSocketAddress(address, port); ChannelFuture connectCheck = bootstrap.connect(remoteAddress); connectCheck.syncUninterruptibly(); Channel channel = connectCheck.getChannel(); channel.getCloseFuture().syncUninterruptibly(); ServerInfoRequestHandler handler = channel.getPipeline().get(ServerInfoRequestHandler.class); ServerInfoMessage serverInfo = handler.getServerInfo(); return serverInfo; }); }
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)); } }
/** * 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(); }
/** * Sets up a new net client with metrics, time, identity, and a world provider. * @param channel * @param networkSystem * @param identity Publice certificate for the client. */ public NetClient(Channel channel, NetworkSystemImpl networkSystem, PublicIdentityCertificate identity) { this.channel = channel; metricSource = (NetMetricSource) channel.getPipeline().get(MetricRecordingHandler.NAME); this.networkSystem = networkSystem; this.time = CoreRegistry.get(Time.class); this.identity = identity; this.biomeManager = CoreRegistry.get(BiomeManager.class); WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class); if (worldProvider != null) { worldProvider.registerListener(this); } }
TimeoutHandler timeoutHandler = (TimeoutHandler)pipeline.get(TIMEOUT_HANDLER); timeoutHandler.setEnabledTimeout(false);
if (ch.getPipeline().get(SslHandler.class) == null) { boolean canEvictAfterTransfer = true; if (!shouldAlwaysEvictOsCache) {
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { // Get the SslHandler from the ChannelPipeline so we can obtain the SslEngine from it. SslHandler handler = ctx.getPipeline().get(SslHandler.class); if (handler == null) { // SslHandler is needed by SPDY by design. throw new IllegalStateException("SslHandler is needed for SPDY"); } ChannelPipeline pipeline = ctx.getPipeline(); SelectedProtocol protocol = getProtocol(handler.getEngine()); switch (protocol) { case None: // Not done with choosing the protocol, so just return here for now, return; case SpdyVersion3_1: addSpdyHandlers(ctx, SpdyVersion.SPDY_3_1); break; case HttpVersion1_0: case HttpVersion1_1: addHttpHandlers(ctx); break; default: throw new IllegalStateException("Unknown SelectedProtocol"); } // When we reached here we can remove this handler as its now clear what protocol we want to use // from this point on. pipeline.remove(this); ctx.sendUpstream(e); }
public static boolean isSslHandlerConfigured(ChannelPipeline pipeline) { return pipeline.get(SSL_HANDLER) != null; }
public static SslHandler getSslHandler(ChannelPipeline pipeline) { return (SslHandler) pipeline.get(SSL_HANDLER); }
} else { allChannels.add(connectCheck.getChannel()); ClientConnectionHandler connectionHandler = connectCheck.getChannel().getPipeline().get(ClientConnectionHandler.class); if (connectionHandler == null) { JoinStatusImpl status = new JoinStatusImpl();
@Override public void onContentAdded() { channel.getPipeline().get(ChunkedWriteHandler.class).resumeTransfer(); } });
public boolean offer(Channel channel, Object partitionKey) { if (isClosed.get() || (!sslConnectionPoolEnabled && channel.getPipeline().get(SslHandler.class) != null)) return false; long now = millisTime(); if (isTTLExpired(channel, now)) return false; boolean added = partitions.computeIfAbsent(partitionKey, PARTITION_COMPUTER).add(new IdleChannel(channel, now)); if (added) channelId2Creation.putIfAbsent(channel.getId(), new ChannelCreation(now, partitionKey)); return added; }
private void onFutureSuccess(final Channel channel) throws ConnectException { SslHandler sslHandler = channel.getPipeline().get(SslHandler.class); if (sslHandler != null) { sslHandler.handshake().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture handshakeFuture) throws Exception { if (handshakeFuture.isSuccess()) { final AsyncHandler<T> asyncHandler = future.getAsyncHandler(); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onSslHandshakeCompleted(); writeRequest(channel); } else { onFutureFailure(channel, handshakeFuture.getCause()); } } }); } else { writeRequest(channel); } }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }