下面列出了java.net.SocketAddress#hashCode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 去读数据
*
* @param channel
* @throws IOException
*/
private void doRead(VirtualBuffer readBuffer, UdpChannel channel) throws IOException {
int count = MAX_READ_TIMES;
while (count-- > 0) {
//接收数据
ByteBuffer buffer = readBuffer.buffer();
buffer.clear();
//The datagram's source address,
// or null if this channel is in non-blocking mode and no datagram was immediately available
SocketAddress remote = channel.getChannel().receive(buffer);
if (remote == null) {
return;
}
buffer.flip();
UdpAioSession<Request> aioSession = channel.createAndCacheSession(remote);
config.getMonitor().beforeRead(aioSession);
config.getMonitor().afterRead(aioSession, buffer.remaining());
Request request = null;
//解码
try {
request = config.getProtocol().decode(buffer, aioSession);
} catch (Exception e) {
config.getProcessor().stateEvent(aioSession, StateMachineEnum.DECODE_EXCEPTION, e);
aioSession.close();
throw e;
}
//理论上每个UDP包都是一个完整的消息
if (request == null) {
config.getProcessor().stateEvent(aioSession, StateMachineEnum.DECODE_EXCEPTION, new DecoderException("decode result is null"));
return;
}
// LOGGER.info("receive:{} from:{}", request, remote);
//任务分发
int hashCode = remote.hashCode();
if (hashCode < 0) {
hashCode = -hashCode;
}
UdpDispatcher dispatcher = workerGroup[hashCode % workerGroup.length];
dispatcher.dispatch(aioSession, request);
}
}
@Override
public int compare(SocketAddress o1, SocketAddress o2) {
return o2.hashCode() - o1.hashCode();
}