List of usage examples for io.netty.channel ChannelHandlerContext fireExceptionCaught
@Override ChannelHandlerContext fireExceptionCaught(Throwable cause);
From source file:ws.wamp.jawampa.transport.netty.WampServerWebsocketHandler.java
License:Apache License
private void tryWebsocketHandshake(final ChannelHandlerContext ctx, FullHttpRequest request) { String wsLocation = getWebSocketLocation(ctx, request); String subProtocols = WampSerialization.makeWebsocketSubprotocolList(supportedSerializations); WebSocketServerHandshaker handshaker = new WebSocketServerHandshakerFactory(wsLocation, subProtocols, false, WampHandlerConfiguration.MAX_WEBSOCKET_FRAME_SIZE).newHandshaker(request); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else {/*from ww w.jav a2 s . co m*/ handshakeInProgress = true; // The next statement will throw if the handshake gets wrong. This will lead to an // exception in the channel which will close the channel (which is OK). final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), request); String actualProtocol = handshaker.selectedSubprotocol(); serialization = WampSerialization.fromString(actualProtocol); // In case of unsupported websocket subprotocols we close the connection. // Won't help us when the client will ignore our protocol response and send // invalid packets anyway if (serialization == WampSerialization.Invalid) { handshakeFuture.addListener(ChannelFutureListener.CLOSE); return; } // Remove all handlers after this one - we don't need them anymore since we switch to WAMP ChannelHandler last = ctx.pipeline().last(); while (last != null && last != this) { ctx.pipeline().removeLast(); last = ctx.pipeline().last(); } if (last == null) { throw new IllegalStateException("Can't find the WAMP server handler in the pipeline"); } // Remove the WampServerWebSocketHandler and replace it with the protocol handler // which processes pings and closes ProtocolHandler protocolHandler = new ProtocolHandler(); ctx.pipeline().replace(this, "wamp-websocket-protocol-handler", protocolHandler); final ChannelHandlerContext protocolHandlerCtx = ctx.pipeline().context(protocolHandler); // Handle websocket fragmentation before the deserializer protocolHandlerCtx.pipeline() .addLast(new WebSocketFrameAggregator(WampHandlerConfiguration.MAX_WEBSOCKET_FRAME_SIZE)); // Install the serializer and deserializer protocolHandlerCtx.pipeline().addLast("wamp-serializer", new WampSerializationHandler(serialization)); protocolHandlerCtx.pipeline().addLast("wamp-deserializer", new WampDeserializationHandler(serialization)); // Retrieve a listener for this new connection final IWampConnectionListener connectionListener = connectionAcceptor.createNewConnectionListener(); // Create a Wamp connection interface on top of that final WampServerConnection connection = new WampServerConnection(serialization); ChannelHandler routerHandler = new SimpleChannelInboundHandler<WampMessage>() { @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // Gets called once the channel gets added to the pipeline connection.ctx = ctx; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connectionAcceptor.acceptNewConnection(connection, connectionListener); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { connectionListener.transportClosed(); } @Override protected void channelRead0(ChannelHandlerContext ctx, WampMessage msg) throws Exception { connectionListener.messageReceived(msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); connectionListener.transportError(cause); } }; // Install the router in the pipeline protocolHandlerCtx.pipeline().addLast("wamp-router", routerHandler); handshakeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { // The handshake was not successful. // Close the channel without registering ctx.fireExceptionCaught(future.cause()); // TODO: This is a race condition if the router did not yet accept the connection } else { // We successfully sent out the handshake // Notify the router of that fact ctx.fireChannelActive(); } } }); // TODO: Maybe there are frames incoming before the handshakeFuture is resolved // This might lead to frames getting sent to the router before it is activated } }
From source file:ws.wamp.jawampa.transport.WampServerWebsocketHandler.java
License:Apache License
private void tryWebsocketHandshake(final ChannelHandlerContext ctx, FullHttpRequest request) { String wsLocation = getWebSocketLocation(ctx, request); WebSocketServerHandshaker handshaker = new WebSocketServerHandshakerFactory(wsLocation, WampHandlerConfiguration.WAMP_WEBSOCKET_PROTOCOLS, false, WampHandlerConfiguration.MAX_WEBSOCKET_FRAME_SIZE).newHandshaker(request); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else {//from w w w. ja v a 2 s.co m handshakeInProgress = true; final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), request); String actualProtocol = handshaker.selectedSubprotocol(); if (actualProtocol != null && actualProtocol.equals("wamp.2.json")) { serialization = Serialization.Json; } // else if (actualProtocol.equals("wamp.2.msgpack")) { // serialization = Serialization.MessagePack; // } // In case of unsupported websocket subprotocols we close the connection. // Won't help us when the client will ignore our protocol response and send // invalid packets anyway if (serialization == Serialization.Invalid) { handshakeFuture.addListener(ChannelFutureListener.CLOSE); return; } // Remove all handlers after this one - we don't need them anymore since we switch to WAMP ChannelHandler last = ctx.pipeline().last(); while (last != null && last != this) { ctx.pipeline().removeLast(); last = ctx.pipeline().last(); } if (last == null) { throw new IllegalStateException("Can't find the WAMP server handler in the pipeline"); } // Remove the WampServerWebSocketHandler and replace it with the protocol handler // which processes pings and closes ProtocolHandler protocolHandler = new ProtocolHandler(); ctx.pipeline().replace(this, "wamp-websocket-protocol-handler", protocolHandler); final ChannelHandlerContext protocolHandlerCtx = ctx.pipeline().context(protocolHandler); // Handle websocket fragmentation before the deserializer protocolHandlerCtx.pipeline() .addLast(new WebSocketFrameAggregator(WampHandlerConfiguration.MAX_WEBSOCKET_FRAME_SIZE)); // Install the serializer and deserializer protocolHandlerCtx.pipeline().addLast("wamp-serializer", new WampSerializationHandler(serialization, router.objectMapper())); protocolHandlerCtx.pipeline().addLast("wamp-deserializer", new WampDeserializationHandler(serialization, router.objectMapper())); // Install the router in the pipeline protocolHandlerCtx.pipeline().addLast(router.eventLoop(), "wamp-router", router.createRouterHandler()); handshakeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } else { // We successfully sent out the handshake // Notify the activation to everything new ctx.fireChannelActive(); } } }); } }