云服务 v8.0.9版本的 MD5 值为:8d75f41fe0bd4154db770899f4a201ba

以下内容为反编译后的 NioReadWriteProcessor.java 源代码,内容仅作参考


package com.open.net.client.impl.tcp.nio.processor;

import com.open.net.client.impl.tcp.nio.NioConnectListener;
import com.open.net.client.structures.BaseClient;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;

public final class NioReadWriteProcessor {
    private static int G_SOCKET_ID;
    private long connect_timeout;
    private BaseClient mClient;
    private ConnectRunnable mConnectProcessor;
    private String mIp;
    private NioConnectListener mNioConnectListener;
    private int mPort;
    private Selector mSelector;
    private SocketChannel mSocketChannel;
    private int mSocketId;
    private String TAG = "NioReadWriteProcessor";
    private Thread mConnectThread = null;
    private boolean closed = false;

    public NioReadWriteProcessor(String str, int i, long j, BaseClient baseClient, NioConnectListener nioConnectListener) {
        this.mIp = "192.168.1.1";
        this.mPort = 9999;
        this.connect_timeout = 10000L;
        int i2 = G_SOCKET_ID + 1;
        G_SOCKET_ID = i2;
        this.mSocketId = i2;
        this.mIp = str;
        this.mPort = i;
        this.connect_timeout = j;
        this.mClient = baseClient;
        this.mNioConnectListener = nioConnectListener;
    }

    public void start() {
        this.mConnectProcessor = new ConnectRunnable();
        Thread thread = new Thread(this.mConnectProcessor);
        this.mConnectThread = thread;
        thread.start();
    }

    public synchronized void close() {
        this.closed = true;
        SocketChannel socketChannel = this.mSocketChannel;
        if (socketChannel != null) {
            try {
                SelectionKey keyFor = socketChannel.keyFor(this.mSelector);
                if (keyFor != null) {
                    keyFor.cancel();
                }
                this.mSocketChannel.socket().close();
                this.mSocketChannel.close();
                this.mSocketChannel = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Selector selector = this.mSelector;
        if (selector != null) {
            try {
                selector.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            this.mSelector = null;
        }
        wakeUp();
    }

    public void wakeUp() {
        ConnectRunnable connectRunnable = this.mConnectProcessor;
        if (connectRunnable != null) {
            connectRunnable.wakeUp();
        }
    }

    public void onSocketExit(int i) {
        System.out.println(this.TAG + "onSocketExit mSocketId " + this.mSocketId + " exit_code " + i);
        close();
        NioConnectListener nioConnectListener = this.mNioConnectListener;
        if (nioConnectListener != null) {
            nioConnectListener.onConnectFailed(this);
        }
    }

    public class ConnectRunnable implements Runnable {
        private ConnectRunnable() {
        }

        public void wakeUp() {
            if (NioReadWriteProcessor.this.mSelector != null) {
                NioReadWriteProcessor.this.mSelector.wakeup();
            }
        }

        @Override
        public void run() {
            try {
                NioReadWriteProcessor.this.mSelector = SelectorProvider.provider().openSelector();
                NioReadWriteProcessor.this.mSocketChannel = SocketChannel.open();
                boolean z = false;
                NioReadWriteProcessor.this.mSocketChannel.configureBlocking(false);
                NioReadWriteProcessor.this.mSocketChannel.connect(new InetSocketAddress(NioReadWriteProcessor.this.mIp, NioReadWriteProcessor.this.mPort));
                NioReadWriteProcessor.this.mSocketChannel.register(NioReadWriteProcessor.this.mSelector, 8, NioReadWriteProcessor.this.mClient);
                if (connect(NioReadWriteProcessor.this.connect_timeout)) {
                    while (!z) {
                        if (NioReadWriteProcessor.this.mSelector.select() > 0) {
                            Iterator<SelectionKey> it = NioReadWriteProcessor.this.mSelector.selectedKeys().iterator();
                            while (it.hasNext()) {
                                SelectionKey next = it.next();
                                it.remove();
                                if (next.isValid()) {
                                    if (next.isReadable()) {
                                        if (!((BaseClient) next.attachment()).onRead()) {
                                            next.cancel();
                                            next.attach(null);
                                            next.channel().close();
                                            z = true;
                                            break;
                                        }
                                    } else if (!next.isWritable()) {
                                        continue;
                                    } else {
                                        if (!((BaseClient) next.attachment()).onWrite()) {
                                            next.cancel();
                                            next.attach(null);
                                            next.channel().close();
                                            z = true;
                                            break;
                                        }
                                        next.interestOps(1);
                                    }
                                }
                            }
                        }
                        if (z || NioReadWriteProcessor.this.closed) {
                            break;
                        } else if (!NioReadWriteProcessor.this.mClient.mWriteMessageQueen.mWriteQueen.isEmpty()) {
                            NioReadWriteProcessor.this.mSocketChannel.keyFor(NioReadWriteProcessor.this.mSelector).interestOps(4);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            NioReadWriteProcessor.this.onSocketExit(1);
        }

        private boolean connect(long j) throws IOException {
            boolean z = false;
            if ((j == -1 ? NioReadWriteProcessor.this.mSelector.select() : NioReadWriteProcessor.this.mSelector.select(j)) > 0) {
                Iterator<SelectionKey> it = NioReadWriteProcessor.this.mSelector.selectedKeys().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    SelectionKey next = it.next();
                    it.remove();
                    if (next.isValid() && next.isConnectable() && !(z = finishConnection(next))) {
                        next.cancel();
                        next.attach(null);
                        next.channel().close();
                        break;
                    }
                }
            } else {
                try {
                    for (SelectionKey selectionKey : NioReadWriteProcessor.this.mSelector.keys()) {
                        selectionKey.cancel();
                        selectionKey.attach(null);
                        selectionKey.channel().close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return z;
        }

        private boolean finishConnection(SelectionKey selectionKey) {
            try {
                if (!((SocketChannel) selectionKey.channel()).finishConnect()) {
                    return true;
                }
                selectionKey.interestOps(1);
                if (NioReadWriteProcessor.this.mNioConnectListener == null) {
                    return true;
                }
                NioConnectListener nioConnectListener = NioReadWriteProcessor.this.mNioConnectListener;
                NioReadWriteProcessor nioReadWriteProcessor = NioReadWriteProcessor.this;
                nioConnectListener.onConnectSuccess(nioReadWriteProcessor, nioReadWriteProcessor.mSocketChannel);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}