use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class ParamedProtobaseProtocolDecoder method decode.
@Override
public ChannelFuture decode(SocketChannel channel, ByteBuf buffer) throws IOException {
ByteBufAllocator allocator = channel.getByteBufAllocator();
ByteBuf buf = allocator.allocate(2);
return new ParamedProtobaseFutureImpl(channel, buf);
}
use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.
the class SslHandler method getTempDst.
private ByteBuf getTempDst(SSLEngine engine) {
if (tempDst == null) {
synchronized (this) {
if (tempDst != null) {
return tempDst;
}
ByteBufAllocator allocator = UnpooledByteBufAllocator.getHeap();
int packetBufferSize = engine.getSession().getPacketBufferSize();
tempDst = allocator.allocate(packetBufferSize);
}
}
return tempDst;
}
use of com.generallycloud.baseio.buffer.ByteBufAllocator 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.ByteBufAllocator in project baseio by generallycloud.
the class ServerHTTPProtocolEncoder method encode.
@Override
public void encode(SocketChannel channel, ChannelFuture readFuture) throws IOException {
ByteBufAllocator allocator = channel.getByteBufAllocator();
ServerHttpFuture f = (ServerHttpFuture) readFuture;
if (f.isUpdateWebSocketProtocol()) {
channel.setProtocolDecoder(WebSocketProtocolFactory.WS_PROTOCOL_DECODER);
channel.setProtocolEncoder(WebSocketProtocolFactory.WS_PROTOCOL_ENCODER);
channel.setProtocolFactory(WebSocketProtocolFactory.WS_PROTOCOL_FACTORY);
channel.getSession().setAttribute(WebSocketFuture.SESSION_KEY_SERVICE_NAME, f.getFutureName());
}
f.setResponseHeader("Date", HttpHeaderDateFormat.getFormat().format(System.currentTimeMillis()));
ByteArrayBuffer os = f.getBinaryBuffer();
if (os != null) {
encode(allocator, f, os.size(), os.array());
return;
}
int writeSize = f.getWriteSize();
if (writeSize == 0) {
encode(allocator, f, 0, null);
return;
}
encode(allocator, f, writeSize, f.getWriteBuffer());
}
use of com.generallycloud.baseio.buffer.ByteBufAllocator 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());
}
Aggregations