Search in sources :

Example 1 with ThreadEventLoopGroup

use of com.firenio.concurrent.ThreadEventLoopGroup in project baseio by generallycloud.

the class TestLoadClient1 method prepare.

@Override
public void prepare() throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            addCount(80000);
            if (debug) {
                count.decrementAndGet();
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(5120000 * 256 * CLIENT_CORE_SIZE);
    group.setMemoryUnit(256);
    group.setWriteBuffers(TestLoadServer.WRITE_BUFFERS);
    group.setEnableMemoryPool(TestLoadServer.ENABLE_POOL);
    context = new ChannelConnector(group, "127.0.0.1", 8300);
    context.setIoEventHandle(eventHandleAdaptor);
    if (TestLoadServer.ENABLE_SSL) {
        context.setSslContext(SslContextBuilder.forClient(true).build());
    }
    context.setPrintConfig(false);
    context.addProtocolCodec(new LengthValueCodec());
    if (TestLoadServer.ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256));
    }
    context.connect(6000);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 2 with ThreadEventLoopGroup

use of com.firenio.concurrent.ThreadEventLoopGroup in project baseio by generallycloud.

the class TestLoadServer method main.

public static void main(String[] args) throws Exception {
    Options.setBufAutoExpansion(AUTO_EXPANSION);
    Options.setEnableEpoll(ENABLE_EPOLL);
    Options.setEnableUnsafeBuf(ENABLE_UNSAFE_BUF);
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            String text = f.getStringContent();
            if (BUFFERED_WRITE) {
                ByteBuf buf = ch.getAttribute(WRITE_BUF);
                if (buf == null) {
                    buf = ch.allocate();
                    ByteBuf temp = buf;
                    ch.setAttribute(WRITE_BUF, buf);
                    ch.getEventLoop().submit(() -> {
                        ch.writeAndFlush(temp);
                        ch.setAttribute(WRITE_BUF, null);
                    });
                }
                byte[] data = text.getBytes(ch.getCharset());
                buf.writeInt(data.length);
                buf.writeBytes(data);
            } else {
                f.setString(text, ch);
                ch.writeAndFlush(f);
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup(SERVER_CORE_SIZE);
    group.setMemoryCapacity(1024 * 512 * MEM_UNIT * SERVER_CORE_SIZE);
    group.setWriteBuffers(WRITE_BUFFERS);
    group.setMemoryUnit(MEM_UNIT);
    group.setEnableMemoryPool(ENABLE_POOL);
    ChannelAcceptor context = new ChannelAcceptor(group, 8300);
    context.addProtocolCodec(new LengthValueCodec());
    context.setIoEventHandle(eventHandle);
    if (ENABLE_SSL) {
    // context.setSslPem("localhost.key;localhost.crt");
    }
    context.addChannelEventListener(new LoggerChannelOpenListener());
    if (ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256 * CLIENT_CORE_SIZE));
    }
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ByteBuf(com.firenio.buffer.ByteBuf) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 3 with ThreadEventLoopGroup

use of com.firenio.concurrent.ThreadEventLoopGroup in project baseio by generallycloud.

the class TestHttpBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean prodMode) throws Exception {
    ClassLoader cl = this.getClass().getClassLoader();
    boolean debug = Util.isTrueValue(System.getProperty("http.debug"));
    if (debug) {
        for (; debug; ) {
            Util.sleep(100);
        }
    }
    DevelopConfig.NATIVE_DEBUG = true;
    DevelopConfig.BUF_DEBUG = true;
    DevelopConfig.BUF_PATH_DEBUG = true;
    DevelopConfig.SSL_DEBUG = true;
    DevelopConfig.CHANNEL_DEBUG = true;
    DevelopConfig.DEBUG_ERROR = true;
    Options.setEnableEpoll(true);
    Options.setEnableUnsafe(true);
    Options.setEnableOpenssl(true);
    Options.setBufThreadYield(true);
    // Options.setEnableUnsafeBuf(true);
    HttpDateUtil.start();
    final SpringHttpFrameHandle handle = new SpringHttpFrameHandle();
    Properties properties = FileUtil.readPropertiesByCls("server.properties", cl);
    NioEventLoopGroup group = new NioEventLoopGroup(true);
    ChannelAcceptor context = new ChannelAcceptor(group);
    ConfigurationParser.parseConfiguration("server.", context, properties);
    ConfigurationParser.parseConfiguration("server.", group, properties);
    context.setIoEventHandle(handle);
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.addChannelEventListener(new WebSocketChannelListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new CountChannelListener());
    context.setExecutorGroup(new ThreadEventLoopGroup());
    context.addLifeCycleListener(new LifeCycleListener() {

        @Override
        public void lifeCycleStarting(LifeCycle lifeCycle) {
            try {
                handle.initialize(context, rootPath, prodMode);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        @Override
        public void lifeCycleStopped(LifeCycle lifeCycle) {
            handle.destroy(context);
        }
    });
    String[] applicationProtocols = null;
    if (properties.getBooleanProperty("app.enableHttp2")) {
        context.addProtocolCodec(new Http2Codec());
        applicationProtocols = new String[] { "h2", "http/1.1" };
    } else {
        context.addProtocolCodec(new HttpCodec(4));
        context.addProtocolCodec(new WebSocketCodec());
    }
    int defaultPort = 80;
    String pem = properties.getProperty("server.sslPem");
    if (!Util.isNullOrBlank(pem)) {
        defaultPort = 443;
        SslContext sslContext = loadSslContextFromPem(pem, applicationProtocols, cl);
        context.setSslContext(sslContext);
    }
    int port = properties.getIntegerProperty("server.port", defaultPort);
    context.setPort(port);
    try {
        context.bind();
    } catch (Exception e) {
        HttpDateUtil.stop();
        group.stop();
        throw e;
    }
    this.group = group;
    this.context = context;
    if (properties.getBooleanProperty("app.proxy")) {
        NetDataTransferServer.get().startup(group, 18088);
    }
}
Also used : LifeCycle(com.firenio.LifeCycle) ChannelAliveListener(com.firenio.component.ChannelAliveListener) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LifeCycleListener(com.firenio.LifeCycleListener) Properties(com.firenio.common.Properties) WebSocketChannelListener(com.firenio.codec.http11.WebSocketChannelListener) SSLException(javax.net.ssl.SSLException) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) HttpCodec(com.firenio.codec.http11.HttpCodec) CountChannelListener(sample.http11.service.CountChannelListener) SpringHttpFrameHandle(sample.http11.SpringHttpFrameHandle) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) SslContext(com.firenio.component.SslContext)

Aggregations

NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 ThreadEventLoopGroup (com.firenio.concurrent.ThreadEventLoopGroup)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 Channel (com.firenio.component.Channel)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 Frame (com.firenio.component.Frame)2 IoEventHandle (com.firenio.component.IoEventHandle)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 LifeCycle (com.firenio.LifeCycle)1 LifeCycleListener (com.firenio.LifeCycleListener)1 ByteBuf (com.firenio.buffer.ByteBuf)1 HttpCodec (com.firenio.codec.http11.HttpCodec)1 WebSocketChannelListener (com.firenio.codec.http11.WebSocketChannelListener)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 Http2Codec (com.firenio.codec.http2.Http2Codec)1 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)1 Properties (com.firenio.common.Properties)1 ChannelAliveListener (com.firenio.component.ChannelAliveListener)1 ChannelConnector (com.firenio.component.ChannelConnector)1 SslContext (com.firenio.component.SslContext)1