use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method close_ssl0.
private void close_ssl0() {
// Fire channelEstablish to tell connector the reason of close.
if (!ssl_handshake_finished) {
context.channelEstablish(this, SSL_HANDSHAKE_FAILED);
}
// Indicate that application is done with engine
if (SslContext.OPENSSL_AVAILABLE && OpenSslHelper.isOpensslEngineDestroyed(ssl_engine)) {
return;
}
ssl_engine.closeOutbound();
if (!ssl_engine.isOutboundDone()) {
try {
ByteBuf out = wrap(ByteBuf.empty());
write_buf_queue.offer(out);
write();
} catch (Throwable e) {
debugException(logger, e);
}
}
try {
if (SslContext.OPENSSL_AVAILABLE) {
// Set ReceivedShutdown to true to shutdown ssl quiet.
OpenSslHelper.setOpensslEngineReceivedShutdown(ssl_engine);
}
ssl_engine.closeInbound();
} catch (Throwable e) {
debugException(logger, e);
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method write.
/**
* Write a ByteBuf to channel, this method will not tell you write result,
* the buf will auto released if write failed.
*/
public void write(ByteBuf buf) {
if (buf != null) {
if (enable_ssl) {
ByteBuf old = buf;
try {
buf = wrap(old);
} catch (Throwable e) {
debugException(logger, e);
} finally {
old.release();
}
}
int write_size = buf.readableBytes();
if (isPendingOverflow(write_size)) {
buf.release();
close();
return;
}
Queue<ByteBuf> write_buf_queue = this.write_buf_queue;
write_buf_queue.offer(buf);
if (!isOpen() && write_buf_queue.remove(buf)) {
buf.release();
}
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method wrap.
private ByteBuf wrap(ByteBuf src) throws IOException {
SSLEngine engine = getSSLEngine();
ByteBufAllocator alloc = alloc();
ByteBuf out = null;
try {
if (ssl_handshake_finished) {
byte sslWrapExt = this.ssl_wrap_ext;
if (sslWrapExt == 0) {
out = alloc.allocate(guess_wrap_out(src.readableBytes(), 0xff + 1));
} else {
out = alloc.allocate(guess_wrap_out(src.readableBytes(), sslWrapExt & 0xff));
}
final int SSL_PACKET_BUFFER_SIZE = SslContext.SSL_PACKET_BUFFER_SIZE;
for (; ; ) {
SSLEngineResult result = engine.wrap(src.nioReadBuffer(), out.nioWriteBuffer());
Status status = result.getStatus();
sync_buf(src, out);
if (status == Status.CLOSED) {
return out;
} else if (status == Status.BUFFER_OVERFLOW) {
out.expansion(out.capacity() + SSL_PACKET_BUFFER_SIZE);
continue;
} else {
if (src.hasReadableBytes()) {
continue;
}
if (sslWrapExt == 0) {
int srcLen = src.writeIndex();
int outLen = out.readIndex();
int y = ((srcLen + 1) / SSL_PACKET_BUFFER_SIZE) + 1;
int u = ((outLen - srcLen) / y) * 2;
this.ssl_wrap_ext = (byte) u;
}
return out;
}
}
} else {
ByteBuf dst = FastThreadLocal.get().getSslWrapBuf();
for (; ; ) {
dst.clear();
SSLEngineResult result = engine.wrap(src.nioReadBuffer(), dst.nioWriteBuffer());
Status status = result.getStatus();
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
sync_buf(src, dst);
if (status == Status.CLOSED) {
return swap(alloc, dst);
}
if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
if (out != null) {
out.writeBytes(dst);
return out;
}
return swap(alloc, dst);
} else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
if (out == null) {
out = alloc.allocate(256);
}
out.writeBytes(dst);
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
finish_handshake();
if (out != null) {
out.writeBytes(dst);
return out;
}
return swap(alloc, dst);
} else if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
// It is shouldn't here to have "NOT_HANDSHAKING", because of the ssl is closed?
throw SSL_WRAP_CLOSED;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
run_delegated_tasks(engine);
continue;
}
}
}
} catch (Throwable e) {
release(out);
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException(e);
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method read_ssl_remain.
protected void read_ssl_remain(ByteBuf dst) {
ByteBuf remain = this.ssl_remain_buf;
if (remain != null) {
dst.writeBytes(remain);
remain.release();
this.ssl_remain_buf = null;
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method release_wb_queue.
private void release_wb_queue() {
Queue<ByteBuf> wfs = this.write_buf_queue;
ByteBuf buf = wfs.poll();
for (; buf != null; ) {
release(buf);
buf = wfs.poll();
}
}
Aggregations