use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class ServerHTTPProtocolEncoder method encode.
private void encode(ByteBufAllocator allocator, ServerHttpFuture f, int length, byte[] array) throws IOException {
ByteBuf buf = allocator.allocate(256);
try {
buf.put(PROTOCOL);
buf.put(f.getStatus().getHeaderBinary());
buf.put(SERVER_CL);
buf.put(String.valueOf(length).getBytes());
buf.put(RN);
writeHeaders(f, buf);
List<Cookie> cookieList = f.getCookieList();
if (cookieList != null) {
for (Cookie c : cookieList) {
writeBuf(buf, SET_COOKIE);
writeBuf(buf, c.toString().getBytes());
writeBuf(buf, RN);
}
}
writeBuf(buf, RN);
if (length != 0) {
writeBuf(buf, array, 0, length);
}
} catch (Exception e) {
ReleaseUtil.release(buf);
throw e;
}
f.setByteBuf(buf.flip());
}
use of com.generallycloud.baseio.buffer.ByteBuf 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());
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class WebSocketFutureImpl method read.
@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
ByteBuf buf = this.buf;
if (type == 0) {
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
buf.flip();
int remain_header_size = 0;
byte b = buf.getByte();
eof = (b & 0b10000000) > 0;
type = (b & 0xF);
if (type == WebSocketProtocolDecoder.TYPE_PING) {
setPING();
} else if (type == WebSocketProtocolDecoder.TYPE_PONG) {
setPONG();
}
b = buf.getByte();
hasMask = (b & 0b10000000) > 0;
if (hasMask) {
remain_header_size += 4;
}
length = (b & 0x7f);
if (length < 126) {
} else if (length == 126) {
remain_header_size += 2;
} else {
remain_header_size += 4;
}
buf.reallocate(remain_header_size);
}
if (!remain_data_complete) {
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
buf.flip();
if (length < 126) {
} else if (length == 126) {
length = buf.getUnsignedShort();
} else {
length = (int) buf.getUnsignedInt();
if (length < 0) {
throw new IOException("too long data length");
}
}
if (hasMask) {
mask = buf.getBytes();
}
buf.reallocate(length, limit);
remain_data_complete = true;
}
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
buf.flip();
byte[] array = buf.getBytes();
if (hasMask) {
byte[] mask = this.mask;
int length = array.length;
for (int i = 0; i < length; i++) {
array[i] = (byte) (array[i] ^ mask[i % 4]);
}
}
this.byteArray = array;
if (type == WebSocketProtocolDecoder.TYPE_BINARY) {
// FIXME 处理binary
} else {
this.readText = new String(array, context.getEncoding());
}
return true;
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Http2HeadersFrameImpl method read.
@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
if (!isComplete) {
ByteBuf buf = this.buf;
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 ClientHTTPProtocolEncoder method encode.
@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
ByteBufAllocator allocator = channel.getByteBufAllocator();
HttpFuture f = (HttpFuture) future;
ByteBuf buf = allocator.allocate(256);
buf.put(f.getMethod().getBytes());
buf.putByte(SPACE);
buf.put(getRequestURI(f).getBytes());
buf.put(PROTOCOL);
writeHeaders(f, buf);
List<Cookie> cookieList = f.getCookieList();
if (cookieList != null && cookieList.size() > 0) {
buf.put(COOKIE);
for (Cookie c : cookieList) {
writeBuf(buf, c.getName().getBytes());
writeBuf(buf, COLON);
writeBuf(buf, c.getValue().getBytes());
writeBuf(buf, SEMICOLON);
}
buf.position(buf.position() - 1);
}
buf.put(RN);
future.setByteBuf(buf.flip());
}
Aggregations