Search in sources :

Example 1 with AcceptingChannel

use of org.xnio.channels.AcceptingChannel in project undertow by undertow-io.

the class UndertowXnioSsl method createSslTcpServer.

@SuppressWarnings("deprecation")
public AcceptingChannel<ConnectedSslStreamChannel> createSslTcpServer(final XnioWorker worker, final InetSocketAddress bindAddress, final ChannelListener<? super AcceptingChannel<ConnectedSslStreamChannel>> acceptListener, final OptionMap optionMap) throws IOException {
    final AcceptingChannel<SslConnection> server = createSslConnectionServer(worker, bindAddress, null, optionMap);
    final AcceptingChannel<ConnectedSslStreamChannel> acceptingChannel = new AcceptingChannel<ConnectedSslStreamChannel>() {

        public ConnectedSslStreamChannel accept() throws IOException {
            final SslConnection connection = server.accept();
            return connection == null ? null : new AssembledConnectedSslStreamChannel(connection, connection.getSourceChannel(), connection.getSinkChannel());
        }

        public ChannelListener.Setter<? extends AcceptingChannel<ConnectedSslStreamChannel>> getAcceptSetter() {
            return ChannelListeners.getDelegatingSetter(server.getAcceptSetter(), this);
        }

        public ChannelListener.Setter<? extends AcceptingChannel<ConnectedSslStreamChannel>> getCloseSetter() {
            return ChannelListeners.getDelegatingSetter(server.getCloseSetter(), this);
        }

        public SocketAddress getLocalAddress() {
            return server.getLocalAddress();
        }

        public <A extends SocketAddress> A getLocalAddress(final Class<A> type) {
            return server.getLocalAddress(type);
        }

        public void suspendAccepts() {
            server.suspendAccepts();
        }

        public void resumeAccepts() {
            server.resumeAccepts();
        }

        public boolean isAcceptResumed() {
            return server.isAcceptResumed();
        }

        public void wakeupAccepts() {
            server.wakeupAccepts();
        }

        public void awaitAcceptable() throws IOException {
            server.awaitAcceptable();
        }

        public void awaitAcceptable(final long time, final TimeUnit timeUnit) throws IOException {
            server.awaitAcceptable(time, timeUnit);
        }

        public XnioWorker getWorker() {
            return server.getWorker();
        }

        @Deprecated
        public XnioExecutor getAcceptThread() {
            return server.getAcceptThread();
        }

        public XnioIoThread getIoThread() {
            return server.getIoThread();
        }

        public void close() throws IOException {
            server.close();
        }

        public boolean isOpen() {
            return server.isOpen();
        }

        public boolean supportsOption(final Option<?> option) {
            return server.supportsOption(option);
        }

        public <T> T getOption(final Option<T> option) throws IOException {
            return server.getOption(option);
        }

        public <T> T setOption(final Option<T> option, final T value) throws IllegalArgumentException, IOException {
            return server.setOption(option, value);
        }
    };
    acceptingChannel.getAcceptSetter().set(acceptListener);
    return acceptingChannel;
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ChannelListener(org.xnio.ChannelListener) TimeUnit(java.util.concurrent.TimeUnit) Option(org.xnio.Option) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 2 with AcceptingChannel

use of org.xnio.channels.AcceptingChannel in project wildfly by wildfly.

the class ListenerService method setEnabled.

public synchronized void setEnabled(boolean enabled) {
    if (started && enabled != this.enabled) {
        if (enabled) {
            final InetSocketAddress socketAddress = binding.get().getSocketAddress();
            final ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
            try {
                startListening(worker.get(), socketAddress, acceptListener);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            stopListening();
        }
    }
    this.enabled = enabled;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 3 with AcceptingChannel

use of org.xnio.channels.AcceptingChannel in project wildfly by wildfly.

the class ListenerService method start.

@Override
public void start(final StartContext context) throws StartException {
    started = true;
    preStart(context);
    serverService.get().registerListener(this);
    try {
        openListener = createOpenListener();
        HttpHandler handler = serverService.get().getRoot();
        for (HandlerWrapper wrapper : listenerHandlerWrappers) {
            handler = wrapper.wrap(handler);
        }
        openListener.setRootHandler(handler);
        if (enabled) {
            final InetSocketAddress socketAddress = binding.get().getSocketAddress();
            final ChannelListener<AcceptingChannel<StreamConnection>> acceptListener;
            if (proxyProtocol) {
                UndertowXnioSsl xnioSsl = getSsl();
                acceptListener = ChannelListeners.openListenerAdapter(new ProxyProtocolOpenListener(openListener, xnioSsl, bufferPool.get(), xnioSsl != null ? getSSLOptions(xnioSsl.getSslContext()) : null));
            } else {
                acceptListener = ChannelListeners.openListenerAdapter(openListener);
            }
            startListening(worker.get(), socketAddress, acceptListener);
        }
        registerBinding();
    } catch (IOException e) {
        cleanFailedStart();
        if (e instanceof BindException) {
            final StringBuilder sb = new StringBuilder().append(e.getLocalizedMessage());
            final InetSocketAddress socketAddress = binding.get().getSocketAddress();
            if (socketAddress != null)
                sb.append(" ").append(socketAddress);
            throw new StartException(sb.toString());
        } else {
            throw UndertowLogger.ROOT_LOGGER.couldNotStartListener(name, e);
        }
    }
    statisticsChangeListener = (enabled) -> {
        OptionMap options = openListener.getUndertowOptions();
        OptionMap.Builder builder = OptionMap.builder().addAll(options);
        builder.set(UndertowOptions.ENABLE_STATISTICS, enabled);
        openListener.setUndertowOptions(builder.getMap());
    };
    getUndertowService().registerStatisticsListener(statisticsChangeListener);
    final ServiceContainer container = context.getController().getServiceContainer();
    this.stoppingWrapper = new HandlerWrapper() {

        @Override
        public HttpHandler wrap(HttpHandler handler) {
            return new HttpHandler() {

                @Override
                public void handleRequest(HttpServerExchange exchange) throws Exception {
                    // even without graceful shutdown we start returning 503 once the container has started shutting down
                    if (container.isShutdown()) {
                        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
                        return;
                    }
                    handler.handleRequest(exchange);
                }
            };
        }
    };
    addWrapperHandler(stoppingWrapper);
    serviceConsumer.accept(this);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) IOException(java.io.IOException) HandlerWrapper(io.undertow.server.HandlerWrapper) BindException(java.net.BindException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) ProxyProtocolOpenListener(io.undertow.server.protocol.proxy.ProxyProtocolOpenListener) ServiceContainer(org.jboss.msc.service.ServiceContainer) OptionMap(org.xnio.OptionMap) StartException(org.jboss.msc.service.StartException) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 4 with AcceptingChannel

use of org.xnio.channels.AcceptingChannel in project indy by Commonjava.

the class HttpProxy method start.

@Override
public void start() throws IndyLifecycleException {
    if (!config.isEnabled()) {
        logger.info("HTTProx proxy is disabled.");
        return;
    }
    String bind;
    if (bootOptions.getBind() == null) {
        bind = "0.0.0.0";
    } else {
        bind = bootOptions.getBind();
    }
    logger.info("Starting HTTProx proxy on: {}:{}", bind, config.getPort());
    XnioWorker worker;
    try {
        worker = Xnio.getInstance().createWorker(OptionMap.EMPTY);
        final InetSocketAddress addr;
        if (config.getPort() < 1) {
            ThreadLocal<InetSocketAddress> using = new ThreadLocal<>();
            ThreadLocal<IOException> errorHolder = new ThreadLocal<>();
            server = PortFinder.findPortFor(16, (foundPort) -> {
                InetSocketAddress a = new InetSocketAddress(bind, config.getPort());
                AcceptingChannel<StreamConnection> result = worker.createStreamConnectionServer(a, acceptHandler, OptionMap.EMPTY);
                result.resumeAccepts();
                using.set(a);
                return result;
            });
            addr = using.get();
            config.setPort(addr.getPort());
        } else {
            addr = new InetSocketAddress(bind, config.getPort());
            server = worker.createStreamConnectionServer(addr, acceptHandler, OptionMap.EMPTY);
            server.resumeAccepts();
        }
        logger.info("HTTProxy listening on: {}", addr);
    } catch (IllegalArgumentException | IOException e) {
        throw new IndyLifecycleException("Failed to start HTTProx general content proxy: %s", e, e.getMessage());
    }
}
Also used : Logger(org.slf4j.Logger) Xnio(org.xnio.Xnio) ShutdownAction(org.commonjava.indy.action.ShutdownAction) XnioWorker(org.xnio.XnioWorker) LoggerFactory(org.slf4j.LoggerFactory) PortFinder(org.commonjava.propulsor.boot.PortFinder) IOException(java.io.IOException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) AcceptingChannel(org.xnio.channels.AcceptingChannel) InetSocketAddress(java.net.InetSocketAddress) OptionMap(org.xnio.OptionMap) Inject(javax.inject.Inject) StreamConnection(org.xnio.StreamConnection) HttproxConfig(org.commonjava.indy.httprox.conf.HttproxConfig) StartupAction(org.commonjava.indy.action.StartupAction) BootOptions(org.commonjava.propulsor.boot.BootOptions) ProxyAcceptHandler(org.commonjava.indy.httprox.handler.ProxyAcceptHandler) ApplicationScoped(javax.enterprise.context.ApplicationScoped) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 5 with AcceptingChannel

use of org.xnio.channels.AcceptingChannel in project undertow by undertow-io.

the class Undertow method start.

public synchronized void start() {
    UndertowLogger.ROOT_LOGGER.infof("starting server: %s", Version.getFullVersionString());
    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 = this.byteBufferPool;
        if (buffers == null) {
            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;
            OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
            if (listener.type == ListenerType.AJP) {
                AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
                openListener.setRootHandler(rootHandler);
                final ChannelListener<StreamConnection> finalListener;
                if (listener.useProxyProtocol) {
                    finalListener = new ProxyProtocolOpenListener(openListener, null, buffers, OptionMap.EMPTY);
                } else {
                    finalListener = openListener;
                }
                ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(finalListener);
                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(), openListener, null, server));
            } else {
                OptionMap undertowOptions = OptionMap.builder().set(UndertowOptions.BUFFER_PIPELINED_DATA, true).addAll(serverOptions).getMap();
                boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
                if (listener.type == ListenerType.HTTP) {
                    HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
                    HttpHandler handler = rootHandler;
                    if (http2) {
                        handler = new Http2UpgradeHandler(handler);
                    }
                    openListener.setRootHandler(handler);
                    final ChannelListener<StreamConnection> finalListener;
                    if (listener.useProxyProtocol) {
                        finalListener = new ProxyProtocolOpenListener(openListener, null, buffers, OptionMap.EMPTY);
                    } else {
                        finalListener = openListener;
                    }
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(finalListener);
                    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(), openListener, null, server));
                } else if (listener.type == ListenerType.HTTPS) {
                    OpenListener openListener;
                    HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
                    httpOpenListener.setRootHandler(rootHandler);
                    if (http2) {
                        AlpnOpenListener alpn = new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
                        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;
                    }
                    UndertowXnioSsl xnioSsl;
                    if (listener.sslContext != null) {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), listener.sslContext, sslEngineDelegatedTaskExecutor);
                    } else {
                        OptionMap.Builder builder = OptionMap.builder().addAll(socketOptionsWithOverrides);
                        if (!socketOptionsWithOverrides.contains(Options.SSL_PROTOCOL)) {
                            builder.set(Options.SSL_PROTOCOL, "TLSv1.2");
                        }
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), JsseSslUtils.createSSLContext(listener.keyManagers, listener.trustManagers, new SecureRandom(), builder.getMap()), sslEngineDelegatedTaskExecutor);
                    }
                    AcceptingChannel<? extends StreamConnection> sslServer;
                    if (listener.useProxyProtocol) {
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(new ProxyProtocolOpenListener(openListener, xnioSsl, buffers, socketOptionsWithOverrides));
                        sslServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides);
                    } else {
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                        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(), openListener, xnioSsl, sslServer));
                }
            }
        }
    } catch (Exception e) {
        if (internalWorker && worker != null) {
            worker.shutdownNow();
        }
        throw new RuntimeException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) ByteBufferPool(io.undertow.connector.ByteBufferPool) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) ChannelListener(org.xnio.ChannelListener) InetSocketAddress(java.net.InetSocketAddress) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) ProxyProtocolOpenListener(io.undertow.server.protocol.proxy.ProxyProtocolOpenListener) HttpHandler(io.undertow.server.HttpHandler) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) Http2UpgradeHandler(io.undertow.server.protocol.http2.Http2UpgradeHandler) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) ProxyProtocolOpenListener(io.undertow.server.protocol.proxy.ProxyProtocolOpenListener) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OpenListener(io.undertow.server.OpenListener) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) SecureRandom(java.security.SecureRandom) StreamConnection(org.xnio.StreamConnection) IOException(java.io.IOException) 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)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)6 AcceptingChannel (org.xnio.channels.AcceptingChannel)6 IOException (java.io.IOException)5 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)3 HttpHandler (io.undertow.server.HttpHandler)3 OptionMap (org.xnio.OptionMap)3 ByteBufferPool (io.undertow.connector.ByteBufferPool)2 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)2 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)2 ProxyProtocolOpenListener (io.undertow.server.protocol.proxy.ProxyProtocolOpenListener)2 ChannelListener (org.xnio.ChannelListener)2 StreamConnection (org.xnio.StreamConnection)2 SslConnection (org.xnio.ssl.SslConnection)2 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 OpenListener (io.undertow.server.OpenListener)1 AjpOpenListener (io.undertow.server.protocol.ajp.AjpOpenListener)1 AlpnOpenListener (io.undertow.server.protocol.http.AlpnOpenListener)1 Http2OpenListener (io.undertow.server.protocol.http2.Http2OpenListener)1 Http2UpgradeHandler (io.undertow.server.protocol.http2.Http2UpgradeHandler)1