Refine search
actualCtx = vertx.getOrCreateContext(); channel = transport.datagramChannel(this.dnsServer.getAddress() instanceof Inet4Address ? InternetProtocolFamily.IPv4 : InternetProtocolFamily.IPv6); channel.config().setOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true); MaxMessagesRecvByteBufAllocator bufAllocator = channel.config().getRecvByteBufAllocator(); bufAllocator.maxMessagesPerRead(1); channel.config().setAllocator(PartialPooledByteBufAllocator.INSTANCE); actualCtx.nettyEventLoop().register(channel); if (options.getLogActivity()) { channel.pipeline().addLast("logging", new LoggingHandler()); channel.pipeline().addLast(new DatagramDnsQueryEncoder()); channel.pipeline().addLast(new DatagramDnsResponseDecoder()); channel.pipeline().addLast(new SimpleChannelInboundHandler<DnsResponse>() { @Override protected void channelRead0(ChannelHandlerContext ctx, DnsResponse msg) throws Exception {
@Override public CompletableFuture<Void> stop() { if (channel != null) { CompletableFuture<Void> future = new CompletableFuture<>(); channel.close().addListener(f -> { started.set(false); group.shutdownGracefully(); future.complete(null); }); return future; } started.set(false); return CompletableFuture.completedFuture(null); }
clientBootstrap.bind().addListener((ChannelFutureListener) f -> { if (f.isSuccess()) { clientChannel = (DatagramChannel) f.channel(); log.info("{} joining multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort()); clientChannel.joinGroup(groupAddress, iface).addListener(f2 -> { if (f2.isSuccess()) { log.info("{} successfully joined multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
/** * Closes the internal datagram channel used for sending and receiving DNS messages, and clears all DNS resource * records from the cache. Attempting to send a DNS query or to resolve a domain name will fail once this method * has been called. */ @Override public void close() { if (ch.isOpen()) { ch.close(); } }
@Override public synchronized void close(final Handler<AsyncResult<Void>> handler) { // make sure everything is flushed out on close if (!channel.isOpen()) { return; } channel.flush(); ChannelFuture future = channel.close(); if (handler != null) { future.addListener(new DatagramChannelFutureListener<>(null, handler, context)); } }
@Override public void unicast(Address address, String subject, byte[] payload) { Message message = new Message(this.address, subject, payload); byte[] bytes = SERIALIZER.encode(message); ByteBuf buf = channel.alloc().buffer(4 + bytes.length); buf.writeInt(bytes.length).writeBytes(bytes); channel.writeAndFlush(new DatagramPacket(buf, new InetSocketAddress(address.address(), address.port()))); }
b.channelFactory(channelFactory); b.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true); final DnsResponseHandler responseHandler = new DnsResponseHandler(executor().<Channel>newPromise()); b.handler(new ChannelInitializer<DatagramChannel>() { @Override ch = (DatagramChannel) b.register().channel(); ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(maxPayloadSize)); ch.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) {
final CountDownLatch latch = new CountDownLatch(count); sc = setupServerChannel(sb, bytes, latch); cc = (DatagramChannel) cb.connect(sc.localAddress()).sync().channel(); cc.write(buf.retainedDuplicate()); break; case SLICE: cc.write(buf.retainedSlice()); break; case READ_ONLY: cc.write(buf.retain().asReadOnly()); break; case NONE: cc.write(buf.retain()); break; default: cc.flush(); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(cc.isConnected()); cc.disconnect().syncUninterruptibly(); assertFalse(cc.isConnected()); ChannelFuture future = cc.writeAndFlush( buf.retain().duplicate()).awaitUninterruptibly(); assertTrue("NotYetConnectedException expected, got: " + future.cause(),
void run() { inProgressMap.put(msg.id(), this); timerID = vertx.setTimer(options.getQueryTimeout(), id -> { timerID = -1; actualCtx.runOnContext(v -> { fail(new VertxException("DNS query timeout for " + name)); }); }); channel.writeAndFlush(msg).addListener((ChannelFutureListener) future -> { if (!future.isSuccess()) { actualCtx.executeFromIO(future.cause(), this::fail); } }); }
cb.option(ChannelOption.SO_REUSEADDR, true); Channel sc = sb.bind(newSocketAddress()).sync().channel(); sc.close().awaitUninterruptibly(); return; InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort()); cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync(); cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); sc.close().awaitUninterruptibly(); cc.close().awaitUninterruptibly();
ChannelFuture channelFuture = bootstrap.bind(configuration.getPort()).sync(); channel = channelFuture.channel(); DatagramChannel datagramChannel = (DatagramChannel) channel; String networkInterface = configuration.getNetworkInterface() == null ? LOOPBACK_INTERFACE : configuration.getNetworkInterface(); ObjectHelper.notNull(multicastNetworkInterface, "No network interface found for '" + networkInterface + "'."); LOG.info("ConnectionlessBootstrap joining {}:{} using network interface: {}", configuration.getHost(), configuration.getPort(), multicastNetworkInterface.getName()); datagramChannel.joinGroup(hostAddress, multicastNetworkInterface).syncUninterruptibly(); allChannels.add(datagramChannel); } else {
if (client == null) { try { client = cb.connect(InetAddress.getLoopbackAddress(), port).sync().channel(); client.attr(SESSION_ID).set(sessionId); client.attr(PORT).set(port); if (e instanceof ConnectException) { UscError reply = new UscError(port, sessionId, UscError.ErrorCode.ECONNREFUSED.getCode()); plugin.writeAndFlush(reply); } else if (e instanceof PortUnreachableException) { UscError reply = new UscError(port, sessionId, UscError.ErrorCode.ENETUNREACH.getCode()); plugin.writeAndFlush(reply); } else { UscError reply = new UscError(port, sessionId, UscError.ErrorCode.E_OTHER.getCode()); plugin.writeAndFlush(reply); throw e; plugin.writeAndFlush(data); LOG.trace("UscAgentUdpHandler send TERMINATION_RESPONSE"); plugin.writeAndFlush(data); LOG.trace("UscAgentUdpHandler send ECHO back.");
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { connection.close(); if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).leaveGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { LOGGER.info("leave multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("leave multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } LOGGER.info("disconnect udp channel={}, connection={}", ctx.channel(), connection); }
private void writeQuery(final DnsQuery query, final ChannelPromise writePromise) { final ChannelFuture writeFuture = parent.ch.writeAndFlush(query, writePromise); if (writeFuture.isDone()) { onQueryWriteCompletion(writeFuture); } else { writeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { onQueryWriteCompletion(writeFuture); } }); } }
/** * Join a multicast group. * * @param multicastAddress multicast address of the group to join * * @return a {@link Publisher} that will be complete when the group has been joined */ @Override public Mono<Void> join(final InetAddress multicastAddress, NetworkInterface iface) { if (null == iface && null != datagramChannel.config().getNetworkInterface()) { iface = datagramChannel.config().getNetworkInterface(); } final ChannelFuture future; if (null != iface) { future = datagramChannel.joinGroup(new InetSocketAddress(multicastAddress, datagramChannel.localAddress() .getPort()), iface); } else { future = datagramChannel.joinGroup(multicastAddress); } return FutureMono.from(future) .doOnSuccess(v -> log.info(format(future.channel(), "JOIN {}"), multicastAddress)); }
/** * Leave a multicast group. * * @param multicastAddress multicast address of the group to leave * * @return a {@link Publisher} that will be complete when the group has been left */ @Override public Mono<Void> leave(final InetAddress multicastAddress, NetworkInterface iface) { if (null == iface && null != datagramChannel.config().getNetworkInterface()) { iface = datagramChannel.config().getNetworkInterface(); } final ChannelFuture future; if (null != iface) { future = datagramChannel.leaveGroup(new InetSocketAddress(multicastAddress, datagramChannel.localAddress() .getPort()), iface); } else { future = datagramChannel.leaveGroup(multicastAddress); } return FutureMono.from(future) .doOnSuccess(v -> log.info(format(future.channel(), "JOIN {}"), multicastAddress)); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { connection.init(ctx.channel(), false); if (multicastAddress != null) { ((DatagramChannel) ctx.channel()).joinGroup(multicastAddress, networkInterface, null).addListener(future -> { if (future.isSuccess()) { LOGGER.info("join multicast group success, channel={}, group={}", ctx.channel(), multicastAddress); } else { LOGGER.error("join multicast group error, channel={}, group={}", ctx.channel(), multicastAddress, future.cause()); } }); } LOGGER.info("init udp channel={}", ctx.channel()); }
c.write("QOTM?", new InetSocketAddress("255.255.255.255", port)); if (!c.getCloseFuture().awaitUninterruptibly(5000)) { System.err.println("QOTM request timed out."); c.close().awaitUninterruptibly();
@Override public void close() { bootstrap.group().shutdownGracefully(); if (channel != null) { channel.close().syncUninterruptibly(); } }
@Override protected void initChannel(DatagramChannel ch) throws Exception { ch.pipeline().addLast(DECODER, ENCODER, responseHandler); } });