Search in sources :

Example 11 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class VertxHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext chctx) throws Exception {
    C connection = removeConnection();
    if (connection != null) {
        ContextImpl context = getContext(connection);
        context.executeFromIO(connection::handleClosed);
    }
}
Also used : ContextImpl(io.vertx.core.impl.ContextImpl)

Example 12 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class VertxHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext chctx, final Throwable t) throws Exception {
    Channel ch = chctx.channel();
    // Don't remove the connection at this point, or the handleClosed won't be called when channelInactive is called!
    C connection = getConnection();
    if (connection != null) {
        ContextImpl context = getContext(connection);
        context.executeFromIO(() -> {
            try {
                if (ch.isOpen()) {
                    ch.close();
                }
            } catch (Throwable ignore) {
            }
            connection.handleException(t);
        });
    } else {
        ch.close();
    }
}
Also used : Channel(io.netty.channel.Channel) ContextImpl(io.vertx.core.impl.ContextImpl)

Example 13 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class HttpServerResponseImpl method doSendFile.

private void doSendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
    synchronized (conn) {
        if (headWritten) {
            throw new IllegalStateException("Head already written");
        }
        checkWritten();
        File file = vertx.resolveFile(filename);
        if (!file.exists()) {
            if (resultHandler != null) {
                ContextImpl ctx = vertx.getOrCreateContext();
                ctx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
            } else {
                log.error("File not found: " + filename);
            }
            return;
        }
        long contentLength = Math.min(length, file.length() - offset);
        bytesWritten = contentLength;
        if (!contentLengthSet()) {
            putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
        }
        if (!contentTypeSet()) {
            String contentType = MimeMapping.getMimeTypeForFilename(filename);
            if (contentType != null) {
                putHeader(HttpHeaders.CONTENT_TYPE, contentType);
            }
        }
        prepareHeaders();
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(file, "r");
            conn.queueForWrite(response);
            conn.sendFile(raf, Math.min(offset, file.length()), contentLength);
        } catch (IOException e) {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (IOException ignore) {
            }
            if (resultHandler != null) {
                ContextImpl ctx = vertx.getOrCreateContext();
                ctx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
            } else {
                log.error("Failed to send file", e);
            }
            return;
        }
        // write an empty last content to let the http encoder know the response is complete
        channelFuture = conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
        written = true;
        if (resultHandler != null) {
            ContextImpl ctx = vertx.getOrCreateContext();
            channelFuture.addListener(future -> {
                AsyncResult<Void> res;
                if (future.isSuccess()) {
                    res = Future.succeededFuture();
                } else {
                    res = Future.failedFuture(future.cause());
                }
                ctx.runOnContext((v) -> resultHandler.handle(res));
            });
        }
        if (!keepAlive) {
            closeConnAfterWrite();
        }
        conn.responseComplete();
        if (bodyEndHandler != null) {
            bodyEndHandler.handle(null);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) HttpVersion(io.netty.handler.codec.http.HttpVersion) VertxInternal(io.vertx.core.impl.VertxInternal) ContextImpl(io.vertx.core.impl.ContextImpl) MultiMap(io.vertx.core.MultiMap) HttpHeaders(io.vertx.core.http.HttpHeaders) IOException(java.io.IOException) Future(io.vertx.core.Future) io.vertx.core.http(io.vertx.core.http) LoggerFactory(io.vertx.core.logging.LoggerFactory) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Unpooled(io.netty.buffer.Unpooled) ChannelFuture(io.netty.channel.ChannelFuture) Nullable(io.vertx.codegen.annotations.Nullable) io.netty.handler.codec.http(io.netty.handler.codec.http) ByteBuf(io.netty.buffer.ByteBuf) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) RandomAccessFile(java.io.RandomAccessFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ContextImpl(io.vertx.core.impl.ContextImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

ContextImpl (io.vertx.core.impl.ContextImpl)13 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ChannelGroupFuture (io.netty.channel.group.ChannelGroupFuture)2 AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 VertxInternal (io.vertx.core.impl.VertxInternal)2 Logger (io.vertx.core.logging.Logger)2 LoggerFactory (io.vertx.core.logging.LoggerFactory)2 IOException (java.io.IOException)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Unpooled (io.netty.buffer.Unpooled)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelOption (io.netty.channel.ChannelOption)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1