use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class StreamsExamples method pump4.
public void pump4(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();
sock.drainHandler(done -> {
sock.resume();
});
}
});
}).listen();
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class WebsocketTest method doTestClientWebsocketConnectionCloseOnBadResponse.
private void doTestClientWebsocketConnectionCloseOnBadResponse(boolean keepAliveInOptions) throws Throwable {
final Exception serverGotCloseException = new Exception();
netServer = vertx.createNetServer().connectHandler(sock -> {
final Buffer fullReq = Buffer.buffer(230);
sock.handler(b -> {
fullReq.appendBuffer(b);
String reqPart = b.toString();
if (fullReq.toString().contains("\r\n\r\n")) {
try {
String content = "0123456789";
content = content + content;
content = content + content + content + content + content;
String resp = "HTTP/1.1 200 OK\r\n";
if (keepAliveInOptions) {
resp += "Connection: close\r\n";
}
resp += "Content-Length: 100\r\n\r\n" + content;
sock.write(Buffer.buffer(resp.getBytes("ASCII")));
} catch (UnsupportedEncodingException e) {
addResult(e);
}
}
});
sock.closeHandler(v -> {
addResult(serverGotCloseException);
});
}).listen(ar -> {
if (ar.failed()) {
addResult(ar.cause());
return;
}
NetServer server = ar.result();
int port = server.actualPort();
HttpClientOptions opts = new HttpClientOptions().setKeepAlive(keepAliveInOptions);
client.close();
client = vertx.createHttpClient(opts).websocket(port, "localhost", "/", ws -> {
addResult(new AssertionError("Websocket unexpectedly connected"));
ws.close();
}, t -> {
addResult(t);
});
});
boolean serverGotClose = false;
boolean clientGotCorrectException = false;
while (!serverGotClose || !clientGotCorrectException) {
Throwable result = resultQueue.poll(20, TimeUnit.SECONDS);
if (result == null) {
throw new AssertionError("Timed out waiting for expected state, current: serverGotClose = " + serverGotClose + ", clientGotCorrectException = " + clientGotCorrectException);
} else if (result == serverGotCloseException) {
serverGotClose = true;
} else if (result instanceof WebSocketHandshakeException && result.getMessage().equals("Websocket connection attempt returned HTTP status code 200")) {
clientGotCorrectException = true;
} else {
throw result;
}
}
}
use of io.vertx.core.net.NetServer in project vert.x by eclipse.
the class HostnameResolutionTest method testResolveMissingLocalhost.
@Test
public void testResolveMissingLocalhost() throws Exception {
InetAddress localhost = InetAddress.getByName("localhost");
// Set a dns resolver that won't resolve localhost
dnsServer.stop();
dnsServer = FakeDNSServer.testResolveASameServer("127.0.0.1");
dnsServer.start();
dnsServerAddress = (InetSocketAddress) dnsServer.getTransports()[0].getAcceptor().getLocalAddress();
// Test using the resolver API
VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false)));
CompletableFuture<Void> test1 = new CompletableFuture<>();
vertx.resolveAddress("localhost", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test1.complete(null);
} else {
test1.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test1.completeExceptionally(ar.cause());
}
});
test1.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test2 = new CompletableFuture<>();
vertx.resolveAddress("LOCALHOST", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test2.complete(null);
} else {
test2.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test2.completeExceptionally(ar.cause());
}
});
test2.get(10, TimeUnit.SECONDS);
// Test using bootstrap
CompletableFuture<Void> test3 = new CompletableFuture<>();
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress()));
server.connectHandler(so -> {
so.write("hello").end();
});
server.listen(ar -> {
if (ar.succeeded()) {
test3.complete(null);
} else {
test3.completeExceptionally(ar.cause());
}
});
test3.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test4 = new CompletableFuture<>();
NetClient client = vertx.createNetClient();
client.connect(1234, "localhost", ar -> {
if (ar.succeeded()) {
test4.complete(null);
} else {
test4.completeExceptionally(ar.cause());
}
});
test4.get(10, TimeUnit.SECONDS);
}
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 -> {
TcpServerConnection connection = createTcpServerConnection();
connection.init(netSocket);
});
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 incubator-servicecomb-java-chassis by apache.
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 -> {
TcpServerConnection connection = createTcpServerConnection();
connection.init(netSocket);
});
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()));
});
}
Aggregations