云服务 v8.0.9版本的 MD5 值为:547a053af090cf9044d8e9e53f35e8e3
以下内容为反编译后的 UdpNioReadWriteProcessor.java 源代码,内容仅作参考
package com.open.net.client.impl.udp.nio.processor;
import com.open.net.client.impl.udp.nio.UdpNioClient;
import com.open.net.client.impl.udp.nio.UdpNioConnectListener;
import com.open.net.client.structures.BaseClient;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
public final class UdpNioReadWriteProcessor {
private static int G_SOCKET_ID;
private BaseClient mClient;
private ConnectRunnable mConnectProcessor;
private String mIp;
private UdpNioConnectListener mNioConnectListener;
private int mPort;
private Selector mSelector;
private DatagramChannel mSocketChannel;
private int mSocketId;
private String TAG = "UdpNioReadWriteProcessor";
private Thread mConnectThread = null;
private boolean closed = false;
public UdpNioReadWriteProcessor(String str, int i, BaseClient baseClient, UdpNioConnectListener udpNioConnectListener) {
this.mIp = "192.168.1.1";
this.mPort = 9999;
int i2 = G_SOCKET_ID + 1;
G_SOCKET_ID = i2;
this.mSocketId = i2;
this.mIp = str;
this.mPort = i;
this.mClient = baseClient;
this.mNioConnectListener = udpNioConnectListener;
}
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;
DatagramChannel datagramChannel = this.mSocketChannel;
if (datagramChannel != null) {
try {
SelectionKey keyFor = datagramChannel.keyFor(this.mSelector);
if (keyFor != null) {
keyFor.cancel();
}
this.mSelector.close();
this.mSocketChannel.socket().close();
this.mSocketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
this.mSocketChannel = null;
this.mSelector = null;
wakeUp();
}
public void wakeUp() {
ConnectRunnable connectRunnable = this.mConnectProcessor;
if (connectRunnable != null) {
connectRunnable.wakeUp();
}
}
public void onSocketExit(int i) {
close();
System.out.println(this.TAG + "onSocketExit mSocketId " + this.mSocketId + " exit_code " + i);
UdpNioConnectListener udpNioConnectListener = this.mNioConnectListener;
if (udpNioConnectListener != null) {
udpNioConnectListener.onConnectFailed(this);
}
}
public class ConnectRunnable implements Runnable {
private ConnectRunnable() {
}
public void wakeUp() {
if (UdpNioReadWriteProcessor.this.mSelector != null) {
UdpNioReadWriteProcessor.this.mSelector.wakeup();
}
}
@Override
public void run() {
try {
UdpNioReadWriteProcessor.this.mSelector = Selector.open();
UdpNioReadWriteProcessor.this.mSocketChannel = DatagramChannel.open();
boolean z = false;
UdpNioReadWriteProcessor.this.mSocketChannel.configureBlocking(false);
UdpNioReadWriteProcessor.this.mSocketChannel.connect(new InetSocketAddress(UdpNioReadWriteProcessor.this.mIp, UdpNioReadWriteProcessor.this.mPort));
UdpNioReadWriteProcessor.this.mSocketChannel.register(UdpNioReadWriteProcessor.this.mSelector, 1, UdpNioReadWriteProcessor.this.mClient);
((UdpNioClient) UdpNioReadWriteProcessor.this.mClient).init(UdpNioReadWriteProcessor.this.mSocketChannel);
if (UdpNioReadWriteProcessor.this.mNioConnectListener != null) {
UdpNioConnectListener udpNioConnectListener = UdpNioReadWriteProcessor.this.mNioConnectListener;
UdpNioReadWriteProcessor udpNioReadWriteProcessor = UdpNioReadWriteProcessor.this;
udpNioConnectListener.onConnectSuccess(udpNioReadWriteProcessor, udpNioReadWriteProcessor.mSocketChannel);
}
while (!z) {
if (UdpNioReadWriteProcessor.this.mSelector.select() > 0) {
Iterator<SelectionKey> it = UdpNioReadWriteProcessor.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 || UdpNioReadWriteProcessor.this.closed) {
break;
} else if (!UdpNioReadWriteProcessor.this.mClient.mWriteMessageQueen.mWriteQueen.isEmpty()) {
UdpNioReadWriteProcessor.this.mSocketChannel.keyFor(UdpNioReadWriteProcessor.this.mSelector).interestOps(4);
}
}
} catch (Exception e) {
e.printStackTrace();
}
UdpNioReadWriteProcessor.this.onSocketExit(1);
}
}
}