use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class FixedLengthProtocolEncoder 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;
}
FixedLengthFuture f = (FixedLengthFuture) future;
int writeSize = f.getWriteSize();
if (writeSize == 0) {
throw new IOException("null write buffer");
}
ByteBuf buf = allocator.allocate(writeSize + 4);
buf.putInt(writeSize);
buf.put(f.getWriteBuffer(), 0, writeSize);
future.setByteBuf(buf.flip());
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class FixedLengthFutureImpl method read.
@Override
public boolean read(SocketChannel channel, ByteBuf src) throws IOException {
ByteBuf buf = this.buf;
if (buf == EmptyByteBuf.getInstance()) {
if (src.remaining() >= 4) {
int len = src.getInt();
if (len < 1) {
setHeartBeat(len);
return true;
}
if (src.remaining() >= len) {
src.markPL();
src.limit(len + src.position());
try {
CharsetDecoder decoder = context.getEncoding().newDecoder();
this.readText = decoder.decode(src.nioBuffer()).toString();
} finally {
src.reset();
src.skipBytes(len);
}
return true;
} else {
header_complete = true;
buf = allocate(channel, len);
buf.read(src);
this.buf = buf;
}
} else {
buf = allocate(channel, 4);
buf.read(src);
this.buf = buf;
}
return false;
} else {
if (!header_complete) {
buf.read(src);
if (buf.hasRemaining()) {
return false;
}
header_complete = true;
buf.flip();
int len = buf.getInt();
if (len < 1) {
setHeartBeat(len);
return true;
}
buf.reallocate(len, limit);
}
buf.read(src);
if (buf.hasRemaining()) {
return false;
}
buf.flip();
CharsetDecoder decoder = context.getEncoding().newDecoder();
this.readText = decoder.decode(buf.nioBuffer()).toString();
return true;
}
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Http2ProtocolEncoder method encode.
@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
ByteBufAllocator allocator = channel.getByteBufAllocator();
Http2Frame frame = (Http2Frame) future;
Http2FrameType frameType = frame.getHttp2FrameType();
byte[] payload = null;
switch(frameType) {
case FRAME_TYPE_CONTINUATION:
break;
case FRAME_TYPE_DATA:
break;
case FRAME_TYPE_FRAME_HEADER:
break;
case FRAME_TYPE_GOAWAY:
break;
case FRAME_TYPE_HEADERS:
Http2HeadersFrame hf = (Http2HeadersFrame) frame;
break;
case FRAME_TYPE_PING:
break;
case FRAME_TYPE_PREFACE:
break;
case FRAME_TYPE_PRIORITY:
break;
case FRAME_TYPE_PUSH_PROMISE:
break;
case FRAME_TYPE_RST_STREAM:
break;
case FRAME_TYPE_SETTINGS:
Http2SettingsFrame sf = (Http2SettingsFrame) frame;
long[] settings = sf.getSettings();
payload = new byte[6 * 6];
for (int i = 0; i < 6; i++) {
int realI = i + 1;
int offset = i * 6;
MathUtil.unsignedShort2Byte(payload, realI, offset);
MathUtil.unsignedInt2Byte(payload, settings[realI], offset + 2);
}
break;
case FRAME_TYPE_WINDOW_UPDATE:
break;
default:
break;
}
int length = payload.length;
ByteBuf buf = allocator.allocate(length + Http2ProtocolDecoder.PROTOCOL_HEADER);
byte b2 = (byte) ((length & 0xff));
byte b1 = (byte) ((length >> 8 * 1) & 0xff);
byte b0 = (byte) ((length >> 8 * 2) & 0xff);
byte b3 = frameType.getByteValue();
buf.putByte(b0);
buf.putByte(b1);
buf.putByte(b2);
buf.putByte(b3);
buf.putByte((byte) 0);
buf.putInt(frame.getHeader().getStreamIdentifier());
buf.put(payload);
future.setByteBuf(buf.flip());
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Http2FrameHeaderImpl method read.
@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
ByteBuf buf = this.buf;
if (!header_complete) {
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
header_complete = true;
doHeaderComplete(channel, buf.flip());
}
return frame.read(channel, buffer);
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Http2SettingsFrameImpl 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;
}
isComplete = true;
doComplete(channel, buf.flip());
}
return true;
}
Aggregations