Search in sources :

Example 1 with ByteBuf

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

Example 2 with ByteBuf

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

Example 3 with ByteBuf

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

Example 4 with ByteBuf

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

Example 5 with ByteBuf

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();
    }
}
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