Search in sources :

Example 6 with NetSocketInternal

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

the class NetTest method testNetSocketInternalBuffer.

@Test
public void testNetSocketInternalBuffer() throws Exception {
    server.connectHandler(so -> {
        NetSocketInternal soi = (NetSocketInternal) so;
        soi.handler(msg -> {
            ByteBuf byteBuf = msg.getByteBuf();
            assertFalse(byteBuf.isDirect());
            assertEquals(1, byteBuf.refCnt());
            assertFalse(byteBuf.release());
            assertEquals(1, byteBuf.refCnt());
            soi.write(msg);
        });
    });
    startServer();
    client.connect(testAddress, onSuccess(so -> {
        NetSocketInternal soi = (NetSocketInternal) so;
        soi.write(Buffer.buffer("Hello World"));
        soi.handler(msg -> {
            ByteBuf byteBuf = msg.getByteBuf();
            assertFalse(byteBuf.isDirect());
            assertEquals(1, byteBuf.refCnt());
            assertFalse(byteBuf.release());
            assertEquals(1, byteBuf.refCnt());
            assertEquals("Hello World", msg.toString());
            testComplete();
        });
    }));
    await();
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ByteBuf(io.netty.buffer.ByteBuf) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) Test(org.junit.Test)

Example 7 with NetSocketInternal

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

the class NetTest method testNetSocketInternalDirectBuffer.

@Test
public void testNetSocketInternalDirectBuffer() throws Exception {
    waitFor(2);
    server.connectHandler(so -> {
        NetSocketInternal soi = (NetSocketInternal) so;
        soi.messageHandler(msg -> {
            ByteBuf byteBuf = (ByteBuf) msg;
            assertTrue(byteBuf.isDirect());
            assertEquals(1, byteBuf.refCnt());
            soi.writeMessage(msg).onSuccess(v -> {
                assertEquals(0, byteBuf.refCnt());
                complete();
            });
        });
    });
    startServer();
    client.connect(testAddress, onSuccess(so -> {
        NetSocketInternal soi = (NetSocketInternal) so;
        soi.write(Buffer.buffer("Hello World"));
        // soi.messageHandler(msg -> fail("Unexpected"));
        soi.messageHandler(msg -> {
            ByteBuf byteBuf = (ByteBuf) msg;
            assertTrue(byteBuf.isDirect());
            assertEquals(1, byteBuf.refCnt());
            assertEquals("Hello World", byteBuf.toString(StandardCharsets.UTF_8));
            assertTrue(byteBuf.release());
            assertEquals(0, byteBuf.refCnt());
            complete();
        });
    }));
    await();
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ByteBuf(io.netty.buffer.ByteBuf) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) Test(org.junit.Test)

Example 8 with NetSocketInternal

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

the class NetTest method testNetClientInternal_.

private void testNetClientInternal_(HttpServerOptions options, boolean expectSSL) throws Exception {
    waitFor(2);
    HttpServer server = vertx.createHttpServer(options);
    server.requestHandler(req -> {
        req.response().end("Hello World");
    });
    CountDownLatch latch = new CountDownLatch(1);
    server.listen(onSuccess(v -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    client.connect(1234, "localhost", onSuccess(so -> {
        NetSocketInternal soInt = (NetSocketInternal) so;
        assertEquals(expectSSL, soInt.isSsl());
        ChannelHandlerContext chctx = soInt.channelHandlerContext();
        ChannelPipeline pipeline = chctx.pipeline();
        pipeline.addBefore("handler", "http", new HttpClientCodec());
        AtomicInteger status = new AtomicInteger();
        soInt.handler(buff -> fail());
        soInt.messageHandler(obj -> {
            switch(status.getAndIncrement()) {
                case 0:
                    assertTrue(obj instanceof HttpResponse);
                    HttpResponse resp = (HttpResponse) obj;
                    assertEquals(200, resp.status().code());
                    break;
                case 1:
                    assertTrue(obj instanceof LastHttpContent);
                    ByteBuf content = ((LastHttpContent) obj).content();
                    assertEquals(!expectSSL, content.isDirect());
                    assertEquals(1, content.refCnt());
                    String val = content.toString(StandardCharsets.UTF_8);
                    assertTrue(content.release());
                    assertEquals("Hello World", val);
                    complete();
                    break;
                default:
                    fail();
            }
        });
        soInt.writeMessage(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/somepath"), onSuccess(v -> complete()));
    }));
    await();
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal)

Example 9 with NetSocketInternal

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

the class NetTest method testNetServerInternal_.

private void testNetServerInternal_(HttpClientOptions clientOptions, boolean expectSSL) throws Exception {
    waitFor(2);
    server.connectHandler(so -> {
        NetSocketInternal internal = (NetSocketInternal) so;
        assertEquals(expectSSL, internal.isSsl());
        ChannelHandlerContext chctx = internal.channelHandlerContext();
        ChannelPipeline pipeline = chctx.pipeline();
        pipeline.addBefore("handler", "http", new HttpServerCodec());
        internal.handler(buff -> fail());
        internal.messageHandler(obj -> {
            if (obj instanceof LastHttpContent) {
                DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("Hello World", StandardCharsets.UTF_8));
                response.headers().set(HttpHeaderNames.CONTENT_LENGTH, "11");
                internal.writeMessage(response, onSuccess(v -> complete()));
            }
        });
    });
    startServer(SocketAddress.inetSocketAddress(1234, "localhost"));
    HttpClient client = vertx.createHttpClient(clientOptions);
    client.request(io.vertx.core.http.HttpMethod.GET, 1234, "localhost", "/somepath", onSuccess(req -> {
        req.send(onSuccess(resp -> {
            assertEquals(200, resp.statusCode());
            resp.body(onSuccess(body -> {
                assertEquals("Hello World", body.toString());
                complete();
            }));
        }));
    }));
    await();
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal)

Aggregations

NetSocketInternal (io.vertx.core.net.impl.NetSocketInternal)9 ByteBuf (io.netty.buffer.ByteBuf)7 Buffer (io.vertx.core.buffer.Buffer)7 Unpooled (io.netty.buffer.Unpooled)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 io.netty.handler.codec.http (io.netty.handler.codec.http)6 PlatformDependent (io.netty.util.internal.PlatformDependent)6 io.vertx.core (io.vertx.core)6 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)5 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)5 ChannelPipeline (io.netty.channel.ChannelPipeline)5 ConnectTimeoutException (io.netty.channel.ConnectTimeoutException)5 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpVersion (io.netty.handler.codec.http.HttpVersion)5 Message (io.vertx.core.eventbus.Message)5 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)5 io.vertx.core.http (io.vertx.core.http)5 ConcurrentHashSet (io.vertx.core.impl.ConcurrentHashSet)5 Utils (io.vertx.core.impl.Utils)5 VertxInternal (io.vertx.core.impl.VertxInternal)5