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();
}
}
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()));
});
}
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();
}
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();
}
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();
}
Aggregations