Search in sources :

Example 1 with ByteBufAllocator

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

the class TestBytebufAllocator method test.

static void test() throws Exception {
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_MEMORY_POOL_CAPACITY(10);
    configuration.setSERVER_MEMORY_POOL_UNIT(1);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    PooledByteBufAllocatorManager allocator = new PooledByteBufAllocatorManager(context);
    allocator.start();
    ByteBufAllocator allocator2 = allocator.getNextBufAllocator();
    ByteBuf buf = allocator2.allocate(15);
    System.out.println(buf);
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) PooledByteBufAllocatorManager(com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 2 with ByteBufAllocator

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

the class ProtobaseProtocolDecoder method decode.

@Override
public ChannelFuture decode(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ByteBuf buf = allocator.allocate(2);
    return new ProtobaseFutureImpl(channel, buf);
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 3 with ByteBufAllocator

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

the class CharBasedProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    CharBasedFuture f = (CharBasedFuture) future;
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        throw new IOException("null write buffer");
    }
    ByteBuf buf = allocator.allocate(writeSize + 1);
    buf.put(f.getWriteBuffer(), 0, writeSize);
    buf.putByte(splitor);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Example 4 with ByteBufAllocator

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

the class FixedLengthProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    if (future.isHeartbeat()) {
        ByteBuf buf = future.isPING() ? PING.duplicate() : PONG.duplicate();
        future.setByteBuf(buf);
        return;
    }
    FixedLengthFuture f = (FixedLengthFuture) future;
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        throw new IOException("null write buffer");
    }
    ByteBuf buf = allocator.allocate(writeSize + 4);
    buf.putInt(writeSize);
    buf.put(f.getWriteBuffer(), 0, writeSize);
    future.setByteBuf(buf.flip());
}
Also used : UnpooledByteBufAllocator(com.generallycloud.baseio.buffer.UnpooledByteBufAllocator) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 5 with ByteBufAllocator

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

the class Http2ProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    Http2Frame frame = (Http2Frame) future;
    Http2FrameType frameType = frame.getHttp2FrameType();
    byte[] payload = null;
    switch(frameType) {
        case FRAME_TYPE_CONTINUATION:
            break;
        case FRAME_TYPE_DATA:
            break;
        case FRAME_TYPE_FRAME_HEADER:
            break;
        case FRAME_TYPE_GOAWAY:
            break;
        case FRAME_TYPE_HEADERS:
            Http2HeadersFrame hf = (Http2HeadersFrame) frame;
            break;
        case FRAME_TYPE_PING:
            break;
        case FRAME_TYPE_PREFACE:
            break;
        case FRAME_TYPE_PRIORITY:
            break;
        case FRAME_TYPE_PUSH_PROMISE:
            break;
        case FRAME_TYPE_RST_STREAM:
            break;
        case FRAME_TYPE_SETTINGS:
            Http2SettingsFrame sf = (Http2SettingsFrame) frame;
            long[] settings = sf.getSettings();
            payload = new byte[6 * 6];
            for (int i = 0; i < 6; i++) {
                int realI = i + 1;
                int offset = i * 6;
                MathUtil.unsignedShort2Byte(payload, realI, offset);
                MathUtil.unsignedInt2Byte(payload, settings[realI], offset + 2);
            }
            break;
        case FRAME_TYPE_WINDOW_UPDATE:
            break;
        default:
            break;
    }
    int length = payload.length;
    ByteBuf buf = allocator.allocate(length + Http2ProtocolDecoder.PROTOCOL_HEADER);
    byte b2 = (byte) ((length & 0xff));
    byte b1 = (byte) ((length >> 8 * 1) & 0xff);
    byte b0 = (byte) ((length >> 8 * 2) & 0xff);
    byte b3 = frameType.getByteValue();
    buf.putByte(b0);
    buf.putByte(b1);
    buf.putByte(b2);
    buf.putByte(b3);
    buf.putByte((byte) 0);
    buf.putInt(frame.getHeader().getStreamIdentifier());
    buf.put(payload);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) Http2SettingsFrame(com.generallycloud.baseio.codec.http2.future.Http2SettingsFrame) Http2HeadersFrame(com.generallycloud.baseio.codec.http2.future.Http2HeadersFrame) Http2FrameType(com.generallycloud.baseio.codec.http2.future.Http2FrameType) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) Http2Frame(com.generallycloud.baseio.codec.http2.future.Http2Frame)

Aggregations

ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)11 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)9 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)3 IOException (java.io.IOException)2 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)1 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)1 ServerHttpFuture (com.generallycloud.baseio.codec.http11.future.ServerHttpFuture)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 Http2SettingsFrame (com.generallycloud.baseio.codec.http2.future.Http2SettingsFrame)1 ParamedProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFutureImpl)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)1 ByteArrayBuffer (com.generallycloud.baseio.component.ByteArrayBuffer)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1