/** * Create new instance, set {@link ProtocolHandler} and register {@link TyrusEndpointWrapper}. * * @param protocolHandler used for writing data (sending). * @param endpointWrapper notifies registered endpoints about incoming events. */ public TyrusWebSocket(final ProtocolHandler protocolHandler, final TyrusEndpointWrapper endpointWrapper) { this.protocolHandler = protocolHandler; this.endpointWrapper = endpointWrapper; protocolHandler.setWebSocket(this); }
@Override public ProtocolHandler createHandler(boolean mask, MaskingKeyGenerator maskingKeyGenerator) { return new ProtocolHandler(mask, maskingKeyGenerator); }
/** * Raw frame is always whole (not partial). * * @param data serialized frame. * @return send future. */ public Future<Frame> sendRawFrame(ByteBuffer data) { lock.lock(); try { checkSendingFragment(); return send(data, null, true); } finally { lock.unlock(); } }
protocolHandler.setWriter(writer); protocolHandler.setWebSocket(socket); protocolHandler.setExtensions(extensions); protocolHandler.setExtensionContext(extensionContext);
final Frame incomingFrame = protocolHandler.unframe(data); Frame frame = incomingFrame; for (Extension extension : protocolHandler.getExtensions()) { if (extension instanceof ExtendedExtension) { try { protocolHandler.process(frame, socket);
TyrusConnection(TyrusEndpointWrapper endpointWrapper, ProtocolHandler protocolHandler, int incomingBufferSize, Writer writer, CloseListener closeListener, UpgradeRequest upgradeRequest, UpgradeResponse upgradeResponse, ExtendedExtension.ExtensionContext extensionContext, DebugContext debugContext) { protocolHandler.setWriter(writer); extensions = protocolHandler.getExtensions(); this.socket = endpointWrapper.createSocket(protocolHandler); // TODO: we might need to introduce some property to check whether we should put this header into the // response. final List<String> connectionIdHeader = upgradeRequest.getHeaders().get(UpgradeRequest.CLUSTER_CONNECTION_ID_HEADER); String connectionId; if (connectionIdHeader != null && connectionIdHeader.size() == 1) { connectionId = connectionIdHeader.get(0); } else { connectionId = upgradeResponse.getFirstHeaderValue(UpgradeRequest.CLUSTER_CONNECTION_ID_HEADER); } this.socket.onConnect(upgradeRequest, protocolHandler.getSubProtocol(), extensions, connectionId, debugContext); this.readHandler = new TyrusReadHandler(protocolHandler, socket, endpointWrapper, incomingBufferSize, extensionContext, debugContext); this.writer = writer; this.closeListener = closeListener; this.extensionContext = extensionContext; }
parsingState.finalFragment = isBitSet(opcode, 7); parsingState.controlFrame = isControlFrame(opcode); parsingState.opcode = (byte) (opcode & 0x7f); if (!parsingState.finalFragment && parsingState.controlFrame) { parsingState.length = decodeLength(parsingState.masker.unmask(lengthBytes)); .rsv1(isBitSet(parsingState.opcode, 6)) .rsv2(isBitSet(parsingState.opcode, 5)) .rsv3(isBitSet(parsingState.opcode, 4)) .opcode((byte) (parsingState.opcode & 0xf)) .payloadLength(parsingState.length)
/** * This callback will be invoked when the remote endpoint sent a closing frame. * <p> * The execution of this method is synchronized using {@link ProtocolHandler} instance; see TYRUS-385. Prevents * multiple invocations, especially from container/user code. * * @param frame the close frame from the remote endpoint. */ public void onClose(CloseFrame frame) { boolean locked = lock.tryLock(); if (locked) { try { final CloseReason closeReason = frame.getCloseReason(); if (endpointWrapper != null) { endpointWrapper.onClose(this, closeReason); } if (state.compareAndSet(State.CONNECTED, State.CLOSING)) { protocolHandler.close(closeReason.getCloseCode().getCode(), closeReason.getReasonPhrase()); } else { state.set(State.CLOSED); protocolHandler.doClose(); } } finally { lock.unlock(); } } }
byte opcode = checkForLastFrame(frame); if (frame.isRsv1()) { opcode |= 0x40; final byte[] lengthBytes = encodeLength(frame.getPayloadLength());
/** * Closes this {@link TyrusWebSocket} using the specified status code and * reason. * * @param code the closing status code. * @param reason the reason, if any. */ public void close(int code, String reason) { if (state.compareAndSet(State.CONNECTED, State.CLOSING)) { protocolHandler.close(code, reason); } }
protocolHandler.setWriter(writer); protocolHandler.setWebSocket(socket); protocolHandler.setExtensions(extensions); protocolHandler.setExtensionContext(extensionContext);
final Frame incomingFrame = protocolHandler.unframe(data); Frame frame = incomingFrame; for (Extension extension : protocolHandler.getExtensions()) { if (extension instanceof ExtendedExtension) { try { protocolHandler.process(frame, socket);
TyrusConnection(TyrusEndpointWrapper endpointWrapper, ProtocolHandler protocolHandler, int incomingBufferSize, Writer writer, CloseListener closeListener, UpgradeRequest upgradeRequest, UpgradeResponse upgradeResponse, ExtendedExtension.ExtensionContext extensionContext, DebugContext debugContext) { protocolHandler.setWriter(writer); extensions = protocolHandler.getExtensions(); this.socket = endpointWrapper.createSocket(protocolHandler); // TODO: we might need to introduce some property to check whether we should put this header into the // response. final List<String> connectionIdHeader = upgradeRequest.getHeaders().get(UpgradeRequest.CLUSTER_CONNECTION_ID_HEADER); String connectionId; if (connectionIdHeader != null && connectionIdHeader.size() == 1) { connectionId = connectionIdHeader.get(0); } else { connectionId = upgradeResponse.getFirstHeaderValue(UpgradeRequest.CLUSTER_CONNECTION_ID_HEADER); } this.socket.onConnect(upgradeRequest, protocolHandler.getSubProtocol(), extensions, connectionId, debugContext); this.readHandler = new TyrusReadHandler(protocolHandler, socket, endpointWrapper, incomingBufferSize, extensionContext, debugContext); this.writer = writer; this.closeListener = closeListener; this.extensionContext = extensionContext; }
parsingState.finalFragment = isBitSet(opcode, 7); parsingState.controlFrame = isControlFrame(opcode); parsingState.opcode = (byte) (opcode & 0x7f); if (!parsingState.finalFragment && parsingState.controlFrame) { parsingState.length = decodeLength(parsingState.masker.unmask(lengthBytes)); .rsv1(isBitSet(parsingState.opcode, 6)) .rsv2(isBitSet(parsingState.opcode, 5)) .rsv3(isBitSet(parsingState.opcode, 4)) .opcode((byte) (parsingState.opcode & 0xf)) .payloadLength(parsingState.length)
/** * This callback will be invoked when the remote endpoint sent a closing frame. * <p> * The execution of this method is synchronized using {@link ProtocolHandler} instance; see TYRUS-385. Prevents * multiple invocations, especially from container/user code. * * @param frame the close frame from the remote endpoint. */ public void onClose(CloseFrame frame) { boolean locked = lock.tryLock(); if (locked) { try { final CloseReason closeReason = frame.getCloseReason(); if (endpointWrapper != null) { endpointWrapper.onClose(this, closeReason); } if (state.compareAndSet(State.CONNECTED, State.CLOSING)) { protocolHandler.close(closeReason.getCloseCode().getCode(), closeReason.getReasonPhrase()); } else { state.set(State.CLOSED); protocolHandler.doClose(); } } finally { lock.unlock(); } } }
byte opcode = checkForLastFrame(frame); if (frame.isRsv1()) { opcode |= 0x40; final byte[] lengthBytes = encodeLength(frame.getPayloadLength());
/** * Closes this {@link TyrusWebSocket} using the specified status code and * reason. * * @param code the closing status code. * @param reason the reason, if any. */ public void close(int code, String reason) { if (state.compareAndSet(State.CONNECTED, State.CLOSING)) { protocolHandler.close(code, reason); } }
/** * Raw frame is always whole (not partial). * * @param data serialized frame. * @return send future. */ public Future<Frame> sendRawFrame(ByteBuffer data) { lock.lock(); try { checkSendingFragment(); return send(data, null, true); } finally { lock.unlock(); } }