Search in sources :

Example 51 with ByteBuf

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

the class Channel method read_plain_remain.

protected void read_plain_remain(ByteBuf dst) {
    ByteBuf remain = this.plain_remain_buf;
    if (remain != null) {
        dst.writeBytes(remain);
        remain.release();
        this.plain_remain_buf = null;
    }
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 52 with ByteBuf

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

the class Channel method release_wb_array.

private void release_wb_array() {
    final ByteBuf[] c_wbs = this.current_wb_array;
    final int max_len = c_wbs.length;
    // 对所有不为空的ByteBuf release
    for (int i = 0; i < max_len; i++) {
        ByteBuf buf = c_wbs[i];
        if (buf == null) {
            break;
        }
        buf.release();
        c_wbs[i] = null;
    }
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 53 with ByteBuf

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

the class ChannelManagerListener method broadcast.

public static void broadcast(Frame frame, Collection<Channel> chs) throws Exception {
    if (chs.size() == 0) {
        frame.release();
        return;
    }
    Channel ch = chs.iterator().next();
    if (ch == null) {
        frame.release();
        return;
    }
    try {
        ByteBuf buf = ch.getCodec().encode(ch, frame);
        broadcast(buf, chs);
    } finally {
        frame.release();
    }
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 54 with ByteBuf

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

the class HttpCodec method encode.

@Override
public ByteBuf encode(final Channel ch, Frame frame) {
    HttpFrame f = (HttpFrame) frame;
    FastThreadLocal l = FastThreadLocal.get();
    List<byte[]> bytes_array = (List<byte[]>) l.getList();
    Object content = f.getContent();
    ByteBuf content_buf = null;
    byte[] content_array = null;
    byte[] head_bytes = f.getStatus().getLine();
    byte[] conn_bytes = f.getConnection().getLine();
    byte[] type_bytes = f.getContentType().getLine();
    byte[] date_bytes = f.getDate();
    boolean is_array = false;
    int write_size = 0;
    if (content instanceof ByteBuf) {
        content_buf = (ByteBuf) content;
        write_size = content_buf.readableBytes();
    } else if (content instanceof byte[]) {
        is_array = true;
        content_array = (byte[]) content;
        write_size = content_array.length;
    }
    byte[] cl_len_bytes;
    int cl_len;
    if (write_size < 1024) {
        cl_len_bytes = cl_bytes[write_size];
        cl_len = cl_len_bytes.length;
    } else {
        cl_len_bytes = cl_buf.array();
        int tmp_len = cl_buf.limit();
        int len_idx = Util.valueOf(write_size, cl_len_bytes);
        int num_len = cl_len_bytes.length - len_idx;
        System.arraycopy(cl_len_bytes, len_idx, cl_len_bytes, tmp_len, num_len);
        cl_len = tmp_len + num_len;
    }
    int hlen = head_bytes.length;
    int tlen = type_bytes == null ? 0 : type_bytes.length;
    int clen = conn_bytes == null ? 0 : conn_bytes.length;
    int dlen = date_bytes == null ? 0 : date_bytes.length;
    int len = hlen + cl_len + dlen + 2 + clen + tlen;
    int h_size = 0;
    IntObjectMap<byte[]> headers = f.getResponseHeaders();
    if (headers != null) {
        for (headers.scan(); headers.hasNext(); ) {
            byte[] k = HttpHeader.get(headers.getKey()).getBytes();
            byte[] v = headers.getValue();
            h_size++;
            bytes_array.add(k);
            bytes_array.add(v);
            len += 4;
            len += k.length;
            len += v.length;
        }
    }
    len += 2;
    if (write_size <= cthreshold) {
        len += write_size;
    }
    ByteBuf buf;
    if (Develop.BUF_DEBUG) {
        buf = ch.alloc().allocate(1);
    } else {
        buf = ch.alloc().allocate(len);
    }
    buf.writeBytes(head_bytes);
    buf.writeBytes(cl_len_bytes, 0, cl_len);
    if (conn_bytes != null) {
        buf.writeBytes(conn_bytes);
    }
    if (type_bytes != null) {
        buf.writeBytes(type_bytes);
    }
    if (date_bytes != null) {
        buf.writeBytes(date_bytes);
    }
    buf.writeByte(R);
    buf.writeByte(N);
    if (h_size > 0) {
        put_headers(buf, bytes_array, h_size);
    }
    buf.writeByte(R);
    buf.writeByte(N);
    if (write_size > cthreshold) {
        ch.write(buf);
        if (is_array) {
            ch.write(ByteBuf.wrap(content_array));
        } else {
            ch.write(content_buf);
        }
        return null;
    } else {
        if (write_size > 0) {
            if (is_array) {
                buf.writeBytes(content_array);
            } else {
                buf.writeBytes(content_buf);
            }
        }
        return buf;
    }
}
Also used : List(java.util.List) ByteBuf(com.firenio.buffer.ByteBuf) FastThreadLocal(com.firenio.component.FastThreadLocal)

Example 55 with ByteBuf

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

the class HttpFrame method updateWebSocketProtocol.

public boolean updateWebSocketProtocol(final Channel ch) throws Exception {
    String Sec_WebSocket_Key_Value = getRequestHeader(Sec_WebSocket_Key);
    if (!Util.isNullOrBlank(Sec_WebSocket_Key_Value)) {
        // FIXME 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 必须这个值?
        String Sec_WebSocket_Key_Magic = Sec_WebSocket_Key_Value + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
        byte[] key_array = Cryptos.SHA1(Sec_WebSocket_Key_Magic);
        String acceptKey = Cryptos.base64_en(key_array);
        setStatus(HttpStatus.C101);
        setConnection(HttpConnection.UPGRADE);
        setResponseHeader(Upgrade, HttpStatic.websocket_bytes);
        setResponseHeader(Sec_WebSocket_Accept, acceptKey.getBytes());
        ((HttpAttachment) ch.getAttachment()).setWebSocketFrameName(getRequestURL());
        ByteBuf buf = ch.encode(this);
        ch.setCodec(WebSocketCodec.PROTOCOL_ID);
        ch.writeAndFlush(buf);
        return true;
    }
    return false;
}
Also used : 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