use of com.generallycloud.baseio.component.CachedAioThread in project baseio by generallycloud.
the class AioSocketChannelConnector method connect.
@Override
protected void connect(InetSocketAddress socketAddress) throws IOException {
AsynchronousChannelGroup group = context.getAsynchronousChannelGroup();
final AsynchronousSocketChannel _channel = AsynchronousSocketChannel.open(group);
_channel.connect(socketAddress, this, new CompletionHandler<Void, AioSocketChannelConnector>() {
@Override
public void completed(Void result, AioSocketChannelConnector connector) {
CachedAioThread aioThread = (CachedAioThread) Thread.currentThread();
AioSocketChannel channel = new AioSocketChannel(aioThread, _channel, 1);
connector.finishConnect(channel.getSession(), null);
aioThread.getReadCompletionHandler().completed(0, channel);
}
@Override
public void failed(Throwable exc, AioSocketChannelConnector connector) {
connector.finishConnect((UnsafeSocketSession) getSession(), exc);
}
});
wait4connect();
}
use of com.generallycloud.baseio.component.CachedAioThread 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);
}
});
}
Aggregations