Search in sources :

Example 16 with ByteBuf

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);
    }
}
Also used : EmptyByteBuf(com.generallycloud.baseio.buffer.EmptyByteBuf) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) SslHandler(com.generallycloud.baseio.component.ssl.SslHandler)

Example 17 with ByteBuf

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;
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) SSLException(javax.net.ssl.SSLException)

Example 18 with ByteBuf

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);
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 19 with ByteBuf

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);
    }
}
Also used : IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) SslFuture(com.generallycloud.baseio.protocol.SslFuture) SslHandler(com.generallycloud.baseio.component.ssl.SslHandler)

Example 20 with ByteBuf

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);
    }
}
Also used : ChannelFuture(com.generallycloud.baseio.protocol.ChannelFuture) IOException(java.io.IOException) FixedUnpooledByteBuf(com.generallycloud.baseio.buffer.FixedUnpooledByteBuf) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Aggregations

ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)31 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)9 IOException (java.io.IOException)9 EmptyByteBuf (com.generallycloud.baseio.buffer.EmptyByteBuf)4 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)3 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)2 SslHandler (com.generallycloud.baseio.component.ssl.SslHandler)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 FixedUnpooledByteBuf (com.generallycloud.baseio.buffer.FixedUnpooledByteBuf)1 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 SimplyByteBufAllocator (com.generallycloud.baseio.buffer.SimplyByteBufAllocator)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)1 WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)1 Http2Frame (com.generallycloud.baseio.codec.http2.future.Http2Frame)1 Http2FrameType (com.generallycloud.baseio.codec.http2.future.Http2FrameType)1 Http2HeadersFrame (com.generallycloud.baseio.codec.http2.future.Http2HeadersFrame)1