Search in sources :

Example 1 with CompositeByteBuf

use of io.netty_voltpatches.buffer.CompositeByteBuf in project voltdb by VoltDB.

the class TLSMessagingChannel method writeMessage.

@Override
public int writeMessage(ByteBuffer message) throws IOException {
    if (!message.hasRemaining()) {
        return 0;
    }
    CompositeByteBuf outbuf = Unpooled.compositeBuffer();
    ByteBuf msg = Unpooled.wrappedBuffer(message);
    final int needed = CipherExecutor.framesFor(msg.readableBytes());
    for (int have = 0; have < needed; ++have) {
        final int slicesz = Math.min(CipherExecutor.FRAME_SIZE, msg.readableBytes());
        ByteBuf clear = msg.readSlice(slicesz).writerIndex(slicesz);
        ByteBuf encr = m_ce.allocator().ioBuffer(packetBufferSize()).writerIndex(packetBufferSize());
        ByteBuffer src = clear.nioBuffer();
        ByteBuffer dst = encr.nioBuffer();
        try {
            m_encrypter.tlswrap(src, dst);
        } catch (TLSException e) {
            outbuf.release();
            encr.release();
            throw new IOException("failed to encrypt tls frame", e);
        }
        assert !src.hasRemaining() : "encryption wrap did not consume the whole source buffer";
        encr.writerIndex(dst.limit());
        outbuf.addComponent(true, encr);
    }
    int bytesWritten = 0;
    try {
        while (outbuf.isReadable()) {
            bytesWritten += outbuf.readBytes(m_socketChannel, outbuf.readableBytes());
        }
    } catch (IOException e) {
        throw e;
    } finally {
        outbuf.release();
    }
    message.position(message.position() + msg.readerIndex());
    return bytesWritten;
}
Also used : CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) TLSException(org.voltcore.network.TLSException) IOException(java.io.IOException) CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) ByteBuf(io.netty_voltpatches.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 2 with CompositeByteBuf

use of io.netty_voltpatches.buffer.CompositeByteBuf in project voltdb by VoltDB.

the class TLSMessagingChannel method readMessage.

@Override
public ByteBuffer readMessage() throws IOException {
    final int appsz = applicationBufferSize();
    ByteBuf readbuf = m_ce.allocator().ioBuffer(packetBufferSize());
    CompositeByteBuf msgbb = Unpooled.compositeBuffer();
    try {
        ByteBuf clear = m_ce.allocator().buffer(appsz).writerIndex(appsz);
        ByteBuffer src, dst;
        do {
            readbuf.clear();
            if (!m_decrypter.readTLSFrame(m_socketChannel, readbuf)) {
                return null;
            }
            src = readbuf.nioBuffer();
            dst = clear.nioBuffer();
        } while (m_decrypter.tlsunwrap(src, dst) == 0);
        msgbb.addComponent(true, clear.writerIndex(dst.limit()));
        int needed = msgbb.readableBytes() >= 4 ? validateLength(msgbb.readInt()) : NOT_AVAILABLE;
        while (msgbb.readableBytes() < (needed == NOT_AVAILABLE ? 4 : needed)) {
            clear = m_ce.allocator().buffer(appsz).writerIndex(appsz);
            do {
                readbuf.clear();
                if (!m_decrypter.readTLSFrame(m_socketChannel, readbuf)) {
                    return null;
                }
                src = readbuf.nioBuffer();
                dst = clear.nioBuffer();
            } while (m_decrypter.tlsunwrap(src, dst) == 0);
            msgbb.addComponent(true, clear.writerIndex(dst.limit()));
            if (needed == NOT_AVAILABLE && msgbb.readableBytes() >= 4) {
                needed = validateLength(msgbb.readInt());
            }
        }
        ByteBuffer retbb = ByteBuffer.allocate(needed);
        msgbb.readBytes(retbb);
        msgbb.discardReadComponents();
        assert !msgbb.isReadable() : "read from unblocked channel that received multiple messages?";
        return (ByteBuffer) retbb.flip();
    } finally {
        readbuf.release();
        msgbb.release();
    }
}
Also used : CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) ByteBuf(io.netty_voltpatches.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuf (io.netty_voltpatches.buffer.ByteBuf)2 CompositeByteBuf (io.netty_voltpatches.buffer.CompositeByteBuf)2 ByteBuffer (java.nio.ByteBuffer)2 IOException (java.io.IOException)1 TLSException (org.voltcore.network.TLSException)1