FOTA v.0.9.2.7版本的 MD5 值为:2305fdf4520259063967289da5df0dc6
以下内容为反编译后的 UdpManager.java 源代码,内容仅作参考
package com.incall.apps.protocol.datalink.socket;
import com.incall.apps.libbase.utils.StringUtil;
import com.incall.apps.protocol.datalink.DataLinkManager;
import com.incall.apps.protocol.util.ByteUtils;
import com.incall.apps.softmanager.util.ScheduleExecutorHelper;
import com.incall.apps.softmanager.util.log.LogUtil;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class UdpManager {
private static final String TAG = "PROTOCOL.UdpManager";
private static UdpManager mInstance;
private ScheduleExecutorHelper mConnectReceiveExecutor;
private DatagramChannel mDatagramChannel;
private Selector mUdpSelector;
private AtomicBoolean mIsReceiveNotification = new AtomicBoolean(false);
private List<SocketManager> socketManagers = new ArrayList();
public static synchronized UdpManager getInstance() {
UdpManager udpManager;
synchronized (UdpManager.class) {
if (mInstance == null) {
mInstance = new UdpManager();
}
udpManager = mInstance;
}
return udpManager;
}
private synchronized void connectUdp() {
try {
if (this.mDatagramChannel == null) {
LogUtil.i(TAG, "start connectUdp");
DatagramChannel open = DatagramChannel.open(StandardProtocolFamily.INET);
this.mDatagramChannel = open;
open.bind((SocketAddress) new InetSocketAddress("172.16.6.50", SocketConfig.UDP_PORT));
this.mDatagramChannel.configureBlocking(false);
Selector open2 = Selector.open();
this.mUdpSelector = open2;
this.mDatagramChannel.register(open2, 1);
}
} catch (Exception e) {
e.printStackTrace();
LogUtil.e(TAG, "connectUdp:" + e);
}
}
public synchronized boolean sendUdpData(byte[] data, SocketConfig config) {
if (!StringUtil.isEmpty(config.getHost()) && !"SMA_EDC_A_ANDROID".equalsIgnoreCase(config.getSubMaster())) {
SocketManager socketManager = (SocketManager) DataLinkManager.getDataLink(config.getSubMaster()).getSourceManager();
this.socketManagers.add(socketManager);
connectUdp();
ByteBuffer writeBuffer = null;
try {
LogUtil.i(TAG, "start sendUdpData:" + ByteUtils.bytes2HexString(data));
writeBuffer = ByteBuffer.allocate(SocketConfig.validDataSizeOfSegmentForEth + 20480);
writeBuffer.put(data);
writeBuffer.flip();
LogUtil.i(TAG, "send host:" + config.getHost());
this.mDatagramChannel.send(writeBuffer, new InetSocketAddress(config.getHost(), SocketConfig.UDP_PORT));
if (writeBuffer != null) {
writeBuffer.clear();
}
return true;
} catch (Exception e) {
e.printStackTrace();
LogUtil.e(TAG, "sendUdpData error:" + e);
if (writeBuffer != null) {
writeBuffer.clear();
}
return false;
}
}
return false;
}
public synchronized boolean sendUdpDataWithSocketAddress(String ip, int port, byte[] data) {
LogUtil.i(TAG, "sendUdpDataWithSocketAddress: ip = " + ip);
if (StringUtil.isEmpty(ip)) {
return false;
}
connectUdp();
ByteBuffer writeBuffer = null;
try {
LogUtil.i(TAG, "sendUdpDataWithSocketAddress:" + ByteUtils.bytes2HexString(data));
writeBuffer = ByteBuffer.allocate(SocketConfig.validDataSizeOfSegmentForEth + 20480);
writeBuffer.put(data);
writeBuffer.flip();
this.mDatagramChannel.send(writeBuffer, new InetSocketAddress(ip, port));
if (writeBuffer != null) {
writeBuffer.clear();
}
return true;
} catch (Exception e) {
e.printStackTrace();
LogUtil.e(TAG, "sendUdpDataWithSocketAddress error:" + e);
if (writeBuffer != null) {
writeBuffer.clear();
}
return false;
}
}
public synchronized void startReceiveNotification() {
this.mIsReceiveNotification.set(true);
connectUdp();
if (this.mConnectReceiveExecutor == null) {
ScheduleExecutorHelper scheduleExecutorHelper = new ScheduleExecutorHelper();
this.mConnectReceiveExecutor = scheduleExecutorHelper;
scheduleExecutorHelper.schedule(new Runnable() {
@Override
public void run() {
while (UdpManager.this.mIsReceiveNotification.get()) {
try {
} catch (Exception e) {
e.printStackTrace();
LogUtil.e(UdpManager.TAG, "startReceiveConnectData:" + e);
UdpManager.this.mIsReceiveNotification.set(false);
UdpManager.this.mConnectReceiveExecutor.shutdown();
UdpManager.this.mConnectReceiveExecutor = null;
}
if (UdpManager.this.mUdpSelector != null) {
UdpManager.this.mUdpSelector.select();
Iterator<SelectionKey> ite = UdpManager.this.mUdpSelector.selectedKeys().iterator();
while (ite.hasNext()) {
SelectionKey key = ite.next();
ite.remove();
if (key.isReadable()) {
UdpManager.this.readUdpData();
}
}
} else {
throw new Exception("mUdpSelector is null");
break;
}
}
}
}, 0L);
}
}
public synchronized void readUdpData() {
try {
ByteBuffer readBuffer = ByteBuffer.allocate(SocketConfig.validDataSizeOfSegmentForEth + 20480);
InetSocketAddress isa = (InetSocketAddress) this.mDatagramChannel.receive(readBuffer);
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
LogUtil.i(TAG, "readUdpData:" + ByteUtils.bytes2HexString(bytes));
for (SocketManager socketManager : this.socketManagers) {
if (isa != null && isa.getAddress() != null && socketManager.getSocketConfig().getHost().equals(isa.getAddress().getHostAddress())) {
socketManager.receiveProtocolMessage(bytes);
}
}
if (isa != null && isa.getAddress() != null) {
LogUtil.i(TAG, "readUdpData: from IP = " + isa.getAddress().getHostAddress() + " port = " + isa.getPort());
NotSubMasterMsgUdpManager.getInstance().receiveProtocolMessage(bytes, isa.getAddress().getHostAddress(), isa.getPort());
} else {
LogUtil.i(TAG, "readUdpData: isa == null");
}
} catch (Exception e) {
e.printStackTrace();
LogUtil.e(TAG, "readUdpData error:" + e);
}
}
public synchronized void destroy() {
this.mIsReceiveNotification.set(false);
ScheduleExecutorHelper scheduleExecutorHelper = this.mConnectReceiveExecutor;
if (scheduleExecutorHelper != null) {
scheduleExecutorHelper.shutdown();
this.mIsReceiveNotification = null;
}
try {
DatagramChannel datagramChannel = this.mDatagramChannel;
if (datagramChannel != null) {
datagramChannel.close();
this.mDatagramChannel = null;
}
Selector selector = this.mUdpSelector;
if (selector != null) {
selector.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}