Search in sources :

Example 26 with ByteBuf

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

the class TestDuplicatedHeapByteBuf method testHeap.

@Test
public void testHeap() {
    ByteBuf buf = ByteBuf.direct(16);
    buf.writeBytes(data.getBytes());
    ByteBuf buf2 = buf.duplicate();
    v(buf2);
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 27 with ByteBuf

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

the class TestDuplicatedHeapByteBuf method testDirectP.

@Test
public void testDirectP() throws Exception {
    ByteBufAllocator a = TestAllocUtil.direct();
    ByteBuf buf = a.allocate(16);
    buf.writeBytes(data.getBytes());
    ByteBuf buf2 = buf.duplicate();
    v(buf2);
}
Also used : ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 28 with ByteBuf

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

the class TestDuplicatedHeapByteBuf method testHeapP.

@Test
public void testHeapP() throws Exception {
    ByteBufAllocator a = TestAllocUtil.heap();
    ByteBuf buf = a.allocate(16);
    buf.writeBytes(data.getBytes());
    ByteBuf buf2 = buf.duplicate();
    v(buf2);
}
Also used : ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 29 with ByteBuf

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

the class ClientHttpCodec method encode.

@Override
public ByteBuf encode(Channel ch, Frame frame) {
    ClientHttpFrame f = (ClientHttpFrame) frame;
    Object content = f.getContent();
    ByteBuf contentBuf = null;
    byte[] contentArray = null;
    boolean isArray = false;
    int write_size = 0;
    if (content instanceof ByteBuf) {
        contentBuf = ((ByteBuf) content);
        write_size = contentBuf.writeIndex();
    } else if (content instanceof byte[]) {
        isArray = true;
        contentArray = (byte[]) content;
        write_size = contentArray.length;
    }
    byte[] byte32 = FastThreadLocal.get().getBytes32();
    byte[] url_bytes = getRequestURI(f).getBytes();
    byte[] mtd_bytes = f.getMethod().getBytes();
    int len_idx = Util.valueOf(write_size, byte32);
    int len_len = 32 - len_idx;
    int len = mtd_bytes.length + 1 + url_bytes.length + PROTOCOL.length + len_len + 2;
    int header_size = 0;
    List<byte[]> bytes_array = (List<byte[]>) FastThreadLocal.get().getList();
    IntObjectMap<String> headers = f.getRequestHeaders();
    if (headers != null) {
        headers.remove(HttpHeader.Content_Length.getId());
        for (headers.scan(); headers.hasNext(); ) {
            byte[] k = HttpHeader.get(headers.getKey()).getBytes();
            byte[] v = headers.getValue().getBytes();
            if (v == null) {
                continue;
            }
            header_size++;
            bytes_array.add(k);
            bytes_array.add(v);
            len += 4;
            len += k.length;
            len += v.length;
        }
    }
    len += 2;
    if (isArray) {
        len += write_size;
    }
    ByteBuf buf = ch.alloc().allocate(len);
    buf.writeBytes(mtd_bytes);
    buf.writeByte(SPACE);
    buf.writeBytes(url_bytes);
    if (f.isGet()) {
        buf.writeBytes(PROTOCOL);
    } else {
        buf.writeBytes(PROTOCOL_CL);
        buf.writeBytes(byte32, len_idx, len_len);
        buf.writeByte(R);
        buf.writeByte(N);
    }
    int j = 0;
    for (int i = 0; i < header_size; i++) {
        buf.writeBytes(bytes_array.get(j++));
        buf.writeByte((byte) ':');
        buf.writeByte(SPACE);
        buf.writeBytes(bytes_array.get(j++));
        buf.writeByte(R);
        buf.writeByte(N);
    }
    buf.writeByte(R);
    buf.writeByte(N);
    if (write_size > 0) {
        if (isArray) {
            buf.writeBytes(contentArray);
        } else {
            ch.write(buf);
            ch.write(contentBuf);
            return null;
        }
    }
    return buf;
}
Also used : List(java.util.List) ByteBuf(com.firenio.buffer.ByteBuf)

Example 30 with ByteBuf

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

the class WebSocketMsgAdapter method doLoop.

@Override
protected void doLoop() throws InterruptedException {
    Msg msg = (Msg) msgs.poll(16, TimeUnit.MILLISECONDS);
    if (msg == null) {
        return;
    }
    if (msg.ch != null) {
        WebSocketFrame f = new WebSocketFrame();
        f.setString(msg.msg, msg.ch);
        try {
            msg.ch.writeAndFlush(f);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    } else {
        if (!clientMap.isEmpty()) {
            synchronized (this) {
                Client client = clientMap.values().iterator().next();
                Channel ch = client.channel;
                WebSocketFrame f = new WebSocketFrame();
                byte[] data = msg.msg.getBytes();
                f.setContent(data);
                try {
                    ByteBuf buf = ch.getCodec().encode(ch, f);
                    ChannelManagerListener.broadcast(buf, channelMap.values());
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : Channel(com.firenio.component.Channel) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) ByteBuf(com.firenio.buffer.ByteBuf)

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