Search in sources :

Example 6 with IovArray

use of io.netty.channel.unix.IovArray in project netty by netty.

the class IovArrayTest method testNotFailsWihtoutMemoryAddress.

@Test
public void testNotFailsWihtoutMemoryAddress() {
    ByteBuf buffer = new NoMemoryAddressByteBuf(128);
    IovArray array = new IovArray(buffer);
    ByteBuf buf = Unpooled.directBuffer().writeZero(8);
    ByteBuf buf2 = new NoMemoryAddressByteBuf(8).writeZero(8);
    assertTrue(array.add(buf, 0, buf.readableBytes()));
    assertTrue(array.add(buf, 0, buf2.readableBytes()));
    assertEquals(2, array.count());
    assertEquals(16, array.size());
    assertTrue(buf.release());
    assertTrue(buf2.release());
    assertNotEquals(-1, array.memoryAddress(0));
    array.release();
    assertEquals(0, buffer.refCnt());
}
Also used : IovArray(io.netty.channel.unix.IovArray) ByteBuf(io.netty.buffer.ByteBuf) UnpooledDirectByteBuf(io.netty.buffer.UnpooledDirectByteBuf) Test(org.junit.jupiter.api.Test)

Example 7 with IovArray

use of io.netty.channel.unix.IovArray in project netty by netty.

the class AbstractKQueueStreamChannel method doWriteMultiple.

/**
 * Attempt to write multiple {@link ByteBuf} objects.
 * @param in the collection which contains objects to write.
 * @return The value that should be decremented from the write quantum which starts at
 * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows:
 * <ul>
 *     <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content)
 *     is encountered</li>
 *     <li>1 - if a single call to write data was made to the OS</li>
 *     <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no
 *     data was accepted</li>
 * </ul>
 * @throws Exception If an I/O error occurs.
 */
private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception {
    final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite();
    IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray();
    array.maxBytes(maxBytesPerGatheringWrite);
    in.forEachFlushedMessage(array);
    if (array.count() >= 1) {
        // TODO: Handle the case where cnt == 1 specially.
        return writeBytesMultiple(in, array);
    }
    // cnt == 0, which means the outbound buffer contained empty buffers only.
    in.removeBytes(0);
    return 0;
}
Also used : IovArray(io.netty.channel.unix.IovArray)

Example 8 with IovArray

use of io.netty.channel.unix.IovArray in project netty by netty.

the class KQueueDatagramChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg) throws Exception {
    final ByteBuf data;
    InetSocketAddress remoteAddress;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, InetSocketAddress> envelope = (AddressedEnvelope<ByteBuf, InetSocketAddress>) msg;
        data = envelope.content();
        remoteAddress = envelope.recipient();
    } else {
        data = (ByteBuf) msg;
        remoteAddress = null;
    }
    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    final long writtenBytes;
    if (data.hasMemoryAddress()) {
        long memoryAddress = data.memoryAddress();
        if (remoteAddress == null) {
            writtenBytes = socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
        } else {
            writtenBytes = socket.sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.getAddress(), remoteAddress.getPort());
        }
    } else if (data.nioBufferCount() > 1) {
        IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray();
        array.add(data, data.readerIndex(), data.readableBytes());
        int cnt = array.count();
        assert cnt != 0;
        if (remoteAddress == null) {
            writtenBytes = socket.writevAddresses(array.memoryAddress(0), cnt);
        } else {
            writtenBytes = socket.sendToAddresses(array.memoryAddress(0), cnt, remoteAddress.getAddress(), remoteAddress.getPort());
        }
    } else {
        ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
        if (remoteAddress == null) {
            writtenBytes = socket.write(nioData, nioData.position(), nioData.limit());
        } else {
            writtenBytes = socket.sendTo(nioData, nioData.position(), nioData.limit(), remoteAddress.getAddress(), remoteAddress.getPort());
        }
    }
    return writtenBytes > 0;
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) IovArray(io.netty.channel.unix.IovArray) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

IovArray (io.netty.channel.unix.IovArray)8 ByteBuf (io.netty.buffer.ByteBuf)5 ByteBuffer (java.nio.ByteBuffer)4 AddressedEnvelope (io.netty.channel.AddressedEnvelope)3 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)3 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)2 UnpooledDirectByteBuf (io.netty.buffer.UnpooledDirectByteBuf)1 ChannelOutboundBuffer (io.netty.channel.ChannelOutboundBuffer)1 InetSocketAddress (java.net.InetSocketAddress)1 Test (org.junit.jupiter.api.Test)1