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);
}
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);
}
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;
}
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);
}
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());
}
Aggregations