use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class ReadCompletionHandler method completed.
@Override
public void completed(Integer result, AioSocketChannel channel) {
try {
if (result < 1) {
if (result == 0) {
channel.read(channel.getReadCache());
return;
}
CloseUtil.close(channel);
return;
}
channel.active();
ByteBuf buf = channel.getReadCache();
buf.reverse();
buf.flip();
byteBufReader.accept(channel, buf);
} catch (Exception e) {
failed(e, channel);
}
channel.read(channel.getReadCache());
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class ProtobaseProtocolEncoder 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;
}
ProtobaseFuture f = (ProtobaseFuture) future;
String futureName = f.getFutureName();
if (StringUtil.isNullOrBlank(futureName)) {
throw new ProtocolException("future name is empty");
}
byte[] futureNameBytes = futureName.getBytes(channel.getEncoding());
if (futureNameBytes.length > Byte.MAX_VALUE) {
throw new ProtocolException("future name max length 127");
}
byte futureNameLength = (byte) futureNameBytes.length;
int allLen = 6 + futureNameLength;
int textWriteSize = f.getWriteSize();
int binaryWriteSize = f.getWriteBinarySize();
byte h1 = 0b01000000;
if (f.isBroadcast()) {
h1 |= 0b00100000;
}
if (f.getFutureId() > 0) {
h1 |= 0b00010000;
allLen += 4;
}
if (f.getSessionId() > 0) {
h1 |= 0b00001000;
allLen += 4;
}
if (f.getHashCode() > 0) {
h1 |= 0b00000100;
allLen += 4;
}
if (f.getWriteBinarySize() > 0) {
h1 |= 0b00000010;
allLen += 4;
allLen += f.getWriteBinarySize();
}
if (textWriteSize > 0) {
allLen += textWriteSize;
}
ByteBuf buf = allocator.allocate(allLen);
buf.putByte(h1);
buf.putByte(futureNameLength);
buf.putInt(textWriteSize);
if (f.getFutureId() > 0) {
buf.putInt(f.getFutureId());
}
if (f.getSessionId() > 0) {
buf.putInt(f.getSessionId());
}
if (f.getHashCode() > 0) {
buf.putInt(f.getHashCode());
}
if (binaryWriteSize > 0) {
buf.putInt(binaryWriteSize);
}
buf.put(futureNameBytes);
if (textWriteSize > 0) {
buf.put(f.getWriteBuffer(), 0, textWriteSize);
}
if (binaryWriteSize > 0) {
buf.put(f.getWriteBinary(), 0, binaryWriteSize);
}
future.setByteBuf(buf.flip());
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Http2PrefaceFuture method read.
@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
ByteBuf buf = this.buf;
if (!isComplete) {
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
this.isComplete = true;
doComplete(channel, buf.flip());
}
return true;
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class SslFutureImpl method copy.
@Override
public SslFuture copy(SocketChannel channel) {
ByteBuf src = getByteBuf();
ByteBuf buf = allocate(channel, src.limit());
buf.read(src.flip());
SslFutureImpl copy = new SslFutureImpl(channel.getContext(), buf, 1024 * 64);
copy.header_complete = this.header_complete;
return copy;
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class TestSimplyByteBufAllocator method main.
public static void main(String[] args) throws Exception {
int capacity = 100;
int unit = 1;
int max = unit * capacity / 5;
allocator = new SimplyByteBufAllocator(capacity, unit, false);
// allocator = new SimpleByteBufAllocator(capacity, unit, false);
allocator.start();
Runnable r = () -> {
for (; ; ) {
int random = new Random().nextInt(max);
if (random == 0) {
continue;
}
ByteBuf buf = allocator.allocate(random);
if (buf == null) {
System.out.println(buf + Thread.currentThread().getName());
}
ThreadUtil.sleep(new Random().nextInt(20));
ReleaseUtil.release(buf);
// String des = allocator.toString();
//
// if (des.indexOf("free=100") == -1) {
// System.out.println();
// }
// ThreadUtil.sleep(10);
debug();
}
};
ThreadUtil.execute(r);
ThreadUtil.execute(r);
// ThreadUtil.execute(r);
//
// ThreadUtil.execute(r);
}
Aggregations