Search in sources :

Example 1 with ByteBuf

use of com.generallycloud.baseio.buffer.ByteBuf 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 ByteBuf

use of com.generallycloud.baseio.buffer.ByteBuf 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 ByteBuf

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

the class ProtobaseFutureImpl method read.

@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBuf buf = this.buf;
    if (futureNameLength == 0) {
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        int nextLen = 4;
        byte h1 = buf.getByte(0);
        int type = h1 & 0b11000000;
        if (type > 0b01000000) {
            setHeartbeat(type == 0b10000000);
            return true;
        }
        isBroadcast = ((h1 & 0b00100000) != 0);
        if ((h1 & 0b00010000) != 0) {
            futureId = -1;
            nextLen += 4;
        }
        if ((h1 & 0b00001000) != 0) {
            sessionId = -1;
            nextLen += 4;
        }
        if ((h1 & 0b00000100) != 0) {
            hashCode = -1;
            nextLen += 4;
        }
        if ((h1 & 0b00000010) != 0) {
            binaryReadSize = -1;
            nextLen += 4;
        }
        futureNameLength = buf.getByte(1);
        if (futureNameLength < 1) {
            throw new IOException("futureNameLength < 1");
        }
        nextLen += futureNameLength;
        buf.reallocate(nextLen);
    }
    if (futureName == null) {
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        buf.flip();
        textLength = buf.getInt();
        if (futureId == -1) {
            futureId = buf.getInt();
        }
        if (sessionId == -1) {
            sessionId = buf.getInt();
        }
        if (hashCode == -1) {
            hashCode = buf.getInt();
        }
        if (binaryReadSize == -1) {
            binaryReadSize = buf.getInt();
        }
        Charset charset = context.getEncoding();
        ByteBuffer memory = buf.nioBuffer();
        futureName = StringUtil.decode(charset, memory);
        buf.reallocate(textLength + binaryReadSize);
    }
    buf.read(buffer);
    if (buf.hasRemaining()) {
        return false;
    }
    if (textLength > 0) {
        buf.flip();
        buf.markPL();
        buf.limit(textLength);
        Charset charset = context.getEncoding();
        ByteBuffer memory = buf.nioBuffer();
        readText = StringUtil.decode(charset, memory);
        buf.reset();
        buf.skipBytes(textLength);
    }
    if (binaryReadSize > 0) {
        this.binaryReadBuffer = buf.getBytes();
    }
    return true;
}
Also used : Charset(java.nio.charset.Charset) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ByteBuf

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

the class RedisProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    RedisFuture f = (RedisFuture) future;
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        throw new IOException("null write buffer");
    }
    ByteBuf buf = UnpooledByteBufAllocator.getHeap().wrap(f.getWriteBuffer(), 0, writeSize);
    future.setByteBuf(buf);
}
Also used : RedisFuture(com.generallycloud.baseio.codec.redis.future.RedisFuture) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 5 with ByteBuf

use of com.generallycloud.baseio.buffer.ByteBuf 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)

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