Search in sources :

Example 1 with FixedAtomicInteger

use of com.generallycloud.baseio.concurrent.FixedAtomicInteger in project baseio by generallycloud.

the class AbstractSocketChannelContext method createChannelIdsSequence.

private void createChannelIdsSequence() {
    int core_size = serverConfiguration.getSERVER_CORE_SIZE();
    int max = (Integer.MAX_VALUE / core_size) * core_size - 1;
    this.CHANNEL_ID = new FixedAtomicInteger(0, max);
}
Also used : FixedAtomicInteger(com.generallycloud.baseio.concurrent.FixedAtomicInteger)

Example 2 with FixedAtomicInteger

use of com.generallycloud.baseio.concurrent.FixedAtomicInteger in project baseio by generallycloud.

the class AioSocketChannelAcceptor method bind.

@Override
protected void bind(InetSocketAddress socketAddress) throws IOException {
    AioSocketChannelContext context = (AioSocketChannelContext) getContext();
    AsynchronousChannelGroup group = context.getAsynchronousChannelGroup();
    final FixedAtomicInteger channelIds = new FixedAtomicInteger(1, Integer.MAX_VALUE);
    serverSocketChannel = AsynchronousServerSocketChannel.open(group);
    serverSocketChannel.bind(socketAddress);
    serverSocketChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {

        @Override
        public void completed(AsynchronousSocketChannel _channel, Void attachment) {
            // 接受下一个连接
            serverSocketChannel.accept(null, this);
            int channelId = channelIds.getAndIncrement();
            CachedAioThread aioThread = (CachedAioThread) Thread.currentThread();
            AioSocketChannel channel = new AioSocketChannel(aioThread, _channel, channelId);
            channel.fireOpend();
            aioThread.getReadCompletionHandler().completed(0, channel);
        }

        @Override
        public void failed(Throwable exc, Void attachment) {
            logger.error(exc.getMessage(), exc);
        }
    });
}
Also used : AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) CachedAioThread(com.generallycloud.baseio.component.CachedAioThread) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) FixedAtomicInteger(com.generallycloud.baseio.concurrent.FixedAtomicInteger) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) AioSocketChannel(com.generallycloud.baseio.component.AioSocketChannel)

Aggregations

FixedAtomicInteger (com.generallycloud.baseio.concurrent.FixedAtomicInteger)2 AioSocketChannel (com.generallycloud.baseio.component.AioSocketChannel)1 AioSocketChannelContext (com.generallycloud.baseio.component.AioSocketChannelContext)1 CachedAioThread (com.generallycloud.baseio.component.CachedAioThread)1 AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1