Search in sources :

Example 51 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class ClientConnection method handleResponseEnd.

void handleResponseEnd(LastHttpContent trailer) {
    if (metrics.isEnabled()) {
        HttpClientRequestBase req = currentResponse.request();
        Object reqMetric = req.metric();
        if (req.exceptionOccurred != null) {
            metrics.requestReset(reqMetric);
        } else {
            metrics.responseEnd(reqMetric, currentResponse);
        }
    }
    Buffer last = pausedChunk;
    pausedChunk = null;
    currentResponse.handleEnd(last, new HeadersAdaptor(trailer.trailingHeaders()));
    // Also we keep the connection open for an HTTP CONNECT
    if (currentResponse.statusCode() != 100 && requestForResponse.method() != io.vertx.core.http.HttpMethod.CONNECT) {
        boolean close = false;
        // See https://tools.ietf.org/html/rfc7230#section-6.3
        String responseConnectionHeader = currentResponse.getHeader(HttpHeaders.Names.CONNECTION);
        io.vertx.core.http.HttpVersion protocolVersion = client.getOptions().getProtocolVersion();
        String requestConnectionHeader = requestForResponse.headers().get(HttpHeaders.Names.CONNECTION);
        // We don't need to protect against concurrent changes on forceClose as it only goes from false -> true
        if (HttpHeaders.Values.CLOSE.equalsIgnoreCase(responseConnectionHeader) || HttpHeaders.Values.CLOSE.equalsIgnoreCase(requestConnectionHeader)) {
            // In all cases, if we have a close connection option then we SHOULD NOT treat the connection as persistent
            close = true;
        } else if (protocolVersion == io.vertx.core.http.HttpVersion.HTTP_1_0 && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader)) {
            // In the HTTP/1.0 case both request/response need a keep-alive connection header the connection to be persistent
            // currently Vertx forces the Connection header if keepalive is enabled for 1.0
            close = true;
        }
        if (close) {
            pool.responseEnded(this, true);
        } else {
            if (reset) {
                if (requests.isEmpty()) {
                    pool.responseEnded(this, true);
                }
            } else {
                pool.responseEnded(this, false);
            }
        }
    }
    currentResponse = null;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpVersion(io.vertx.core.http.HttpVersion) io.vertx.core.http(io.vertx.core.http) io.netty.handler.codec.http(io.netty.handler.codec.http)

Example 52 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class StreamsExamples method pump2.

public void pump2(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            if (!sock.writeQueueFull()) {
                sock.write(buffer);
            }
        });
    }).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 53 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class ConnectionHolder method writeMessage.

// TODO optimise this (contention on monitor)
synchronized void writeMessage(ClusteredMessage message) {
    if (connected) {
        Buffer data = message.encodeToWire();
        metrics.messageWritten(message.address(), data.length());
        socket.write(data);
    } else {
        if (pending == null) {
            pending = new ArrayDeque<>();
        }
        pending.add(message);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer)

Example 54 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class SharedDataExamples method example1.

public void example1(Vertx vertx) {
    SharedData sd = vertx.sharedData();
    LocalMap<String, String> map1 = sd.getLocalMap("mymap1");
    // Strings are immutable so no need to copy
    map1.put("foo", "bar");
    LocalMap<String, Buffer> map2 = sd.getLocalMap("mymap2");
    // This buffer will be copied before adding to map
    map2.put("eek", Buffer.buffer().appendInt(123));
    // Then... in another part of your application:
    map1 = sd.getLocalMap("mymap1");
    String val = map1.get("foo");
    map2 = sd.getLocalMap("mymap2");
    Buffer buff = map2.get("eek");
}
Also used : Buffer(io.vertx.core.buffer.Buffer)

Example 55 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class StreamsExamples method pump1.

public void pump1(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            sock.write(buffer);
        });
    }).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

Buffer (io.vertx.core.buffer.Buffer)1052 Test (org.junit.Test)604 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)256 Future (io.vertx.core.Future)253 AtomicReference (java.util.concurrent.atomic.AtomicReference)237 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 Handler (io.vertx.core.Handler)230 Vertx (io.vertx.core.Vertx)185 TestUtils (io.vertx.test.core.TestUtils)177 JsonObject (io.vertx.core.json.JsonObject)172 TimeUnit (java.util.concurrent.TimeUnit)161 CountDownLatch (java.util.concurrent.CountDownLatch)153 List (java.util.List)152 Consumer (java.util.function.Consumer)149 Function (java.util.function.Function)142 ReadStream (io.vertx.core.streams.ReadStream)139 Context (io.vertx.core.Context)135 ArrayList (java.util.ArrayList)132 MultiMap (io.vertx.core.MultiMap)130 Assume (org.junit.Assume)130