use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture 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