Search in sources :

Example 1 with CharBasedProtocolFactory

use of com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory 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)

Example 2 with CharBasedProtocolFactory

use of com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory in project baseio by generallycloud.

the class TestLineBasedClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new CharBasedProtocolFactory());
    SocketSession session = connector.connect();
    CharBasedFuture future = new CharBasedFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) CharBasedFutureImpl(com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Example 3 with CharBasedProtocolFactory

use of com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory in project baseio by generallycloud.

the class TestLineBasedServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            String res = "yes server already accept your message:" + future.getReadText();
            future.write(res);
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    File certificate = FileUtil.readFileByCls("generallycloud.com.crt");
    File privateKey = FileUtil.readFileByCls("generallycloud.com.key");
    SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
    context.setSslContext(sslContext);
    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) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)3 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)3 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)3 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)3 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)3 SocketSession (com.generallycloud.baseio.component.SocketSession)3 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)3 Future (com.generallycloud.baseio.protocol.Future)3 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)2 ChannelAcceptor (com.generallycloud.baseio.acceptor.ChannelAcceptor)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 CharBasedFutureImpl (com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl)1 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)1 File (java.io.File)1