Search in sources :

Example 1 with ByteBufAllocator

use of com.firenio.buffer.ByteBufAllocator 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 2 with ByteBufAllocator

use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class TestExpansion method arrayPool.

@Test
public void arrayPool() throws Exception {
    ByteBufAllocator alloc = TestAllocUtil.heap(1024 * 4);
    ByteBuf buf = alloc.allocate(2);
    for (int i = 0; i < 10; i++) {
        buf.writeBytes(data);
        buf.writeByte(a);
        alloc.allocate(1);
    }
    Assert.assertTrue(v(buf));
}
Also used : PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 3 with ByteBufAllocator

use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class TestExpansion method directPool.

@Test
public void directPool() throws Exception {
    ByteBufAllocator alloc = TestAllocUtil.direct(1024 * 4);
    ByteBuf buf = alloc.allocate(2);
    for (int i = 0; i < 10; i++) {
        buf.writeBytes(data);
        buf.writeByte(a);
        alloc.allocate(1);
    }
    Assert.assertTrue(v(buf));
}
Also used : PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 4 with ByteBufAllocator

use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class TestDuplicatedHeapByteBuf method testDirectP.

@Test
public void testDirectP() throws Exception {
    ByteBufAllocator a = TestAllocUtil.direct();
    ByteBuf buf = a.allocate(16);
    buf.writeBytes(data.getBytes());
    ByteBuf buf2 = buf.duplicate();
    v(buf2);
}
Also used : ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 5 with ByteBufAllocator

use of com.firenio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class TestDuplicatedHeapByteBuf method testHeapP.

@Test
public void testHeapP() throws Exception {
    ByteBufAllocator a = TestAllocUtil.heap();
    ByteBuf buf = a.allocate(16);
    buf.writeBytes(data.getBytes());
    ByteBuf buf2 = buf.duplicate();
    v(buf2);
}
Also used : ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)5 ByteBufAllocator (com.firenio.buffer.ByteBufAllocator)5 Test (org.junit.Test)4 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)2 IOException (java.io.IOException)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLEngineResult (javax.net.ssl.SSLEngineResult)1 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)1 Status (javax.net.ssl.SSLEngineResult.Status)1