Refine search
in.skipBytes(actualReadableBytes()); return; if (!in.isReadable()) { return; byte b = in.readByte(); frameFinalFlag = (b & 0x80) != 0; frameRsv = (b & 0x70) >> 4; payloadBuffer = readBytes(ctx.alloc(), in, toFrameLength(framePayloadLength)); receivedClosingHandshake = true; checkCloseFrameBody(ctx, payloadBuffer); out.add(new CloseWebSocketFrame(frameFinalFlag, frameRsv, payloadBuffer)); payloadBuffer = null; return;
public void handle(final Object msg) { ready(); if (msg instanceof TextWebSocketFrame) { onTextCallback.accept(((TextWebSocketFrame) msg).text()); } else if (msg instanceof BinaryWebSocketFrame) { onBinaryCallback.accept(((BinaryWebSocketFrame) msg).content().nioBuffer()); } else if (msg instanceof CloseWebSocketFrame) { CloseWebSocketFrame closeFrame = ((CloseWebSocketFrame) msg).retain(); int statusCode = closeFrame.statusCode(); onCloseCallback.accept(statusCode == -1 ? WebSocket.NORMAL.code() : statusCode, Optional.ofNullable(closeFrame.reasonText())); handshaker.close(ctx.channel(), closeFrame).addListener(CLOSE); } else if (msg instanceof Throwable) { onErrorCallback.accept((Throwable) msg); } }
/** * Returns the reason text as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a> If a reason * text is not supplied, an empty string is returned. */ public String reasonText() { ByteBuf binaryData = content(); if (binaryData == null || binaryData.capacity() <= 2) { return ""; } binaryData.readerIndex(2); String reasonText = binaryData.toString(CharsetUtil.UTF_8); binaryData.readerIndex(0); return reasonText; }
@Override public CloseWebSocketFrame replace(ByteBuf content) { return new CloseWebSocketFrame(isFinalFragment(), rsv(), content); }
private void handleCloseFrame(ChannelHandlerContext ctx, CloseWebSocketFrame cwsf) { if (closed.compareAndSet(false, true)) { CloseReason cr = new CloseReason(cwsf.statusCode(), cwsf.reasonText()); handleCloseReason(ctx, cr); } }
@Override protected void decode(final ChannelHandlerContext channelHandlerContext, final CloseWebSocketFrame frame, final List<Object> objects) throws Exception { final ByteBuf messageBytes = frame.content(); final byte len = messageBytes.readByte(); if (len <= 0) { objects.add(RequestMessage.INVALID); return; } final ByteBuf contentTypeBytes = channelHandlerContext.alloc().buffer(len); try { messageBytes.readBytes(contentTypeBytes); final String contentType = contentTypeBytes.toString(UTF8); final MessageSerializer serializer = select(contentType, ServerSerializers.DEFAULT_SERIALIZER); // it's important to re-initialize these channel attributes as they apply globally to the channel. in // other words, the next request to this channel might not come with the same configuration and mixed // state can carry through from one request to the next channelHandlerContext.channel().attr(StateKey.SESSION).set(null); channelHandlerContext.channel().attr(StateKey.SERIALIZER).set(serializer); channelHandlerContext.channel().attr(StateKey.USE_BINARY).set(true); try { objects.add(serializer.deserializeRequest(messageBytes.discardReadBytes())); } catch (SerializationException se) { objects.add(RequestMessage.INVALID); } } finally { contentTypeBytes.release(); } }
WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof CloseWebSocketFrame) { this.handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content().retain())); return; ctx.channel(), new CloseWebSocketFrame(1003, String.format( "%s frame types not supported", frame.getClass() .getName())));
if (msg instanceof FullHttpResponse) { started = true; channel().pipeline() .remove(HttpObjectAggregator.class); FullHttpResponse response = (FullHttpResponse) msg; .release(); channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content())); ctx.read(); return; ((CloseWebSocketFrame)msg).isFinalFragment()) { if (log.isDebugEnabled()) { log.debug(format(channel(), "CloseWebSocketFrame detected. Closing Websocket")); sendCloseNow(new CloseWebSocketFrame(true, close.rsv(), close.content()));
private void protocolViolation(ChannelHandlerContext ctx, CorruptedFrameException ex) { state = State.CORRUPT; if (ctx.channel().isActive()) { Object closeMessage; if (receivedClosingHandshake) { closeMessage = Unpooled.EMPTY_BUFFER; } else { closeMessage = new CloseWebSocketFrame(1002, null); } ctx.writeAndFlush(closeMessage).addListener(ChannelFutureListener.CLOSE); } throw ex; }
Channel channel = ctx.channel(); Single.fromPublisher(finalPublisher).subscribeOn(Schedulers.from(channel.eventLoop())).subscribe((BiConsumer<MutableHttpResponse<?>, Throwable>) (actualResponse, throwable) -> { if (throwable != null) { ctx.fireExceptionCaught(throwable); } else if (actualResponse == proceed) { ChannelPipeline pipeline = ctx.pipeline(); LOG.error("Error opening WebSocket: " + e.getMessage(), e); ctx.writeAndFlush(new CloseWebSocketFrame(CloseReason.INTERNAL_ERROR.getCode(), CloseReason.INTERNAL_ERROR.getReason()));
@Override public void close(CloseReason closeReason) { if (channel.isOpen()) { channel.writeAndFlush(new CloseWebSocketFrame(closeReason.getCode(), closeReason.getReason())) .addListener(future -> channel.close()); } }
private void writeCloseFrameAndTerminate(ChannelHandlerContext ctx, int code, String reason) { final CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(code, reason); ctx.channel().writeAndFlush(closeFrame) .addListener(future -> handleCloseFrame(ctx, new CloseWebSocketFrame(code, reason))); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.channel().writeAndFlush(new CloseWebSocketFrame(1011, "Encountered an unexpected condition")); ctx.close(); connectorFuture.notifyWSListener(cause); } }
@Override public void close(CloseReason closeReason) { ctx.channel().writeAndFlush(new CloseWebSocketFrame(closeReason.getCloseCode().getCode(), closeReason.getReasonPhrase())); ctx.channel().close(); this.isOpen = false; }
private void notifyCloseMessage(CloseWebSocketFrame closeWebSocketFrame) throws ServerConnectorException { String reasonText = closeWebSocketFrame.reasonText(); int statusCode = closeWebSocketFrame.statusCode(); ctx.channel().close(); channelSession.setIsOpen(false); WebSocketMessageImpl webSocketCloseMessage = new WebSocketCloseMessageImpl(statusCode, reasonText); webSocketCloseMessage = setupCommonProperties(webSocketCloseMessage); connectorFuture.notifyWSListener((WebSocketCloseMessage) webSocketCloseMessage); }
@Override public Future<Void> sendCloseFrame(int statusCode, String reasonText) { if (channel.isOpen()) { return channel.writeAndFlush(new CloseWebSocketFrame(1000, "normal closure")); } return ImmediateEventExecutor.INSTANCE.newSucceededFuture(null); }
/** * Returns the closing status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. If * a getStatus code is set, -1 is returned. */ public int statusCode() { ByteBuf binaryData = content(); if (binaryData == null || binaryData.capacity() == 0) { return -1; } binaryData.readerIndex(0); int statusCode = binaryData.readShort(); binaryData.readerIndex(0); return statusCode; }
void sendCloseNow(@Nullable CloseWebSocketFrame frame, ChannelFutureListener listener) { if (frame != null && !frame.isFinalFragment()) { channel().writeAndFlush(frame); return; } if (CLOSE_SENT.getAndSet(this, 1) == 0) { ChannelFuture f = channel().writeAndFlush( frame == null ? new CloseWebSocketFrame() : frame); f.addListener(listener); } else if (frame != null) { frame.release(); } }
private WebSocketFrame decodeBinaryFrame(ChannelHandlerContext ctx, byte type, ByteBuf buffer) { long frameSize = 0; int lengthFieldSize = 0; byte b; do { b = buffer.readByte(); frameSize <<= 7; frameSize |= b & 0x7f; if (frameSize > maxFrameSize) { throw new TooLongFrameException(); } lengthFieldSize++; if (lengthFieldSize > 8) { // Perhaps a malicious peer? throw new TooLongFrameException(); } } while ((b & 0x80) == 0x80); if (type == (byte) 0xFF && frameSize == 0) { receivedClosingHandshake = true; return new CloseWebSocketFrame(); } ByteBuf payload = readBytes(ctx.alloc(), buffer, (int) frameSize); return new BinaryWebSocketFrame(payload); }
@Override public void close(final int status, final String reason) { handshaker.close(ctx.channel(), new CloseWebSocketFrame(status, reason)) .addListener(CLOSE); Attribute<NettyWebSocket> ws = ctx.channel().attr(KEY); if (ws != null) { ws.set(null); } }