use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class AbstractChannelFuture method wrapSSL.
private void wrapSSL(SocketChannel channel) throws IOException {
// FIXME 部分情况下可以不在业务线程做wrapssl
ByteBuf old = this.buf;
SslHandler handler = channel.getSslHandler();
try {
this.buf = handler.wrap(channel, old);
this.buf.nioBuffer();
} finally {
ReleaseUtil.release(old);
}
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class SslFutureImpl method read.
@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
if (!header_complete) {
ByteBuf buf = this.buf;
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
header_complete = true;
// SSLv3 or TLS - Check ContentType
short type = buf.getUnsignedByte(0);
if (type < 20 || type > 23) {
throw new SSLException("Neither SSLv3 or TLS");
}
// SSLv3 or TLS - Check ProtocolVersion
int majorVersion = buf.getUnsignedByte(1);
int packetLength = buf.getUnsignedShort(3);
if (majorVersion != 3 || packetLength <= 0) {
throw new SSLException("Neither SSLv3 or TLS");
} else {
// Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
}
buf.reallocate(packetLength + 5, limit, true);
}
ByteBuf buf = this.buf;
buf.read(buffer);
if (buf.hasRemaining()) {
return false;
}
buf.flip();
return true;
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class Lz4CompressedInputStream method read.
@Override
public int read(byte[] b, int off, int len) throws IOException {
ByteBuf buf = this.buf;
byte[] readBuffer = buf.array();
int limit = buf.limit();
int offset = buf.position();
if (buf.remaining() <= 4) {
if (!hasRemaining) {
return -1;
}
read(buf);
return read(b, off, len);
}
int _len = MathUtil.byte2Int(readBuffer, offset);
offset += 4;
if (limit - offset < _len) {
if (!hasRemaining) {
return -1;
}
read(buf);
return read(b, off, len);
}
buf.position(offset + _len);
return Lz4RawDecompressor.decompress(readBuffer, offset, _len, b, off, len);
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class SslChannelByteBufReader method accept.
@Override
public void accept(SocketChannel channel, ByteBuf buffer) throws Exception {
for (; ; ) {
if (!buffer.hasRemaining()) {
return;
}
SslFuture future = channel.getSslReadFuture();
boolean setFutureNull = true;
if (future == null) {
future = this.temporary.reset();
setFutureNull = false;
}
try {
if (!future.read(channel, buffer)) {
if (!setFutureNull) {
if (future == this.temporary) {
future = future.copy(channel);
}
channel.setSslReadFuture(future);
}
return;
}
} catch (Throwable e) {
ReleaseUtil.release(future);
channel.setSslReadFuture(null);
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException("exception occurred when do decode ssl," + e.getMessage(), e);
}
if (setFutureNull) {
channel.setSslReadFuture(null);
}
SslHandler sslHandler = channel.getSslHandler();
ByteBuf product;
try {
product = sslHandler.unwrap(channel, future.getByteBuf());
} finally {
ReleaseUtil.release(future);
}
if (product == null) {
continue;
}
nextAccept(channel, product);
}
}
use of com.generallycloud.baseio.buffer.ByteBuf in project baseio by generallycloud.
the class TransparentByteBufReader2 method accept.
@Override
public void accept(SocketChannel channel, ByteBuf buffer) throws Exception {
for (; ; ) {
if (!buffer.hasRemaining()) {
return;
}
ChannelFuture future = channel.getReadFuture();
boolean setFutureNull = true;
if (future == null) {
// future = channel.getProtocolDecoder().decode(channel, buffer,temporary.clear());
setFutureNull = false;
}
try {
if (!future.read(channel, buffer)) {
if (!setFutureNull) {
if (future.getByteBuf() == this.temporary) {
ByteBuf src = future.getByteBuf();
ByteBuf buf = allocate(channel, src.limit());
buf.read(src.flip());
future.setByteBuf(buf);
}
channel.setReadFuture(future);
}
return;
}
} catch (Throwable e) {
ReleaseUtil.release(future);
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException("exception occurred when do decode," + e.getMessage(), e);
}
if (setFutureNull) {
channel.setReadFuture(null);
}
ReleaseUtil.release(future);
foreReadFutureAcceptor.accept(channel.getSession(), future);
}
}
Aggregations