You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/09/12 08:07:45 UTC

[httpcomponents-core] 02/03: Protocol handlers to use IOSession for read and write operations instead of the underlying network channel

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 367d5e34472ce086b1e836e0a8dd93bb93b92250
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Sep 10 12:01:35 2019 +0200

    Protocol handlers to use IOSession for read and write operations instead of the underlying network channel
---
 .../http2/impl/nio/AbstractH2StreamMultiplexer.java      | 10 +++++-----
 .../http2/impl/nio/ClientHttpProtocolNegotiator.java     |  2 +-
 .../http2/impl/nio/H2OnlyClientProtocolNegotiator.java   |  2 +-
 .../http2/impl/nio/ServerHttpProtocolNegotiator.java     |  2 +-
 .../org/apache/hc/core5/benchmark/HttpBenchmark.java     |  2 +-
 .../apache/hc/core5/testing/nio/LoggingIOSession.java    |  2 +-
 .../core5/http/impl/nio/AbstractHttp1StreamDuplexer.java | 12 ++++++------
 .../hc/core5/reactor/SocksProxyProtocolHandler.java      | 16 ++++++++--------
 .../org/apache/hc/core5/reactor/ssl/SSLIOSession.java    |  8 ++++----
 9 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
index e9b0690..fdfa55d 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
@@ -233,7 +233,7 @@ abstract class AbstractH2StreamMultiplexer implements Identifiable, HttpConnecti
             if (streamListener != null) {
                 streamListener.onFrameOutput(this, frame.getStreamId(), frame);
             }
-            outputBuffer.write(frame, ioSession.channel());
+            outputBuffer.write(frame, ioSession);
         } else {
             outputQueue.addLast(frame);
         }
@@ -331,7 +331,7 @@ abstract class AbstractH2StreamMultiplexer implements Identifiable, HttpConnecti
         }
         updateOutputWindow(0, connOutputWindow, -chunk);
         updateOutputWindow(streamId, streamOutputWindow, -chunk);
-        outputBuffer.write(dataFrame, ioSession.channel());
+        outputBuffer.write(dataFrame, ioSession);
     }
 
     private int streamData(
@@ -420,7 +420,7 @@ abstract class AbstractH2StreamMultiplexer implements Identifiable, HttpConnecti
             ioSession.clearEvent(SelectionKey.OP_READ);
         } else {
             RawFrame frame;
-            while ((frame = inputBuffer.read(ioSession.channel())) != null) {
+            while ((frame = inputBuffer.read(ioSession)) != null) {
                 if (streamListener != null) {
                     streamListener.onFrameInput(this, frame.getStreamId(), frame);
                 }
@@ -433,7 +433,7 @@ abstract class AbstractH2StreamMultiplexer implements Identifiable, HttpConnecti
         ioSession.getLock().lock();
         try {
             if (!outputBuffer.isEmpty()) {
-                outputBuffer.flush(ioSession.channel());
+                outputBuffer.flush(ioSession);
             }
             while (outputBuffer.isEmpty()) {
                 final RawFrame frame = outputQueue.poll();
@@ -441,7 +441,7 @@ abstract class AbstractH2StreamMultiplexer implements Identifiable, HttpConnecti
                     if (streamListener != null) {
                         streamListener.onFrameOutput(this, frame.getStreamId(), frame);
                     }
-                    outputBuffer.write(frame, ioSession.channel());
+                    outputBuffer.write(frame, ioSession);
                 } else {
                     break;
                 }
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
index 2c0f6b6..8e6e346 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
@@ -118,7 +118,7 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
 
     private void writeOutPreface(final IOSession session) throws IOException {
         if (preface.hasRemaining()) {
-            final ByteChannel channel = session.channel();
+            final ByteChannel channel = session;
             channel.write(preface);
         }
         if (!preface.hasRemaining()) {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java
index 3809d52..0decbf0 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java
@@ -102,7 +102,7 @@ public class H2OnlyClientProtocolNegotiator implements HttpConnectionEventHandle
 
     private void writePreface(final IOSession session) throws IOException  {
         if (preface.hasRemaining()) {
-            final ByteChannel channel = session.channel();
+            final ByteChannel channel = session;
             channel.write(preface);
         }
         if (!preface.hasRemaining()) {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
index a2776a1..157efe1 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
@@ -126,7 +126,7 @@ public class ServerHttpProtocolNegotiator implements HttpConnectionEventHandler
         try {
             boolean endOfStream = false;
             if (bytebuf.position() < PREFACE.length) {
-                final int bytesRead = session.channel().read(bytebuf);
+                final int bytesRead = session.read(bytebuf);
                 if (bytesRead == -1) {
                     endOfStream = true;
                 }
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
index e055718..3e572ef 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
@@ -211,7 +211,7 @@ public class HttpBenchmark {
 
                             @Override
                             public ByteChannel channel() {
-                                return this;
+                                return ioSession.channel();
                             }
 
                             @Override
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
index f1b4fd3..79fae8a 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
@@ -96,7 +96,7 @@ public class LoggingIOSession implements ProtocolIOSession {
 
     @Override
     public ByteChannel channel() {
-        return this;
+        return this.session.channel();
     }
 
     @Override
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
index 5a3eda1..3b21997 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
@@ -250,7 +250,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
 
                 int bytesRead;
                 do {
-                    bytesRead = inbuf.fill(ioSession.channel());
+                    bytesRead = inbuf.fill(ioSession);
                     if (bytesRead > 0) {
                         totalBytesRead += bytesRead;
                         inTransportMetrics.incrementBytesTransferred(bytesRead);
@@ -266,7 +266,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                         final ContentDecoder contentDecoder;
                         if (handleIncomingMessage(messageHead)) {
                             final long len = incomingContentStrategy.determineLength(messageHead);
-                            contentDecoder = createContentDecoder(len, ioSession.channel(), inbuf, inTransportMetrics);
+                            contentDecoder = createContentDecoder(len, ioSession, inbuf, inTransportMetrics);
                             consumeHeader(messageHead, contentDecoder != null ? new IncomingEntityDetails(messageHead, len) : null);
                         } else {
                             consumeHeader(messageHead, null);
@@ -335,7 +335,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
         ioSession.getLock().lock();
         try {
             if (outbuf.hasData()) {
-                final int bytesWritten = outbuf.flush(ioSession.channel());
+                final int bytesWritten = outbuf.flush(ioSession);
                 if (bytesWritten > 0) {
                     outTransportMetrics.incrementBytesTransferred(bytesWritten);
                 }
@@ -415,7 +415,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                 final ContentEncoder contentEncoder;
                 if (handleOutgoingMessage(messageHead)) {
                     final long len = outgoingContentStrategy.determineLength(messageHead);
-                    contentEncoder = createContentEncoder(len, ioSession.channel(), outbuf, outTransportMetrics);
+                    contentEncoder = createContentEncoder(len, ioSession, outbuf, outTransportMetrics);
                 } else {
                     contentEncoder = null;
                 }
@@ -425,7 +425,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
             }
             outgoingMessageWriter.reset();
             if (flushMode == FlushMode.IMMEDIATE) {
-                outbuf.flush(ioSession.channel());
+                outbuf.flush(ioSession);
             }
             ioSession.setEvent(EventMask.WRITE);
         } finally {
@@ -454,7 +454,7 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
         ioSession.getLock().lock();
         try {
             if (outbuf.hasData()) {
-                outbuf.flush(ioSession.channel());
+                outbuf.flush(ioSession);
             } else {
                 ioSession.clearEvent(SelectionKey.OP_WRITE);
             }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java
index a848228..ed55892 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SocksProxyProtocolHandler.java
@@ -108,19 +108,19 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
     public void outputReady(final IOSession session) throws IOException {
         switch (this.state) {
             case SEND_AUTH:
-                if (writeAndPrepareRead(session.channel(), 2)) {
+                if (writeAndPrepareRead(session, 2)) {
                     session.setEventMask(SelectionKey.OP_READ);
                     this.state = State.RECEIVE_AUTH_METHOD;
                 }
                 break;
             case SEND_USERNAME_PASSWORD:
-                if (writeAndPrepareRead(session.channel(), 2)) {
+                if (writeAndPrepareRead(session, 2)) {
                     session.setEventMask(SelectionKey.OP_READ);
                     this.state = State.RECEIVE_AUTH;
                 }
                 break;
             case SEND_CONNECT:
-                if (writeAndPrepareRead(session.channel(), 2)) {
+                if (writeAndPrepareRead(session, 2)) {
                     session.setEventMask(SelectionKey.OP_READ);
                     this.state = State.RECEIVE_RESPONSE_CODE;
                 }
@@ -141,7 +141,7 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
     public void inputReady(final IOSession session) throws IOException {
         switch (this.state) {
             case RECEIVE_AUTH_METHOD:
-                if (fillBuffer(session.channel())) {
+                if (fillBuffer(session)) {
                     this.buffer.flip();
                     final byte serverVersion = this.buffer.get();
                     final byte serverMethod = this.buffer.get();
@@ -168,7 +168,7 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
                 }
                 break;
             case RECEIVE_AUTH:
-                if (fillBuffer(session.channel())) {
+                if (fillBuffer(session)) {
                     this.buffer.flip();
                     this.buffer.get(); // skip server auth version
                     final byte status = this.buffer.get();
@@ -181,7 +181,7 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
                 }
                 break;
             case RECEIVE_RESPONSE_CODE:
-                if (fillBuffer(session.channel())) {
+                if (fillBuffer(session)) {
                     this.buffer.flip();
                     final byte serverVersion = this.buffer.get();
                     final byte responseCode = this.buffer.get();
@@ -199,7 +199,7 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
                     break;
                 }
             case RECEIVE_ADDRESS_TYPE:
-                if (fillBuffer(session.channel())) {
+                if (fillBuffer(session)) {
                     this.buffer.flip();
                     this.buffer.get(); // reserved byte that has no purpose
                     final byte aType = this.buffer.get();
@@ -224,7 +224,7 @@ final class SocksProxyProtocolHandler implements IOEventHandler {
                     break;
                 }
             case RECEIVE_ADDRESS:
-                if (fillBuffer(session.channel())) {
+                if (fillBuffer(session)) {
                     this.buffer.clear();
                     this.state = State.COMPLETE;
                     final IOEventHandler newHandler = this.eventHandlerFactory.createHandler(this.ioSession, this.attachment);
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
index e75da59..6b54fcb 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
@@ -402,7 +402,7 @@ public class SSLIOSession implements IOSession {
             // This will ensure that tests performed by write() still take place without
             // having to acquire and release an empty buffer (e.g. connection closed,
             // interrupted thread, etc..)
-            return this.session.channel().write(EMPTY_BUFFER);
+            return this.session.write(EMPTY_BUFFER);
         }
 
         // Acquire buffer
@@ -413,7 +413,7 @@ public class SSLIOSession implements IOSession {
         if (outEncryptedBuf.position() > 0) {
             outEncryptedBuf.flip();
             try {
-                bytesWritten = this.session.channel().write(outEncryptedBuf);
+                bytesWritten = this.session.write(outEncryptedBuf);
             } finally {
                 outEncryptedBuf.compact();
             }
@@ -435,7 +435,7 @@ public class SSLIOSession implements IOSession {
         final ByteBuffer inEncryptedBuf = this.inEncrypted.acquire();
 
         // Perform operation
-        final int bytesRead = this.session.channel().read(inEncryptedBuf);
+        final int bytesRead = this.session.read(inEncryptedBuf);
 
         // Release if empty
         if (inEncryptedBuf.position() == 0) {
@@ -725,7 +725,7 @@ public class SSLIOSession implements IOSession {
 
     @Override
     public ByteChannel channel() {
-        return this;
+        return this.session.channel();
     }
 
     @Override