Search in sources :

Example 26 with ByteBuf

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

the class ServerHTTPProtocolEncoder method encode.

private void encode(ByteBufAllocator allocator, ServerHttpFuture f, int length, byte[] array) throws IOException {
    ByteBuf buf = allocator.allocate(256);
    try {
        buf.put(PROTOCOL);
        buf.put(f.getStatus().getHeaderBinary());
        buf.put(SERVER_CL);
        buf.put(String.valueOf(length).getBytes());
        buf.put(RN);
        writeHeaders(f, buf);
        List<Cookie> cookieList = f.getCookieList();
        if (cookieList != null) {
            for (Cookie c : cookieList) {
                writeBuf(buf, SET_COOKIE);
                writeBuf(buf, c.toString().getBytes());
                writeBuf(buf, RN);
            }
        }
        writeBuf(buf, RN);
        if (length != 0) {
            writeBuf(buf, array, 0, length);
        }
    } catch (Exception e) {
        ReleaseUtil.release(buf);
        throw e;
    }
    f.setByteBuf(buf.flip());
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) IOException(java.io.IOException)

Example 27 with ByteBuf

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

the class WebSocketProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    WebSocketFuture f = (WebSocketFuture) future;
    byte[] header;
    byte[] data = f.getWriteBuffer();
    int size = f.getWriteSize();
    byte header0 = (byte) (0x8f & (f.getType() | 0xf0));
    if (size < 126) {
        header = new byte[2];
        header[0] = header0;
        header[1] = (byte) size;
    } else if (size <= MAX_UNSIGNED_SHORT) {
        header = new byte[4];
        header[0] = header0;
        header[1] = 126;
        MathUtil.unsignedShort2Byte(header, size, 2);
    } else {
        header = new byte[6];
        header[0] = header0;
        header[1] = 127;
        MathUtil.int2Byte(header, size, 2);
    }
    ByteBuf buf = allocator.allocate(header.length + size);
    buf.put(header);
    buf.put(data, 0, size);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 28 with ByteBuf

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

the class WebSocketFutureImpl method read.

@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBuf buf = this.buf;
    if (type == 0) {
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        buf.flip();
        int remain_header_size = 0;
        byte b = buf.getByte();
        eof = (b & 0b10000000) > 0;
        type = (b & 0xF);
        if (type == WebSocketProtocolDecoder.TYPE_PING) {
            setPING();
        } else if (type == WebSocketProtocolDecoder.TYPE_PONG) {
            setPONG();
        }
        b = buf.getByte();
        hasMask = (b & 0b10000000) > 0;
        if (hasMask) {
            remain_header_size += 4;
        }
        length = (b & 0x7f);
        if (length < 126) {
        } else if (length == 126) {
            remain_header_size += 2;
        } else {
            remain_header_size += 4;
        }
        buf.reallocate(remain_header_size);
    }
    if (!remain_data_complete) {
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        buf.flip();
        if (length < 126) {
        } else if (length == 126) {
            length = buf.getUnsignedShort();
        } else {
            length = (int) buf.getUnsignedInt();
            if (length < 0) {
                throw new IOException("too long data length");
            }
        }
        if (hasMask) {
            mask = buf.getBytes();
        }
        buf.reallocate(length, limit);
        remain_data_complete = true;
    }
    buf.read(buffer);
    if (buf.hasRemaining()) {
        return false;
    }
    buf.flip();
    byte[] array = buf.getBytes();
    if (hasMask) {
        byte[] mask = this.mask;
        int length = array.length;
        for (int i = 0; i < length; i++) {
            array[i] = (byte) (array[i] ^ mask[i % 4]);
        }
    }
    this.byteArray = array;
    if (type == WebSocketProtocolDecoder.TYPE_BINARY) {
    // FIXME 处理binary
    } else {
        this.readText = new String(array, context.getEncoding());
    }
    return true;
}
Also used : IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 29 with ByteBuf

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

the class Http2HeadersFrameImpl method read.

@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
    if (!isComplete) {
        ByteBuf buf = this.buf;
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        this.isComplete = true;
        doComplete(channel, buf.flip());
    }
    return true;
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 30 with ByteBuf

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

the class ClientHTTPProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    HttpFuture f = (HttpFuture) future;
    ByteBuf buf = allocator.allocate(256);
    buf.put(f.getMethod().getBytes());
    buf.putByte(SPACE);
    buf.put(getRequestURI(f).getBytes());
    buf.put(PROTOCOL);
    writeHeaders(f, buf);
    List<Cookie> cookieList = f.getCookieList();
    if (cookieList != null && cookieList.size() > 0) {
        buf.put(COOKIE);
        for (Cookie c : cookieList) {
            writeBuf(buf, c.getName().getBytes());
            writeBuf(buf, COLON);
            writeBuf(buf, c.getValue().getBytes());
            writeBuf(buf, SEMICOLON);
        }
        buf.position(buf.position() - 1);
    }
    buf.put(RN);
    future.setByteBuf(buf.flip());
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Aggregations

ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)31 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)9 IOException (java.io.IOException)9 EmptyByteBuf (com.generallycloud.baseio.buffer.EmptyByteBuf)4 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)3 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)2 SslHandler (com.generallycloud.baseio.component.ssl.SslHandler)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 FixedUnpooledByteBuf (com.generallycloud.baseio.buffer.FixedUnpooledByteBuf)1 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 SimplyByteBufAllocator (com.generallycloud.baseio.buffer.SimplyByteBufAllocator)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)1 WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)1 Http2Frame (com.generallycloud.baseio.codec.http2.future.Http2Frame)1 Http2FrameType (com.generallycloud.baseio.codec.http2.future.Http2FrameType)1 Http2HeadersFrame (com.generallycloud.baseio.codec.http2.future.Http2HeadersFrame)1