handler = this.readCompletionHandler;
- ByteBuffer attach = this.readByteBuffer;
- //清空读参数
- this.readCompletionHandler = null;
- this.readByteBuffer = null;
- this.readPending = false; //必须放最后
-
- if (handler == null) {
- if (t == null) {
- protocolCodec.completed(totalCount, attach);
- } else {
- protocolCodec.failed(t, attach);
- }
- } else {
- if (t == null) {
- handler.completed(totalCount, attach);
- } else {
- handler.failed(t, attach);
- }
- }
- }
-
- protected void handleWrite(final int totalCount, Throwable t) {
- CompletionHandler handler = this.writeCompletionHandler;
- Object attach = this.writeAttachment;
- //清空写参数
- this.writeCompletionHandler = null;
- this.writeAttachment = null;
- this.writeByteBuffer = null;
- this.writeByteBuffers = null;
- this.writeOffset = 0;
- this.writeLength = 0;
- this.writeTotal = 0;
- this.writePending = false; //必须放最后
-
- if (t == null) {
- handler.completed(totalCount, attach);
- } else {
- handler.failed(t, attach);
- }
- }
-
- @Deprecated(since = "2.5.0")
- protected abstract ReadableByteChannel readableByteChannel();
-
- @Deprecated(since = "2.5.0")
- protected abstract WritableByteChannel writableByteChannel();
-
- protected InputStream newInputStream() {
- final ReadableByteChannel reader = readableByteChannel();
- return new InputStream() {
-
- ByteBuffer bb;
-
- int count;
-
- @Override
- public synchronized int read() throws IOException {
- if (bb == null || !bb.hasRemaining()) {
- int r = readBuffer();
- if (r < 1) {
- return -1;
- }
- }
- return bb.get() & 0xff;
- }
-
- @Override
- public synchronized int read(byte b[], int off, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if (off < 0 || len < 0 || len > b.length - off) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- }
- if (bb == null || !bb.hasRemaining()) {
- int r = readBuffer();
- if (r < 1) {
- return -1;
- }
- }
- int size = Math.min(b.length, Math.min(len, bb.remaining()));
- bb.get(b, off, size);
- return size;
- }
-
- @Override
- public void close() throws IOException {
- if (bb != null) {
- offerReadBuffer(bb);
- bb = null;
- }
- reader.close();
- }
-
- @Override
- public int available() throws IOException {
- if (bb == null || !bb.hasRemaining()) {
- return 0;
- }
- return bb.remaining();
- }
-
- private int readBuffer() throws IOException {
- if (bb == null) {
- bb = pollReadBuffer();
- } else {
- bb.clear();
- }
- try {
- int size = reader.read(bb);
- bb.flip();
- return size;
- } catch (IOException ioe) {
- throw ioe;
- } catch (Exception e) {
- throw new IOException(e);
- }
-
- }
-
- };
- }
-
- protected abstract SelectionKey implRegister(Selector sel, int ops) throws ClosedChannelException;
-
- protected abstract int implRead(ByteBuffer dst) throws IOException;
-
- protected abstract int implWrite(ByteBuffer src) throws IOException;
-
- protected abstract int implWrite(ByteBuffer[] srcs, int offset, int length) throws IOException;
-
- public abstract boolean isConnected();
-
- public abstract void doConnect();
-}
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.redkale.net;
+
+import java.io.*;
+import java.net.SocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.*;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import javax.net.ssl.SSLContext;
+import org.redkale.util.ByteBufferWriter;
+
+/**
+ *
+ *
+ * 详情见: https://redkale.org
+ *
+ * @author zhangjx
+ *
+ * @since 2.3.0
+ */
+abstract class AsyncNioConnection extends AsyncConnection {
+
+ final AsyncIOThread connectThread;
+
+ protected SocketAddress remoteAddress;
+
+ //-------------------------------- 连操作 --------------------------------------
+ protected Object connectAttachment;
+
+ protected CompletionHandler connectCompletionHandler;
+
+ protected boolean connectPending;
+
+ protected SelectionKey connectKey;
+
+ //-------------------------------- 读操作 --------------------------------------
+ protected final AsyncNioCompletionHandler readTimeoutCompletionHandler = new AsyncNioCompletionHandler<>(true, this);
+
+ protected int readTimeoutSeconds;
+
+ protected ByteBuffer readByteBuffer;
+
+ protected CompletionHandler readCompletionHandler;
+
+ protected boolean readPending;
+
+ protected SelectionKey readKey;
+
+ //-------------------------------- 写操作 --------------------------------------
+ protected final AsyncNioCompletionHandler