Search in sources :

Example 1 with ChannelAcceptor

use of com.generallycloud.baseio.acceptor.ChannelAcceptor in project baseio by generallycloud.

the class SocketSelectorEventLoop method rebuildSelector0.

private SocketSelector rebuildSelector0() throws IOException {
    SocketChannelContext context = getChannelContext();
    NioChannelService nioChannelService = (NioChannelService) context.getChannelService();
    SelectableChannel channel = nioChannelService.getSelectableChannel();
    SocketSelector selector = openSelector(channel);
    if (nioChannelService instanceof ChannelAcceptor) {
        if (isMainEventLoop()) {
            channel.register(selector.getSelector(), SelectionKey.OP_ACCEPT);
        }
    } else {
        channel.register(selector.getSelector(), SelectionKey.OP_CONNECT);
    }
    return selector;
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel) ChannelAcceptor(com.generallycloud.baseio.acceptor.ChannelAcceptor)

Example 2 with ChannelAcceptor

use of com.generallycloud.baseio.acceptor.ChannelAcceptor in project baseio by generallycloud.

the class TestLineBasedBroadcastServer method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            long old = System.currentTimeMillis();
            String res = "hello world!";
            future.write(res);
            ChannelAcceptor acceptor = (ChannelAcceptor) session.getContext().getChannelService();
            acceptor.broadcast(future);
            long now = System.currentTimeMillis();
            System.out.println("广播花费时间:" + (now - old) + ",连接数:" + session.getContext().getSessionManager().getManagedSessionSize());
        }
    };
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_PORT(18300);
    configuration.setSERVER_SESSION_IDLE_TIME(180000);
    configuration.setSERVER_MEMORY_POOL_CAPACITY(1024 * 512);
    configuration.setSERVER_MEMORY_POOL_UNIT(64);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) ChannelAcceptor(com.generallycloud.baseio.acceptor.ChannelAcceptor) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)

Aggregations

ChannelAcceptor (com.generallycloud.baseio.acceptor.ChannelAcceptor)2 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1 CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)1 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)1 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 Future (com.generallycloud.baseio.protocol.Future)1 SelectableChannel (java.nio.channels.SelectableChannel)1