Java Code Examples for java.net.SocketAddress#hashCode()

The following examples show how to use java.net.SocketAddress#hashCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UdpBootstrap.java    From smart-socket with Apache License 2.0 4 votes vote down vote up
/**
     * 去读数据
     *
     * @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);
        }
    }
 
Example 2
Source File: IoSessionFactory.java    From cougar with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(SocketAddress o1, SocketAddress o2) {
    return o2.hashCode() - o1.hashCode();
}