Search in sources :

Example 46 with ByteBuf

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;
            }
        }
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLEngine(javax.net.ssl.SSLEngine) ByteBuf(com.firenio.buffer.ByteBuf) HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus)

Example 47 with ByteBuf

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

Example 48 with ByteBuf

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

Example 49 with ByteBuf

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

Example 50 with ByteBuf

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

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)58 Test (org.junit.Test)18 ByteBufAllocator (com.firenio.buffer.ByteBufAllocator)5 Channel (com.firenio.component.Channel)5 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)4 Frame (com.firenio.component.Frame)3 IoEventHandle (com.firenio.component.IoEventHandle)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 ChannelConnector (com.firenio.component.ChannelConnector)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 ByteBufAllocatorGroup (com.firenio.buffer.ByteBufAllocatorGroup)1 PoolState (com.firenio.buffer.PooledByteBufAllocator.PoolState)1