Search in sources :

Example 6 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress 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 7 with DomainSocketAddress

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

the class EpollDomainDatagramChannel method filterOutboundMessage.

@Override
protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof DomainDatagramPacket) {
        DomainDatagramPacket packet = (DomainDatagramPacket) msg;
        ByteBuf content = packet.content();
        return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DomainDatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg;
    }
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        return UnixChannelUtil.isBufferCopyNeededForWrite(buf) ? newDirectBuffer(buf) : buf;
    }
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
        if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof DomainSocketAddress)) {
            ByteBuf content = (ByteBuf) e.content();
            return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DefaultAddressedEnvelope<ByteBuf, DomainSocketAddress>(newDirectBuffer(e, content), (DomainSocketAddress) e.recipient()) : e;
        }
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) DomainDatagramPacket(io.netty.channel.unix.DomainDatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) DomainDatagramSocketAddress(io.netty.channel.unix.DomainDatagramSocketAddress) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress)

Example 8 with DomainSocketAddress

use of io.netty.channel.unix.DomainSocketAddress 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 9 with DomainSocketAddress

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

the class KQueueDomainDatagramChannel method filterOutboundMessage.

@Override
protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof DomainDatagramPacket) {
        DomainDatagramPacket packet = (DomainDatagramPacket) msg;
        ByteBuf content = packet.content();
        return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DomainDatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg;
    }
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        return UnixChannelUtil.isBufferCopyNeededForWrite(buf) ? newDirectBuffer(buf) : buf;
    }
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
        if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof DomainSocketAddress)) {
            ByteBuf content = (ByteBuf) e.content();
            return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DefaultAddressedEnvelope<ByteBuf, DomainSocketAddress>(newDirectBuffer(e, content), (DomainSocketAddress) e.recipient()) : e;
        }
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) DomainDatagramPacket(io.netty.channel.unix.DomainDatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) DomainDatagramSocketAddress(io.netty.channel.unix.DomainDatagramSocketAddress) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress)

Example 10 with DomainSocketAddress

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

the class LinuxSocketTest method testUnixDomainSocketTooLongPathFails.

@Test
public void testUnixDomainSocketTooLongPathFails() throws IOException {
    // Most systems has a limit for UDS path of 108, 255 is generally too long.
    StringBuilder socketPath = new StringBuilder("/tmp/");
    while (socketPath.length() < 255) {
        socketPath.append(UUID.randomUUID());
    }
    final DomainSocketAddress domainSocketAddress = new DomainSocketAddress(socketPath.toString());
    final Socket socket = Socket.newSocketDomain();
    try {
        Exception exception = Assertions.assertThrows(NativeIoException.class, new Executable() {

            @Override
            public void execute() throws Throwable {
                socket.bind(domainSocketAddress);
            }
        });
        Assertions.assertTrue(exception.getMessage().contains("too long"));
    } finally {
        socket.close();
    }
}
Also used : DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Executable(org.junit.jupiter.api.function.Executable) Socket(io.netty.channel.unix.Socket) NativeIoException(io.netty.channel.unix.Errors.NativeIoException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)23 File (java.io.File)11 InetSocketAddress (java.net.InetSocketAddress)9 SocketAddress (java.net.SocketAddress)8 IOException (java.io.IOException)7 Test (org.junit.Test)6 ByteString (com.google.protobuf.ByteString)5 ByteBuf (io.netty.buffer.ByteBuf)4 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)4 Test (org.junit.jupiter.api.Test)4 PeerCredentials (io.netty.channel.unix.PeerCredentials)3 SocketTest (io.netty.channel.unix.tests.SocketTest)3 DomainSocketAddress (org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.unix.DomainSocketAddress)3 DomainDatagramPacket (io.netty.channel.unix.DomainDatagramPacket)2 DomainDatagramSocketAddress (io.netty.channel.unix.DomainDatagramSocketAddress)2 IovArray (io.netty.channel.unix.IovArray)2 ByteBuffer (java.nio.ByteBuffer)2 GrpcServerBuilder (alluxio.grpc.GrpcServerBuilder)1 HostAndPort (com.google.common.net.HostAndPort)1