Search in sources :

Example 11 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestLoadClient method buildRequest.

private static ByteBuf buildRequest(String request, int pipes) {
    byte[] bytes = request.getBytes();
    ByteBuf buf = ByteBuf.direct(bytes.length * pipes);
    for (int i = 0; i < pipes; i++) {
        buf.writeBytes(bytes);
    }
    return buf;
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 12 with ByteBuf

use of com.firenio.buffer.ByteBuf 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 13 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestBytebufPut method testPut4.

@Test
public void testPut4() {
    ByteBuf src = ByteBuf.heap(1024);
    src.writeBytes("hello;hello;".getBytes());
    ByteBuf dst = ByteBuf.heap(1024);
    dst.writeBytes(src);
    v(dst);
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 14 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestBytebufPut method testput2.

@Test
public void testput2() {
    ByteBuf src = ByteBuf.heap(1024);
    src.writeBytes(data.getBytes());
    ByteBuf dst = ByteBuf.direct(1024);
    dst.writeBytes(src);
    v(dst);
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 15 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestExpansion method arrayPool.

@Test
public void arrayPool() throws Exception {
    ByteBufAllocator alloc = TestAllocUtil.heap(1024 * 4);
    ByteBuf buf = alloc.allocate(2);
    for (int i = 0; i < 10; i++) {
        buf.writeBytes(data);
        buf.writeByte(a);
        alloc.allocate(1);
    }
    Assert.assertTrue(v(buf));
}
Also used : PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)58 Test (org.junit.Test)18 ByteBufAllocator (com.firenio.buffer.ByteBufAllocator)5 Channel (com.firenio.component.Channel)5 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)4 Frame (com.firenio.component.Frame)3 IoEventHandle (com.firenio.component.IoEventHandle)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 ChannelConnector (com.firenio.component.ChannelConnector)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 ByteBufAllocatorGroup (com.firenio.buffer.ByteBufAllocatorGroup)1 PoolState (com.firenio.buffer.PooledByteBufAllocator.PoolState)1