Search in sources :

Example 1 with IovArray

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

the class AbstractEpollStreamChannel 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 = ((EpollEventLoop) eventLoop()).cleanIovArray();
    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 2 with IovArray

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

the class EpollDomainDatagramChannel method doWriteMessage.

private boolean doWriteMessage(Object msg) throws Exception {
    final ByteBuf data;
    DomainSocketAddress remoteAddress;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, DomainSocketAddress> envelope = (AddressedEnvelope<ByteBuf, DomainSocketAddress>) 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.sendToAddressDomainSocket(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    } else if (data.nioBufferCount() > 1) {
        IovArray array = ((EpollEventLoop) eventLoop()).cleanIovArray();
        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.sendToAddressesDomainSocket(array.memoryAddress(0), cnt, remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    } else {
        ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
        if (remoteAddress == null) {
            writtenBytes = socket.write(nioData, nioData.position(), nioData.limit());
        } else {
            writtenBytes = socket.sendToDomainSocket(nioData, nioData.position(), nioData.limit(), remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    }
    return writtenBytes > 0;
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) IovArray(io.netty.channel.unix.IovArray) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 3 with IovArray

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

the class AbstractEpollChannel method doWriteOrSendBytes.

/**
 * Write bytes to the socket, with or without a remote address.
 * Used for datagram and TCP client fast open writes.
 */
final long doWriteOrSendBytes(ByteBuf data, InetSocketAddress remoteAddress, boolean fastOpen) throws IOException {
    assert !(fastOpen && remoteAddress == null) : "fastOpen requires a remote address";
    if (data.hasMemoryAddress()) {
        long memoryAddress = data.memoryAddress();
        if (remoteAddress == null) {
            return socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
        }
        return socket.sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen);
    }
    if (data.nioBufferCount() > 1) {
        IovArray array = ((EpollEventLoop) eventLoop()).cleanIovArray();
        array.add(data, data.readerIndex(), data.readableBytes());
        int cnt = array.count();
        assert cnt != 0;
        if (remoteAddress == null) {
            return socket.writevAddresses(array.memoryAddress(0), cnt);
        }
        return socket.sendToAddresses(array.memoryAddress(0), cnt, remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen);
    }
    ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
    if (remoteAddress == null) {
        return socket.write(nioData, nioData.position(), nioData.limit());
    }
    return socket.sendTo(nioData, nioData.position(), nioData.limit(), remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen);
}
Also used : IovArray(io.netty.channel.unix.IovArray) ByteBuffer(java.nio.ByteBuffer)

Example 4 with IovArray

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

the class KQueueDomainDatagramChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg) throws Exception {
    final ByteBuf data;
    DomainSocketAddress remoteAddress;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, DomainSocketAddress> envelope = (AddressedEnvelope<ByteBuf, DomainSocketAddress>) 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.sendToAddressDomainSocket(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    } 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.sendToAddressesDomainSocket(array.memoryAddress(0), cnt, remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    } else {
        ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
        if (remoteAddress == null) {
            writtenBytes = socket.write(nioData, nioData.position(), nioData.limit());
        } else {
            writtenBytes = socket.sendToDomainSocket(nioData, nioData.position(), nioData.limit(), remoteAddress.path().getBytes(CharsetUtil.UTF_8));
        }
    }
    return writtenBytes > 0;
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) IovArray(io.netty.channel.unix.IovArray) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 5 with IovArray

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

the class KQueueSocketChannel method doConnect0.

@Override
protected boolean doConnect0(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    if (config.isTcpFastOpenConnect()) {
        ChannelOutboundBuffer outbound = unsafe().outboundBuffer();
        outbound.addFlush();
        Object curr;
        if ((curr = outbound.current()) instanceof ByteBuf) {
            ByteBuf initialData = (ByteBuf) curr;
            // Don't bother with TCP FastOpen if we don't have any initial data to send anyway.
            if (initialData.isReadable()) {
                IovArray iov = new IovArray(config.getAllocator().directBuffer());
                try {
                    iov.add(initialData, initialData.readerIndex(), initialData.readableBytes());
                    int bytesSent = socket.connectx((InetSocketAddress) localAddress, (InetSocketAddress) remoteAddress, iov, true);
                    writeFilter(true);
                    outbound.removeBytes(Math.abs(bytesSent));
                    // So we should return `true` to indicate that connection was established, if it's positive.
                    return bytesSent > 0;
                } finally {
                    iov.release();
                }
            }
        }
    }
    return super.doConnect0(remoteAddress, localAddress);
}
Also used : IovArray(io.netty.channel.unix.IovArray) ByteBuf(io.netty.buffer.ByteBuf) ChannelOutboundBuffer(io.netty.channel.ChannelOutboundBuffer)

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