Search in sources :

Example 1 with DatagramChannelConfig

use of io.netty.channel.socket.DatagramChannelConfig in project netty by netty.

the class OioDatagramChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannelConfig config = config();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf data = config.getAllocator().heapBuffer(allocHandle.guess());
    boolean free = true;
    try {
        // Ensure we null out the address which may have been set before.
        tmpPacket.setAddress(null);
        tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity());
        socket.receive(tmpPacket);
        InetSocketAddress remoteAddr = (InetSocketAddress) tmpPacket.getSocketAddress();
        allocHandle.lastBytesRead(tmpPacket.getLength());
        buf.add(new DatagramPacket(data.writerIndex(allocHandle.lastBytesRead()), localAddress(), remoteAddr));
        free = false;
        return 1;
    } catch (SocketTimeoutException e) {
        // Expected
        return 0;
    } catch (SocketException e) {
        if (!e.getMessage().toLowerCase(Locale.US).contains("socket closed")) {
            throw e;
        }
        return -1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannelConfig(io.netty.channel.socket.DatagramChannelConfig) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with DatagramChannelConfig

use of io.netty.channel.socket.DatagramChannelConfig in project netty by netty.

the class NioDatagramChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    DatagramChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }
        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), localAddress(), remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannel(java.nio.channels.DatagramChannel) DatagramChannelConfig(io.netty.channel.socket.DatagramChannelConfig) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)2 DatagramChannelConfig (io.netty.channel.socket.DatagramChannelConfig)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1