use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ProtocolCodec method unwrap.
private ByteBuf unwrap(Channel ch, ByteBuf src) throws IOException {
SSLEngine ssl_engine = ch.getSSLEngine();
ByteBuf dst = FastThreadLocal.get().getSslUnwrapBuf();
if (ch.ssl_handshake_finished) {
ch.read_plain_remain(dst);
SSLEngineResult result = ssl_engine.unwrap(src.nioReadBuffer(), dst.nioWriteBuffer());
if (result.getStatus() == Status.BUFFER_OVERFLOW) {
// file system or others way.
throw SSL_UNWRAP_OVER_LIMIT;
}
ch.sync_buf(src, dst);
return dst;
} else {
for (; ; ) {
dst.clear();
SSLEngineResult result = unwrap(ssl_engine, src, dst);
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
ch.sync_buf(src, dst);
if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
ch.writeAndFlush(ByteBuf.empty());
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
ch.run_delegated_tasks(ssl_engine);
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
ch.finish_handshake();
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
if (src.hasReadableBytes()) {
continue;
}
return null;
} else if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
// see: https://stackoverflow.com/questions/31149383/difference-between-not-handshaking-and-finished
if (SSL_DEBUG) {
logger.error("unwrap handle status: NOT_HANDSHAKING({})", this);
}
throw SSL_UNWRAP_CLOSED;
} else if (result.getStatus() == Status.BUFFER_OVERFLOW) {
throw SSL_UNWRAP_OVER_LIMIT;
}
}
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ProtocolCodec method read_ssl.
protected void read_ssl(Channel ch) throws Exception {
ByteBuf src = ch.getEventLoop().getReadBuf().clear();
for (; ; ) {
src.clear();
ch.read_ssl_remain(src);
if (!read_data(ch, src)) {
return;
}
for (; ; ) {
if (isEnoughSslUnwrap(src)) {
ByteBuf res = unwrap(ch, src);
if (res != null) {
read_buf(ch, res);
}
src.resetWriteIndex();
if (!src.hasReadableBytes()) {
break;
}
} else {
if (src.hasReadableBytes()) {
ch.store_ssl_remain(src);
}
break;
}
}
// for epoll et mode
if (src.hasWritableBytes()) {
break;
}
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ProtocolCodec method read_plain.
protected void read_plain(Channel ch) throws Exception {
ByteBuf dst = ch.getEventLoop().getReadBuf().clear();
for (; ; ) {
ch.read_plain_remain(dst);
if (!read_data(ch, dst)) {
return;
}
read_buf(ch, dst);
// for epoll et mode
if (dst.hasWritableBytes()) {
break;
}
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ProtocolCodec method encode.
// 注意:encode失败要release掉encode过程中申请的内存
public ByteBuf encode(Channel ch, Frame frame) throws Exception {
Object content = frame.getContent();
ByteBuf buf;
if (content instanceof ByteBuf) {
buf = (ByteBuf) content;
} else {
byte[] data = (byte[]) content;
buf = ch.allocateWithSkipHeader(data.length);
buf.writeBytes(data);
}
encode(ch, frame, buf);
return buf;
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method swap.
// FIXME 部分buf不需要swap
private ByteBuf swap(ByteBufAllocator allocator, ByteBuf buf) {
// use writeIndex instead of readableBytes because of the buf readIndex always be zero.
ByteBuf out = allocator.allocate(buf.writeIndex());
out.writeBytes(buf);
return out;
}
Aggregations