use of io.netty.channel.socket.DatagramChannel in project netty by netty.
the class NioDatagramChannelTest method testBindMultiple.
/**
* Test try to reproduce issue #1335
*/
@Test
public void testBindMultiple() throws Exception {
DefaultChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
NioEventLoopGroup group = new NioEventLoopGroup();
try {
for (int i = 0; i < 100; i++) {
Bootstrap udpBootstrap = new Bootstrap();
udpBootstrap.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Discard
ReferenceCountUtil.release(msg);
}
});
DatagramChannel datagramChannel = (DatagramChannel) udpBootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
channelGroup.add(datagramChannel);
}
Assert.assertEquals(100, channelGroup.size());
} finally {
channelGroup.close().sync();
group.shutdownGracefully().sync();
}
}
use of io.netty.channel.socket.DatagramChannel in project opennms by OpenNMS.
the class UdpListener method start.
public void start() throws InterruptedException {
bossGroup = new NioEventLoopGroup();
final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(maxPacketSize)).handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
// Wrap the contents of the packet in a ByteBuffer, referencing
// the underlying byte array if possible
final ByteBuffer buffer = wrapContentsWithNioByteBuffer(packet);
// Build the message to dispatch via the Sink API
final TelemetryMessage msg = new TelemetryMessage(packet.sender(), buffer);
// Dispatch and retain a reference to the packet
// in the case that we are sharing the underlying byte array
final CompletableFuture<TelemetryMessage> future = dispatcher.send(msg);
packet.retain();
future.whenComplete((res, ex) -> packet.release());
}
});
}
});
future = b.bind(host, port).await();
}
use of io.netty.channel.socket.DatagramChannel in project opennms by OpenNMS.
the class UdpListener method start.
public void start() throws InterruptedException {
bossGroup = new NioEventLoopGroup();
final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler());
ch.pipeline().addLast(new MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
msg.retain();
out.add(msg.content());
}
});
ch.pipeline().addLast(new XmlFrameDecoder(2147483647));
ch.pipeline().addLast(new XmlEventProcessor(eventIpcManager));
}
});
future = b.bind(config.getUDPIpAddress(), config.getUDPPort()).await();
}
use of io.netty.channel.socket.DatagramChannel in project reactor-netty by reactor.
the class UdpClient method newHandler.
@Override
public Mono<? extends NettyContext> newHandler(BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> handler) {
final BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> targetHandler = null == handler ? ChannelOperations.noopHandler() : handler;
return Mono.create(sink -> {
Bootstrap b = options.get();
SocketAddress adr = options.getAddress();
if (adr == null) {
sink.error(new NullPointerException("Provided UdpClientOptions do not " + "define any address to bind to "));
return;
}
b.remoteAddress(adr);
ContextHandler<DatagramChannel> c = doHandler(targetHandler, sink, adr);
b.handler(c);
c.setFuture(b.connect());
});
}
use of io.netty.channel.socket.DatagramChannel in project traccar by tananaev.
the class EskyProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_SATELLITES, parser.nextInt());
position.setValid(true);
position.setTime(parser.nextDateTime());
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setSpeed(UnitsConverter.knotsFromMps(parser.nextDouble()));
position.setCourse(parser.nextDouble());
if (parser.hasNext(3)) {
int input = parser.nextHexInt();
position.set(Position.KEY_IGNITION, !BitUtil.check(input, 0));
position.set(Position.PREFIX_IN + 1, !BitUtil.check(input, 1));
position.set(Position.PREFIX_IN + 2, !BitUtil.check(input, 2));
position.set(Position.KEY_EVENT, parser.nextInt());
position.set(Position.KEY_ODOMETER, parser.nextInt());
}
position.set(Position.PREFIX_ADC + 1, parser.nextInt());
position.set(Position.KEY_BATTERY, parser.nextInt() * 0.01);
int index = sentence.lastIndexOf('+');
if (index > 0 && channel instanceof DatagramChannel) {
channel.writeAndFlush(new NetworkMessage("ACK," + sentence.substring(index + 1) + "#", remoteAddress));
}
return position;
}
Aggregations