use of java.nio.channels.DatagramChannel 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();
}
}
}
use of java.nio.channels.DatagramChannel in project jeromq by zeromq.
the class TestZLoop method testZLoopWithUDP.
@Test
public void testZLoopWithUDP() throws IOException {
final int port = Utils.findOpenPort();
final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);
ZLoop loop = new ZLoop(ctx);
DatagramChannel udpIn = DatagramChannel.open();
assertThat(udpIn, notNullValue());
udpIn.configureBlocking(false);
udpIn.socket().bind(new InetSocketAddress(port));
DatagramChannel udpOut = DatagramChannel.open();
assertThat(udpOut, notNullValue());
udpOut.configureBlocking(false);
udpOut.socket().connect(addr);
final AtomicInteger counter = new AtomicInteger();
final AtomicBoolean done = new AtomicBoolean();
loop.addPoller(new PollItem(udpIn, ZMQ.Poller.POLLIN), new IZLoopHandler() {
@Override
public int handle(ZLoop loop, PollItem item, Object arg) {
DatagramChannel udpIn = (DatagramChannel) arg;
ByteBuffer bb = ByteBuffer.allocate(3);
try {
udpIn.receive(bb);
String read = new String(bb.array(), 0, bb.limit(), ZMQ.CHARSET);
assertThat(read, is("udp"));
done.set(true);
counter.incrementAndGet();
} catch (IOException e) {
e.printStackTrace();
fail();
}
return -1;
}
}, udpIn);
loop.addPoller(new PollItem(udpOut, ZMQ.Poller.POLLOUT), new IZLoopHandler() {
@Override
public int handle(ZLoop loop, PollItem item, Object arg) {
DatagramChannel udpOut = (DatagramChannel) arg;
try {
ByteBuffer bb = ByteBuffer.allocate(3);
bb.put("udp".getBytes(ZMQ.CHARSET));
bb.flip();
int written = udpOut.send(bb, addr);
assertThat(written, is(3));
counter.incrementAndGet();
} catch (IOException e) {
e.printStackTrace();
fail();
}
return 0;
}
}, udpOut);
loop.start();
assertThat(done.get(), is(true));
assertThat(counter.get(), is(2));
udpIn.close();
udpOut.close();
}
use of java.nio.channels.DatagramChannel in project j2objc by google.
the class DatagramChannelTest method test_getRemoteAddress.
public void test_getRemoteAddress() throws IOException {
InetSocketAddress socketAddress = new InetSocketAddress(Inet4Address.LOOPBACK, 0);
try (DatagramChannel clientChannel = DatagramChannel.open();
DatagramChannel serverChannel = DatagramChannel.open()) {
serverChannel.bind(socketAddress);
assertNull(clientChannel.getRemoteAddress());
clientChannel.connect(serverChannel.getLocalAddress());
assertEquals(socketAddress.getAddress(), ((InetSocketAddress) (clientChannel.getRemoteAddress())).getAddress());
assertEquals(((InetSocketAddress) (serverChannel.getLocalAddress())).getPort(), ((InetSocketAddress) (clientChannel.getRemoteAddress())).getPort());
}
}
use of java.nio.channels.DatagramChannel in project j2objc by google.
the class DatagramChannelTest method test_bounded_harmony6493.
public void test_bounded_harmony6493() throws IOException {
DatagramChannel server = DatagramChannel.open();
InetSocketAddress addr = new InetSocketAddress("localhost", 0);
server.socket().bind(addr);
SocketAddress boundedAddress = server.socket().getLocalSocketAddress();
DatagramChannel client = DatagramChannel.open();
ByteBuffer sent = ByteBuffer.allocate(1024);
sent.put("test".getBytes());
sent.flip();
client.send(sent, boundedAddress);
assertTrue(client.socket().isBound());
server.close();
client.close();
}
use of java.nio.channels.DatagramChannel in project j2objc by google.
the class DatagramChannelTest method testReceive_UnboundBufFull.
public void testReceive_UnboundBufFull() throws Exception {
DatagramChannel dc = DatagramChannel.open();
assertFalse(dc.isConnected());
assertFalse(dc.socket().isBound());
ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE);
// buf is full
dst.put((byte) 88);
assertEquals(dst.position(), dst.limit());
assertNull(dc.receive(dst));
dc.close();
}
Aggregations