use of io.netty_voltpatches.buffer.ByteBuf in project voltdb by VoltDB.
the class TLSNIOWriteStream method serializeQueuedWrites.
@Override
int serializeQueuedWrites(NetworkDBBPool pool) throws IOException {
checkForGatewayExceptions();
final int frameMax = Math.min(CipherExecutor.FRAME_SIZE, applicationBufferSize());
int processedWrites = 0;
final Deque<DeferredSerialization> oldlist = getQueuedWrites();
if (oldlist.isEmpty())
return 0;
ByteBuf accum = m_ce.allocator().buffer(frameMax).clear();
DeferredSerialization ds = null;
int bytesQueued = 0;
int frameMsgs = 0;
while ((ds = oldlist.poll()) != null) {
++processedWrites;
final int serializedSize = ds.getSerializedSize();
if (serializedSize == DeferredSerialization.EMPTY_MESSAGE_LENGTH)
continue;
// the encryption gateway
if (serializedSize > frameMax) {
// messages and an incomplete partial fragment of one
if (accum.writerIndex() > 0) {
m_ecryptgw.offer(new EncryptFrame(accum, frameMsgs));
frameMsgs = 0;
bytesQueued += accum.writerIndex();
accum = m_ce.allocator().buffer(frameMax).clear();
}
ByteBuf big = m_ce.allocator().buffer(serializedSize).writerIndex(serializedSize);
ByteBuffer jbb = big.nioBuffer();
ds.serialize(jbb);
checkSloppySerialization(jbb, ds);
bytesQueued += big.writerIndex();
m_ecryptgw.offer(new EncryptFrame(big, 1));
frameMsgs = 0;
continue;
} else if (accum.writerIndex() + serializedSize > frameMax) {
m_ecryptgw.offer(new EncryptFrame(accum, frameMsgs));
frameMsgs = 0;
bytesQueued += accum.writerIndex();
accum = m_ce.allocator().buffer(frameMax).clear();
}
ByteBuf packet = accum.slice(accum.writerIndex(), serializedSize);
ByteBuffer jbb = packet.nioBuffer();
ds.serialize(jbb);
checkSloppySerialization(jbb, ds);
accum.writerIndex(accum.writerIndex() + serializedSize);
++frameMsgs;
}
if (accum.writerIndex() > 0) {
m_ecryptgw.offer(new EncryptFrame(accum, frameMsgs));
bytesQueued += accum.writerIndex();
} else {
accum.release();
}
updateQueued(bytesQueued, true);
return processedWrites;
}
use of io.netty_voltpatches.buffer.ByteBuf in project voltdb by VoltDB.
the class TLSVoltPort method run.
@Override
public void run() throws IOException {
try {
do {
checkForGatewayExceptions();
/*
* Have the read stream fill from the network
*/
if (readyForRead()) {
final int maxRead = getMaxRead();
if (maxRead > 0) {
int read = fillReadStream(maxRead);
if (read > 0) {
ByteBuf frameHeader = Unpooled.wrappedBuffer(new byte[TLS_HEADER_SIZE]);
while (readStream().dataAvailable() >= TLS_HEADER_SIZE) {
NIOReadStream rdstrm = readStream();
rdstrm.peekBytes(frameHeader.array());
m_needed = frameHeader.getShort(3) + TLS_HEADER_SIZE;
if (rdstrm.dataAvailable() < m_needed)
break;
m_dcryptgw.offer(rdstrm.getSlice(m_needed));
m_needed = NOT_AVAILABLE;
}
}
}
}
if (m_network.isStopping() || m_isShuttingDown) {
waitForPendingDecrypts();
}
ByteBuffer message = null;
while ((message = m_decrypted.poll()) != null) {
++m_messagesRead;
m_handler.handleMessage(message, this);
}
/*
* On readiness selection, optimistically assume that write will succeed,
* in the common case it will
*/
drainEncryptedStream();
/*
* some encrypt or decrypt task may have finished while this port is running
* so enabling write interest would have been muted. Signal is there to
* reconsider finished decrypt or encrypt tasks.
*/
} while (m_signal.compareAndSet(true, false));
} finally {
synchronized (m_lock) {
assert (m_running == true);
m_running = false;
}
}
}
use of io.netty_voltpatches.buffer.ByteBuf 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;
}
use of io.netty_voltpatches.buffer.ByteBuf 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();
}
}
use of io.netty_voltpatches.buffer.ByteBuf in project voltdb by VoltDB.
the class SSLBufferDecrypter method readTLSFrame.
public boolean readTLSFrame(ScatteringByteChannel chn, ByteBuf buf) throws IOException {
int widx = buf.writerIndex();
ByteBuf header = buf.slice(widx, TLS_HEADER_SIZE).clear();
int rc = 0;
do {
rc = header.writeBytes(chn, header.writableBytes());
} while (rc > 0 && header.isWritable());
if (rc < 0) {
throw new IOException("channel closed while reading tls frame header");
}
if (rc == 0)
return false;
int framesz = header.getShort(3);
if (framesz + header.capacity() > buf.writableBytes()) {
throw new IOException("destination buffer is too small to contain the whole frame");
}
buf.writerIndex(buf.writerIndex() + header.writerIndex());
ByteBuf frame = buf.slice(buf.writerIndex(), framesz).clear();
while (frame.isWritable()) {
if (frame.writeBytes(chn, frame.writableBytes()) < 0) {
throw new IOException("channel closed while reading tls frame header");
}
}
buf.writerIndex(buf.writerIndex() + framesz);
return true;
}
Aggregations