Example usage for io.netty.channel ChannelHandlerContext flush

List of usage examples for io.netty.channel ChannelHandlerContext flush

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext flush.

Prototype

@Override
    ChannelHandlerContext flush();

Source Link

Usage

From source file:com.liferay.sync.engine.lan.server.discovery.LanDiscoveryListenerHandler.java

License:Open Source License

@Override
public void channelReadComplete(ChannelHandlerContext channelHandlerContext) {

    channelHandlerContext.flush();
}

From source file:com.linecorp.armeria.client.http.Http2ClientConnectionHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);

    // NB: Http2ConnectionHandler does not flush the preface string automatically.
    ctx.flush();
}

From source file:com.linecorp.armeria.client.Http2ResponseDecoder.java

License:Apache License

@Override
HttpResponseWrapper addResponse(int id, @Nullable HttpRequest req, DecodedHttpResponse res,
        RequestLogBuilder logBuilder, long responseTimeoutMillis, long maxContentLength) {

    final HttpResponseWrapper resWrapper = super.addResponse(id, req, res, logBuilder, responseTimeoutMillis,
            maxContentLength);//  w ww .  java 2 s.  co  m

    resWrapper.completionFuture().whenCompleteAsync((unused, cause) -> {
        if (cause != null) {
            // Ensure that the resWrapper is closed.
            // This is needed in case the response is aborted by the client.
            resWrapper.close(cause);

            // We are not closing the connection but just send a RST_STREAM,
            // so we have to remove the response manually.
            removeResponse(id);

            // Reset the stream.
            final int streamId = idToStreamId(id);
            if (conn.streamMayHaveExisted(streamId)) {
                final ChannelHandlerContext ctx = channel().pipeline().lastContext();
                encoder.writeRstStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
                ctx.flush();
            }
        } else {
            // Ensure that the resWrapper is closed.
            // This is needed in case the response is aborted by the client.
            resWrapper.close();
        }
    }, channel().eventLoop());
    return resWrapper;
}

From source file:com.linecorp.armeria.internal.FlushConsolidationHandler.java

License:Apache License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (readInprogess) {
        // If there is still a read in progress we are sure we will see a channelReadComplete(...) call. Thus
        // we only need to flush if we reach the explicitFlushAfterFlushes limit.
        if (++flushPendingCount == explicitFlushAfterFlushes) {
            flushPendingCount = 0;/*w w  w  .j a v a2s. c  o  m*/
            ctx.flush();
        }
        return;
    }
    ctx.flush();
}

From source file:com.linecorp.armeria.internal.FlushConsolidationHandler.java

License:Apache License

private void flushIfNeeded(ChannelHandlerContext ctx, boolean resetReadInProgress) {
    if (resetReadInProgress) {
        readInprogess = false;/*from w w  w .  j a  va  2s .  co m*/
    }
    if (flushPendingCount > 0) {
        flushPendingCount = 0;
        ctx.flush();
    }
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

private ChannelFuture write(ChannelHandlerContext ctx, int id, HttpObject obj, boolean endStream) {
    if (id < currentId) {
        // Attempted to write something on a finished request/response; discard.
        // e.g. the request already timed out.
        return ctx.newFailedFuture(ClosedPublisherException.get());
    }//from w w  w.ja  v a  2s  .c o  m

    final PendingWrites currentPendingWrites = pendingWrites.get(id);
    if (id == currentId) {
        if (currentPendingWrites != null) {
            pendingWrites.remove(id);
            flushPendingWrites(ctx, currentPendingWrites);
        }

        final ChannelFuture future = ctx.write(obj);
        if (endStream) {
            currentId++;

            // The next PendingWrites might be complete already.
            for (;;) {
                final PendingWrites nextPendingWrites = pendingWrites.get(currentId);
                if (nextPendingWrites == null) {
                    break;
                }

                flushPendingWrites(ctx, nextPendingWrites);
                if (!nextPendingWrites.isEndOfStream()) {
                    break;
                }

                pendingWrites.remove(currentId);
                currentId++;
            }
        }

        ctx.flush();
        return future;
    } else {
        final ChannelPromise promise = ctx.newPromise();
        final Entry<HttpObject, ChannelPromise> entry = new SimpleImmutableEntry<>(obj, promise);

        if (currentPendingWrites == null) {
            final PendingWrites newPendingWrites = new PendingWrites();
            maxIdWithPendingWrites = Math.max(maxIdWithPendingWrites, id);
            newPendingWrites.add(entry);
            pendingWrites.put(id, newPendingWrites);
        } else {
            currentPendingWrites.add(entry);
        }

        if (endStream) {
            currentPendingWrites.setEndOfStream();
        }

        return promise;
    }
}

From source file:com.linecorp.armeria.internal.Http1ObjectEncoder.java

License:Apache License

private ChannelFuture writeServerHeaders(ChannelHandlerContext ctx, int id, int streamId, HttpHeaders headers,
        boolean endStream) throws Http2Exception {

    final HttpObject converted = convertServerHeaders(streamId, headers, endStream);
    final HttpStatus status = headers.status();
    if (status == null) {
        // Trailing headers
        final ChannelFuture f = write(ctx, id, converted, endStream);
        ctx.flush();
        return f;
    }//  www  .  j  a va  2 s .com

    if (status.codeClass() == HttpStatusClass.INFORMATIONAL) {
        // Informational status headers.
        final ChannelFuture f = write(ctx, id, converted, false);
        if (endStream) {
            // Can't end a stream with informational status in HTTP/1.
            f.addListener(ChannelFutureListener.CLOSE);
        }
        ctx.flush();
        return f;
    }

    // Non-informational status headers.
    return writeNonInformationalHeaders(ctx, id, converted, endStream);
}

From source file:com.linecorp.armeria.internal.Http1ObjectEncoder.java

License:Apache License

private ChannelFuture writeNonInformationalHeaders(ChannelHandlerContext ctx, int id, HttpObject converted,
        boolean endStream) {

    ChannelFuture f;/*from ww  w.j a  v a2  s.c  o m*/
    if (converted instanceof LastHttpContent) {
        assert endStream;
        f = write(ctx, id, converted, true);
    } else {
        f = write(ctx, id, converted, false);
        if (endStream) {
            f = write(ctx, id, LastHttpContent.EMPTY_LAST_CONTENT, true);
        }
    }

    ctx.flush();
    return f;
}

From source file:com.linecorp.armeria.internal.Http1ObjectEncoder.java

License:Apache License

@Override
protected ChannelFuture doWriteData(ChannelHandlerContext ctx, int id, int streamId, HttpData data,
        boolean endStream) {

    if (id >= minClosedId) {
        ReferenceCountUtil.safeRelease(data);
        return ctx.newFailedFuture(ClosedSessionException.get());
    }//  ww w.j  a v a2s .  co  m

    final int length = data.length();
    if (length == 0) {
        ReferenceCountUtil.safeRelease(data);
        final HttpContent content = endStream ? LastHttpContent.EMPTY_LAST_CONTENT : EMPTY_CONTENT;
        final ChannelFuture future = write(ctx, id, content, endStream);
        ctx.flush();
        return future;
    }

    try {
        if (!isTls || length <= MAX_TLS_DATA_LENGTH) {
            // Cleartext connection or data.length() <= MAX_TLS_DATA_LENGTH
            return doWriteUnsplitData(ctx, id, data, endStream);
        } else {
            // TLS and data.length() > MAX_TLS_DATA_LENGTH
            return doWriteSplitData(ctx, id, data, endStream);
        }
    } catch (Throwable t) {
        return ctx.newFailedFuture(t);
    }
}

From source file:com.linecorp.armeria.internal.Http1ObjectEncoder.java

License:Apache License

private ChannelFuture doWriteUnsplitData(ChannelHandlerContext ctx, int id, HttpData data, boolean endStream) {
    final ByteBuf buf = toByteBuf(ctx, data);
    boolean handled = false;
    try {// w  ww .  j  a  v a2s.c o  m
        final HttpContent content;
        if (endStream) {
            content = new DefaultLastHttpContent(buf);
        } else {
            content = new DefaultHttpContent(buf);
        }

        final ChannelFuture future = write(ctx, id, content, endStream);
        handled = true;
        ctx.flush();
        return future;
    } finally {
        if (!handled) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}