Search in sources :

Example 11 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class HostnameResolutionTest method testNet.

private void testNet(String hostname) throws Exception {
    NetClient client = vertx.createNetClient();
    NetServer server = vertx.createNetServer().connectHandler(so -> {
        so.handler(buff -> {
            so.write(buff);
            so.close();
        });
    });
    try {
        CountDownLatch listenLatch = new CountDownLatch(1);
        server.listen(1234, hostname, onSuccess(s -> {
            listenLatch.countDown();
        }));
        awaitLatch(listenLatch);
        client.connect(1234, hostname, onSuccess(so -> {
            Buffer buffer = Buffer.buffer();
            so.handler(buffer::appendBuffer);
            so.closeHandler(v -> {
                assertEquals(Buffer.buffer("foo"), buffer);
                testComplete();
            });
            so.write(Buffer.buffer("foo"));
        }));
        await();
    } finally {
        client.close();
        server.close();
    }
}
Also used : VertxException(io.vertx.core.VertxException) RecordClass(org.apache.directory.server.dns.messages.RecordClass) java.util(java.util) HttpServer(io.vertx.core.http.HttpServer) CompletableFuture(java.util.concurrent.CompletableFuture) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) InetAddress(java.net.InetAddress) TestUtils(io.vertx.test.core.TestUtils) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) DnsAttribute(org.apache.directory.server.dns.store.DnsAttribute) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) HttpMethod(io.vertx.core.http.HttpMethod) RecordType(org.apache.directory.server.dns.messages.RecordType) HttpClient(io.vertx.core.http.HttpClient) Buffer(io.vertx.core.buffer.Buffer) NetClient(io.vertx.core.net.NetClient) NetServer(io.vertx.core.net.NetServer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with NetServer

use of io.vertx.core.net.NetServer in project java-chassis by ServiceComb.

the class TcpServer method init.

public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) {
    NetServer netServer;
    if (endpointObject.isSslEnabled()) {
        SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(sslKey, null);
        SSLOption sslOption;
        if (factory == null) {
            sslOption = SSLOption.buildFromYaml(sslKey);
        } else {
            sslOption = factory.createSSLOption();
        }
        SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
        NetServerOptions serverOptions = new NetServerOptions();
        VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions);
        netServer = vertx.createNetServer(serverOptions);
    } else {
        netServer = vertx.createNetServer();
    }
    netServer.connectHandler(netSocket -> {
        DefaultTcpServerMetrics serverMetrics = (DefaultTcpServerMetrics) ((NetSocketImpl) netSocket).metrics();
        DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric();
        long connectedCount = endpointMetric.getCurrentConnectionCount();
        int connectionLimit = getConnectionLimit();
        if (connectedCount > connectionLimit) {
            netSocket.close();
            endpointMetric.onRejectByConnectionLimit();
            return;
        }
        TcpServerConnection connection = createTcpServerConnection();
        connection.init(netSocket);
    });
    netServer.exceptionHandler(e -> {
        LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
    });
    InetSocketAddress socketAddress = endpointObject.getSocketAddress();
    netServer.listen(socketAddress.getPort(), socketAddress.getHostString(), ar -> {
        if (ar.succeeded()) {
            callback.success(socketAddress);
            return;
        }
        // 监听失败
        String msg = String.format("listen failed, address=%s", socketAddress.toString());
        callback.fail(new Exception(msg, ar.cause()));
    });
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NetServer(io.vertx.core.net.NetServer) DefaultTcpServerMetrics(org.apache.servicecomb.foundation.vertx.metrics.DefaultTcpServerMetrics) DefaultServerEndpointMetric(org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultServerEndpointMetric) SSLOptionFactory(org.apache.servicecomb.foundation.ssl.SSLOptionFactory) NetServerOptions(io.vertx.core.net.NetServerOptions) SSLOption(org.apache.servicecomb.foundation.ssl.SSLOption) SSLCustom(org.apache.servicecomb.foundation.ssl.SSLCustom)

Example 13 with NetServer

use of io.vertx.core.net.NetServer in project vertx-proton by vert-x3.

the class ProtonClientTest method testConnectionDisconnectedDuringCreation.

@Test(timeout = 20000)
public void testConnectionDisconnectedDuringCreation(TestContext context) {
    server.close();
    Async connectFailsAsync = context.async();
    NetServer netServer = this.vertx.createNetServer();
    netServer.connectHandler(netSocket -> {
        netSocket.pause();
        vertx.setTimer(50, x -> {
            netSocket.close();
        });
    });
    netServer.listen(listenResult -> {
        context.assertTrue(listenResult.succeeded());
        ProtonClient.create(vertx).connect("localhost", netServer.actualPort(), connResult -> {
            context.assertFalse(connResult.succeeded());
            connectFailsAsync.complete();
        });
    });
    connectFailsAsync.awaitSuccess();
}
Also used : Async(io.vertx.ext.unit.Async) NetServer(io.vertx.core.net.NetServer) Test(org.junit.Test)

Example 14 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class StreamsExamples method pump5.

public void pump5(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        Pump.pump(sock, sock).start();
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Example 15 with NetServer

use of io.vertx.core.net.NetServer in project vert.x by eclipse.

the class StreamsExamples method pump3.

public void pump3(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            sock.write(buffer);
            if (sock.writeQueueFull()) {
                sock.pause();
            }
        });
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Aggregations

NetServer (io.vertx.core.net.NetServer)28 NetServerOptions (io.vertx.core.net.NetServerOptions)20 Buffer (io.vertx.core.buffer.Buffer)16 Vertx (io.vertx.core.Vertx)15 Test (org.junit.Test)15 NetSocket (io.vertx.core.net.NetSocket)13 Handler (io.vertx.core.Handler)12 NetClient (io.vertx.core.net.NetClient)11 VertxOptions (io.vertx.core.VertxOptions)10 Pump (io.vertx.core.streams.Pump)10 HttpServer (io.vertx.core.http.HttpServer)9 ReadStream (io.vertx.core.streams.ReadStream)7 InetSocketAddress (java.net.InetSocketAddress)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeUnit (java.util.concurrent.TimeUnit)7 TestUtils (io.vertx.test.core.TestUtils)6 File (java.io.File)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AsyncFile (io.vertx.core.file.AsyncFile)5 FileSystem (io.vertx.core.file.FileSystem)5