/** * Binds to listen for data grams on the given local IPAddress/port * * @param nicIPAddress - if null will listen on wildcard address, which * means datagrams will be received on all local network interfaces. * Otherwise, will bind to the provided IP address associated with some NIC. * @param port - the port to listen on * @param receiveBufferSize - the number of bytes to request for a receive * buffer from OS * @throws IOException if unable to add channel */ public void addDatagramChannel(final InetAddress nicIPAddress, final int port, final int receiveBufferSize) throws IOException { final DatagramChannel dChannel = createAndBindDatagramChannel(nicIPAddress, port, receiveBufferSize); dChannel.register(socketChannelSelector, SelectionKey.OP_READ); }
private void run() throws IOException { receiveChannel = DatagramChannel.open(); Common.init(receiveChannel); receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT)); final DatagramChannel sendChannel = DatagramChannel.open(); Common.init(sendChannel); final Selector selector = Selector.open(); receiveChannel.register(selector, OP_READ, this); final NioSelectedKeySet keySet = Common.keySet(selector); final AtomicBoolean running = new AtomicBoolean(true); SigInt.register(() -> running.set(false)); while (running.get()) { measureRoundTrip(HISTOGRAM, SEND_ADDRESS, buffer, sendChannel, selector, keySet, running); HISTOGRAM.reset(); System.gc(); LockSupport.parkNanos(1000 * 1000 * 1000); } }
}; receiveChannel.register(selector, OP_READ, handler);
public SelectionKey registerForRead(final SendChannelEndpoint transport) { SelectionKey key = null; try { key = transport.receiveDatagramChannel().register(selector, SelectionKey.OP_READ, transport); transports = ArrayUtil.add(transports, transport); } catch (final ClosedChannelException ex) { LangUtil.rethrowUnchecked(ex); } return key; }
}; receiveChannel.register(selector, OP_READ, null);
}; receiveChannel.register(selector, OP_READ, handler);
public void bind (Selector selector, InetSocketAddress localPort) throws IOException { close(); readBuffer.clear(); writeBuffer.clear(); try { datagramChannel = selector.provider().openDatagramChannel(); datagramChannel.socket().bind(localPort); datagramChannel.configureBlocking(false); selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ); lastCommunicationTime = System.currentTimeMillis(); } catch (IOException ex) { close(); throw ex; } }
dChannel.register(socketChannelSelector, SelectionKey.OP_READ);
public SelectionKey registerForRead( final ReceiveChannelEndpoint channelEndpoint, final UdpChannelTransport transport, final int transportIndex) { SelectionKey key = null; try { final ChannelAndTransport channelAndTransport = new ChannelAndTransport(channelEndpoint, transport, transportIndex); key = transport.receiveDatagramChannel().register(selector, SelectionKey.OP_READ, channelAndTransport); channelAndTransports = ArrayUtil.add(channelAndTransports, channelAndTransport); } catch (final ClosedChannelException ex) { LangUtil.rethrowUnchecked(ex); } return key; }
public void connect (Selector selector, InetSocketAddress remoteAddress) throws IOException { close(); readBuffer.clear(); writeBuffer.clear(); try { datagramChannel = selector.provider().openDatagramChannel(); datagramChannel.socket().bind(null); datagramChannel.socket().connect(remoteAddress); datagramChannel.configureBlocking(false); selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ); lastCommunicationTime = System.currentTimeMillis(); connectedAddress = remoteAddress; } catch (IOException ex) { close(); IOException ioEx = new IOException("Unable to connect to: " + remoteAddress); ioEx.initCause(ex); throw ioEx; } }
@Override public void open(final InetAddress nicAddress, final int port, final int maxBufferSize) throws IOException { stopped = false; datagramChannel = DatagramChannel.open(); datagramChannel.configureBlocking(false); if (maxBufferSize > 0) { datagramChannel.setOption(StandardSocketOptions.SO_RCVBUF, maxBufferSize); final int actualReceiveBufSize = datagramChannel.getOption(StandardSocketOptions.SO_RCVBUF); if (actualReceiveBufSize < maxBufferSize) { logger.warn("Attempted to set Socket Buffer Size to " + maxBufferSize + " bytes but could only set to " + actualReceiveBufSize + "bytes. You may want to consider changing the Operating System's " + "maximum receive buffer"); } } // we don't have to worry about nicAddress being null here because InetSocketAddress already handles it datagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); datagramChannel.socket().bind(new InetSocketAddress(nicAddress, port)); // if a sending host and port were provided then connect to that specific address to only receive // datagrams from that host/port, otherwise we can receive datagrams from any host/port if (sendingHost != null && sendingPort != null) { datagramChannel.connect(new InetSocketAddress(sendingHost, sendingPort)); } selector = Selector.open(); datagramChannel.register(selector, SelectionKey.OP_READ); }
channel.getDatagramChannel().register( selector, channel.getInternalInterestOps(), channel);
@Override public void doExecute(Selector selector) throws IOException { channel.register(selector, SelectionKey.OP_READ, endpoint); } });
@Deprecated public SelectionKey open(DatagramChannel dataChannel, Channel channel) throws IOException { // Get a selector int index = currSelectorIndex.getAndIncrement(); Selector selector = selectors.get(index % selectors.size()); // Register the channel under the chosen selector SelectionKey key = dataChannel.register(selector, SelectionKey.OP_READ); // Attach the multiplexer to the key key.attach(channel); return key; }
@Deprecated public void open(DatagramChannel channel, ProtocolHandler handler) throws IOException { // Get a selector int index = currSelectorIndex.getAndIncrement(); Selector selector = selectors.get(index % selectors.size()); // Register the channel under the chosen selector SelectionKey key = channel.register(selector, SelectionKey.OP_READ); // Attach the protocol handler to the key key.attach(handler); handler.setKey(key); }
@Deprecated public void open(DatagramChannel channel, ProtocolHandler handler) throws IOException { // Get a selector int index = currSelectorIndex.getAndIncrement(); Selector selector = selectors.get(index % selectors.size()); // Register the channel under the chosen selector SelectionKey key = channel.register(selector, SelectionKey.OP_READ); // Attach the protocol handler to the key key.attach(handler); handler.setKey(key); }
@Deprecated public void open(DatagramChannel channel, ProtocolHandler handler) throws IOException { // Get a selector int index = currSelectorIndex.getAndIncrement(); Selector selector = selectors.get(index % selectors.size()); // Register the channel under the chosen selector SelectionKey key = channel.register(selector, SelectionKey.OP_READ); // Attach the protocol handler to the key key.attach(handler); handler.setKey(key); }
public SelectionKey open(Channel channel) throws IOException { DatagramChannel dataChannel = DatagramChannel.open(); dataChannel.configureBlocking(false); int index = currSelectorIndex.getAndIncrement(); SelectionKey key = dataChannel.register(selectors.get(index % selectors.size()), SelectionKey.OP_READ); key.attach(channel); return key; }
private void listenUDP() throws IOException { if (udpPort > 0) { log.info("Binding UDP to " + udpPort); DatagramChannel udp = DatagramChannel.open(); udp.socket().bind(new InetSocketAddress(udpPort)); udp.configureBlocking(false); udp.register(acceptSelector, SelectionKey.OP_READ); udp.register(sendSelector, SelectionKey.OP_WRITE); } }
public Reader(DatagramChannel channel, LFXMessageRouter router) throws IOException { this.router = router; channel.configureBlocking(false); channel.socket().bind(new InetSocketAddress(PORT)); selector = Selector.open(); channel.register(selector, SelectionKey.OP_READ); }