/** * Create a new {@link Http2ConnectionHandler}. */ protected T build() { if (encoder != null) { assert decoder != null; return buildFromCodec(decoder, encoder); } Http2Connection connection = this.connection; if (connection == null) { connection = new DefaultHttp2Connection(isServer(), maxReservedStreams()); } return buildFromConnection(connection); }
@Override public Http2Stream open(boolean halfClosed) throws Http2Exception { state = activeState(id, state, isLocal(), halfClosed); if (!createdBy().canOpenStream()) { throw connectionError(PROTOCOL_ERROR, "Maximum active streams violated for this endpoint."); } activate(); return this; }
private void closeStreamsGreaterThanLastKnownStreamId(final int lastKnownStream, final DefaultEndpoint<?> endpoint) throws Http2Exception { forEachActiveStream(new Http2StreamVisitor() { @Override public boolean visit(Http2Stream stream) { if (stream.id() > lastKnownStream && endpoint.isValidStreamId(stream.id())) { stream.close(); } return true; } }); }
/** * Remove a stream from the {@link #streamMap}. * @param stream the stream to remove. * @param itr an iterator that may be pointing to the stream during iteration and {@link Iterator#remove()} will be * used if non-{@code null}. */ void removeStream(DefaultStream stream, Iterator<?> itr) { final boolean removed; if (itr == null) { removed = streamMap.remove(stream.id()) != null; } else { itr.remove(); removed = true; } if (removed) { for (int i = 0; i < listeners.size(); i++) { try { listeners.get(i).onStreamRemoved(stream); } catch (Throwable cause) { logger.error("Caught Throwable from listener onStreamRemoved.", cause); } } if (closePromise != null && isStreamMapEmpty()) { closePromise.trySuccess(null); } } }
private void checkNewStreamAllowed(int streamId, State state) throws Http2Exception { assert state != IDLE; if (goAwayReceived() && streamId > localEndpoint.lastStreamKnownByPeer()) { throw connectionError(PROTOCOL_ERROR, "Cannot create stream %d since this endpoint has received a " + "GOAWAY frame with last stream id %d.", streamId, throw streamError(streamId, REFUSED_STREAM, "Maximum active streams violated for this endpoint."); if (isClosed()) { throw connectionError(INTERNAL_ERROR, "Attempted to create stream id %d after connection was closed", streamId);
throw streamError(streamId, REFUSED_STREAM, "Maximum active streams violated for this endpoint."); if (isClosed()) { throw connectionError(INTERNAL_ERROR, "Attempted to create stream id %d after connection was closed", streamId);
closePromise = promise; if (isStreamMapEmpty()) { promise.trySuccess(null); return promise;
throw streamError(streamId, REFUSED_STREAM, "Maximum active streams violated for this endpoint."); if (isClosed()) { throw connectionError(INTERNAL_ERROR, "Attempted to create stream id %d after connection was closed", streamId);
private Http2ClientConnectionHandler newHttp2ConnectionHandler(Channel ch) { final boolean validateHeaders = false; final Http2Connection conn = new DefaultHttp2Connection(false); final Http2FrameReader reader = new DefaultHttp2FrameReader(validateHeaders); final Http2FrameWriter writer = new DefaultHttp2FrameWriter(); final Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(conn, writer); final Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(conn, encoder, reader); return new Http2ClientConnectionHandler(decoder, encoder, http2Settings(), ch, clientFactory); }
@Override public DefaultStream createStream(int streamId, boolean halfClosed) throws Http2Exception { State state = activeState(streamId, IDLE, isLocal(), halfClosed); checkNewStreamAllowed(streamId, state); // Create and initialize the stream. DefaultStream stream = new DefaultStream(streamId, state); incrementExpectedStreamId(streamId); addStream(stream); stream.activate(); return stream; }
/** * Remove a stream from the {@link #streamMap}. * @param stream the stream to remove. * @param itr an iterator that may be pointing to the stream during iteration and {@link Iterator#remove()} will be * used if non-{@code null}. */ void removeStream(DefaultStream stream, Iterator<?> itr) { final boolean removed; if (itr == null) { removed = streamMap.remove(stream.id()) != null; } else { itr.remove(); removed = true; } if (removed) { for (int i = 0; i < listeners.size(); i++) { try { listeners.get(i).onStreamRemoved(stream); } catch (Throwable cause) { logger.error("Caught Throwable from listener onStreamRemoved.", cause); } } if (closePromise != null && isStreamMapEmpty()) { closePromise.trySuccess(null); } } }
private void closeStreamsGreaterThanLastKnownStreamId(final int lastKnownStream, final DefaultEndpoint<?> endpoint) throws Http2Exception { forEachActiveStream(new Http2StreamVisitor() { @Override public boolean visit(Http2Stream stream) { if (stream.id() > lastKnownStream && endpoint.isValidStreamId(stream.id())) { stream.close(); } return true; } }); }
private Http2ConnectionHandler newHttp2ConnectionHandler(ChannelPipeline pipeline) { final Http2Connection conn = new DefaultHttp2Connection(true); final Http2FrameReader reader = new DefaultHttp2FrameReader(true); final Http2FrameWriter writer = new DefaultHttp2FrameWriter(); final Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(conn, writer); final Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(conn, encoder, reader); return new Http2ServerConnectionHandler( decoder, encoder, http2Settings(), pipeline.channel(), config, gracefulShutdownSupport); }
@Override public Http2Stream open(boolean halfClosed) throws Http2Exception { state = activeState(id, state, isLocal(), halfClosed); if (!createdBy().canOpenStream()) { throw connectionError(PROTOCOL_ERROR, "Maximum active streams violated for this endpoint."); } activate(); return this; }
/** * Remove a stream from the {@link #streamMap}. * @param stream the stream to remove. * @param itr an iterator that may be pointing to the stream during iteration and {@link Iterator#remove()} will be * used if non-{@code null}. */ void removeStream(DefaultStream stream, Iterator<?> itr) { final boolean removed; if (itr == null) { removed = streamMap.remove(stream.id()) != null; } else { itr.remove(); removed = true; } if (removed) { for (int i = 0; i < listeners.size(); i++) { try { listeners.get(i).onStreamRemoved(stream); } catch (Throwable cause) { logger.error("Caught Throwable from listener onStreamRemoved.", cause); } } if (closePromise != null && isStreamMapEmpty()) { closePromise.trySuccess(null); } } }
@Override public void goAwayReceived(final int lastKnownStream, long errorCode, ByteBuf debugData) { localEndpoint.lastStreamKnownByPeer(lastKnownStream); for (int i = 0; i < listeners.size(); ++i) { try { listeners.get(i).onGoAwayReceived(lastKnownStream, errorCode, debugData); } catch (Throwable cause) { logger.error("Caught Throwable from listener onGoAwayReceived.", cause); } } try { forEachActiveStream(new Http2StreamVisitor() { @Override public boolean visit(Http2Stream stream) { if (stream.id() > lastKnownStream && localEndpoint.isValidStreamId(stream.id())) { stream.close(); } return true; } }); } catch (Http2Exception e) { PlatformDependent.throwException(e); } }
Http2ConnectionHandler(boolean server, Http2FrameWriter frameWriter, Http2FrameLogger frameLogger, Http2Settings initialSettings) { this.initialSettings = checkNotNull(initialSettings, "initialSettings"); Http2Connection connection = new DefaultHttp2Connection(server); Long maxHeaderListSize = initialSettings.maxHeaderListSize(); Http2FrameReader frameReader = new DefaultHttp2FrameReader(maxHeaderListSize == null ? new DefaultHttp2HeadersDecoder(true) : new DefaultHttp2HeadersDecoder(true, maxHeaderListSize)); if (frameLogger != null) { frameWriter = new Http2OutboundFrameLogger(frameWriter, frameLogger); frameReader = new Http2InboundFrameLogger(frameReader, frameLogger); } encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter); decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader); }
@Override public Http2Stream open(boolean halfClosed) throws Http2Exception { state = activeState(id, state, isLocal(), halfClosed); if (!createdBy().canOpenStream()) { throw connectionError(PROTOCOL_ERROR, "Maximum active streams violated for this endpoint."); } activate(); return this; }
closePromise = promise; if (isStreamMapEmpty()) { promise.trySuccess(null); return promise;
@Override public void goAwaySent(final int lastKnownStream, long errorCode, ByteBuf debugData) { remoteEndpoint.lastStreamKnownByPeer(lastKnownStream); for (int i = 0; i < listeners.size(); ++i) { try { listeners.get(i).onGoAwaySent(lastKnownStream, errorCode, debugData); } catch (Throwable cause) { logger.error("Caught Throwable from listener onGoAwaySent.", cause); } } try { forEachActiveStream(new Http2StreamVisitor() { @Override public boolean visit(Http2Stream stream) { if (stream.id() > lastKnownStream && remoteEndpoint.isValidStreamId(stream.id())) { stream.close(); } return true; } }); } catch (Http2Exception e) { PlatformDependent.throwException(e); } }