Search in sources :

Example 11 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class AsyncSenderImpl method close.

@Override
public void close(final IoCallback callback) {
    try {
        StreamSinkChannel channel = this.channel;
        if (channel == null) {
            if (exchange.getResponseContentLength() == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) {
                exchange.setResponseContentLength(0);
            }
            this.channel = channel = exchange.getResponseChannel();
            if (channel == null) {
                throw UndertowMessages.MESSAGES.responseChannelAlreadyProvided();
            }
        }
        channel.shutdownWrites();
        if (!channel.flush()) {
            channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {

                @Override
                public void handleEvent(final StreamSinkChannel channel) {
                    if (callback != null) {
                        callback.onComplete(exchange, AsyncSenderImpl.this);
                    }
                }
            }, new ChannelExceptionHandler<StreamSinkChannel>() {

                @Override
                public void handleException(final StreamSinkChannel channel, final IOException exception) {
                    try {
                        if (callback != null) {
                            invokeOnException(callback, exception);
                        }
                    } finally {
                        IoUtils.safeClose(channel);
                    }
                }
            }));
            channel.resumeWrites();
        } else {
            if (callback != null) {
                callback.onComplete(exchange, this);
            }
        }
    } catch (IOException e) {
        if (callback != null) {
            invokeOnException(callback, e);
        }
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ChannelExceptionHandler(org.xnio.ChannelExceptionHandler) IOException(java.io.IOException)

Example 12 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class Undertow method start.

public synchronized void start() {
    UndertowLogger.ROOT_LOGGER.debugf("starting undertow server %s", this);
    xnio = Xnio.getInstance(Undertow.class.getClassLoader());
    channels = new ArrayList<>();
    try {
        if (internalWorker) {
            worker = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, ioThreads).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, workerThreads).set(Options.WORKER_TASK_MAX_THREADS, workerThreads).set(Options.TCP_NODELAY, true).set(Options.CORK, true).addAll(workerOptions).getMap());
        }
        OptionMap socketOptions = OptionMap.builder().set(Options.WORKER_IO_THREADS, worker.getIoThreadCount()).set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).set(Options.BALANCING_TOKENS, 1).set(Options.BALANCING_CONNECTIONS, 2).set(Options.BACKLOG, 1000).addAll(this.socketOptions).getMap();
        OptionMap serverOptions = OptionMap.builder().set(UndertowOptions.NO_REQUEST_TIMEOUT, 60 * 1000).addAll(this.serverOptions).getMap();
        ByteBufferPool buffers = new DefaultByteBufferPool(directBuffers, bufferSize, -1, 4);
        listenerInfo = new ArrayList<>();
        for (ListenerConfig listener : listeners) {
            UndertowLogger.ROOT_LOGGER.debugf("Configuring listener with protocol %s for interface %s and port %s", listener.type, listener.host, listener.port);
            final HttpHandler rootHandler = listener.rootHandler != null ? listener.rootHandler : this.rootHandler;
            if (listener.type == ListenerType.AJP) {
                AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
                openListener.setRootHandler(rootHandler);
                ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides);
                server.resumeAccepts();
                channels.add(server);
                listenerInfo.add(new ListenerInfo("ajp", server.getLocalAddress(), null, openListener));
            } else {
                OptionMap undertowOptions = OptionMap.builder().set(UndertowOptions.BUFFER_PIPELINED_DATA, true).addAll(serverOptions).getMap();
                if (listener.type == ListenerType.HTTP) {
                    HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
                    openListener.setRootHandler(rootHandler);
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                    OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                    AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides);
                    server.resumeAccepts();
                    channels.add(server);
                    listenerInfo.add(new ListenerInfo("http", server.getLocalAddress(), null, openListener));
                } else if (listener.type == ListenerType.HTTPS) {
                    OpenListener openListener;
                    HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
                    httpOpenListener.setRootHandler(rootHandler);
                    boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
                    if (http2) {
                        AlpnOpenListener alpn = new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
                        if (http2) {
                            Http2OpenListener http2Listener = new Http2OpenListener(buffers, undertowOptions);
                            http2Listener.setRootHandler(rootHandler);
                            alpn.addProtocol(Http2OpenListener.HTTP2, http2Listener, 10);
                            alpn.addProtocol(Http2OpenListener.HTTP2_14, http2Listener, 7);
                        }
                        openListener = alpn;
                    } else {
                        openListener = httpOpenListener;
                    }
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                    XnioSsl xnioSsl;
                    if (listener.sslContext != null) {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), listener.sslContext);
                    } else {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), JsseSslUtils.createSSLContext(listener.keyManagers, listener.trustManagers, new SecureRandom(), OptionMap.create(Options.USE_DIRECT_BUFFERS, true)));
                    }
                    OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                    AcceptingChannel<SslConnection> sslServer = xnioSsl.createSslConnectionServer(worker, new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides);
                    sslServer.resumeAccepts();
                    channels.add(sslServer);
                    listenerInfo.add(new ListenerInfo("https", sslServer.getLocalAddress(), listener.sslContext, openListener));
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) ByteBufferPool(io.undertow.connector.ByteBufferPool) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) HttpHandler(io.undertow.server.HttpHandler) ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) InetSocketAddress(java.net.InetSocketAddress) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OpenListener(io.undertow.server.OpenListener) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) SecureRandom(java.security.SecureRandom) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) StreamConnection(org.xnio.StreamConnection) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OptionMap(org.xnio.OptionMap) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 13 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class Http2ClearClientProvider method connect.

@Override
public void connect(final ClientCallback<ClientConnection> listener, final InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    final URI upgradeUri;
    try {
        upgradeUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        listener.failed(new IOException(e));
        return;
    }
    if (bindAddress != null) {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    } else {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StreamConnection(org.xnio.StreamConnection) URI(java.net.URI) BoundChannel(org.xnio.channels.BoundChannel)

Example 14 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class Http2Channel method sendGoAway.

public void sendGoAway(int status, final ChannelExceptionHandler<AbstractHttp2StreamSinkChannel> exceptionHandler) {
    if (thisGoneAway) {
        return;
    }
    thisGoneAway = true;
    if (UndertowLogger.REQUEST_IO_LOGGER.isDebugEnabled()) {
        UndertowLogger.REQUEST_IO_LOGGER.debugf(new ClosedChannelException(), "Sending goaway on channel %s", this);
    }
    Http2GoAwayStreamSinkChannel goAway = new Http2GoAwayStreamSinkChannel(this, status, lastGoodStreamId);
    try {
        goAway.shutdownWrites();
        if (!goAway.flush()) {
            goAway.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<Channel>() {

                @Override
                public void handleEvent(Channel channel) {
                    IoUtils.safeClose(Http2Channel.this);
                }
            }, exceptionHandler));
            goAway.resumeWrites();
        } else {
            IoUtils.safeClose(this);
        }
    } catch (IOException e) {
        exceptionHandler.handleException(goAway, e);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelListener(org.xnio.ChannelListener) AbstractFramedChannel(io.undertow.server.protocol.framed.AbstractFramedChannel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Example 15 with ChannelListener

use of org.xnio.ChannelListener in project undertow by undertow-io.

the class UndertowXnioSsl method connectSsl.

@SuppressWarnings("deprecation")
public IoFuture<ConnectedSslStreamChannel> connectSsl(final XnioWorker worker, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super ConnectedSslStreamChannel> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) {
    final FutureResult<ConnectedSslStreamChannel> futureResult = new FutureResult<>(IoUtils.directExecutor());
    final IoFuture<SslConnection> futureSslConnection = openSslConnection(worker, bindAddress, destination, new ChannelListener<SslConnection>() {

        public void handleEvent(final SslConnection sslConnection) {
            final ConnectedSslStreamChannel assembledChannel = new AssembledConnectedSslStreamChannel(sslConnection, sslConnection.getSourceChannel(), sslConnection.getSinkChannel());
            if (!futureResult.setResult(assembledChannel)) {
                safeClose(assembledChannel);
            } else {
                ChannelListeners.invokeChannelListener(assembledChannel, openListener);
            }
        }
    }, bindListener, optionMap).addNotifier(new IoFuture.HandlingNotifier<SslConnection, FutureResult<ConnectedSslStreamChannel>>() {

        public void handleCancelled(final FutureResult<ConnectedSslStreamChannel> result) {
            result.setCancelled();
        }

        public void handleFailed(final IOException exception, final FutureResult<ConnectedSslStreamChannel> result) {
            result.setException(exception);
        }
    }, futureResult);
    futureResult.getIoFuture().addNotifier(new IoFuture.HandlingNotifier<ConnectedStreamChannel, IoFuture<SslConnection>>() {

        public void handleCancelled(final IoFuture<SslConnection> result) {
            result.cancel();
        }
    }, futureSslConnection);
    futureResult.addCancelHandler(futureSslConnection);
    return futureResult.getIoFuture();
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ConnectedStreamChannel(org.xnio.channels.ConnectedStreamChannel) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) IoFuture(org.xnio.IoFuture) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) IOException(java.io.IOException) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel)

Aggregations

ChannelListener (org.xnio.ChannelListener)23 IOException (java.io.IOException)16 InetSocketAddress (java.net.InetSocketAddress)8 OptionMap (org.xnio.OptionMap)7 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)6 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)6 ByteBuffer (java.nio.ByteBuffer)6 StreamConnection (org.xnio.StreamConnection)6 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)6 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)5 HttpHandler (io.undertow.server.HttpHandler)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 ChannelExceptionHandler (org.xnio.ChannelExceptionHandler)4 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Channel (java.nio.channels.Channel)3 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)2 DeploymentManager (io.undertow.servlet.api.DeploymentManager)2 FilterInfo (io.undertow.servlet.api.FilterInfo)2