Refine search
private void onChannelAcceptable(SelectionKey key) throws IOException { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); SelectionKey readKey = socketChannel.register(selector, SelectionKey.OP_READ); readKey.attach(key.attachment()); }
private void selectSocketChannelKeys() throws IOException { // once a channel associated with a key in this selector is 'ready', it causes this select to immediately return. // thus, for each trip through the run() we only get hit with one real timeout...the one in selectServerSocketKeys. int numSelected = socketChannelSelector.select(timeout); if (numSelected == 0) { return; } for (SelectionKey socketChannelKey : socketChannelSelector.selectedKeys()) { final SelectableChannel channel = socketChannelKey.channel(); AbstractChannelReader reader = null; // there are 2 kinds of channels in this selector, both which have their own readers and are executed in their own // threads. We will get here whenever a new SocketChannel is created due to an incoming connection. However, // for a DatagramChannel we don't want to create a new reader unless it is a new DatagramChannel. The only // way to tell if it's new is the lack of an attachment. if (channel instanceof DatagramChannel && socketChannelKey.attachment() == null) { reader = new DatagramChannelReader(UUID.randomUUID().toString(), socketChannelKey, emptyBuffers, factory, readSingleDatagram); socketChannelKey.attach(reader); final ScheduledFuture<?> readerFuture = executor.scheduleWithFixedDelay(reader, 10L, channelReaderFrequencyMilliseconds.get(), TimeUnit.MILLISECONDS); reader.setScheduledFuture(readerFuture); } if (reader != null && LOGGER.isDebugEnabled()) { LOGGER.debug(this + " New Connection established. Server channel: " + channel + " Reader: " + reader); } } }
if ((k.readyOps() & SelectionKey.OP_ACCEPT) != 0) { SocketChannel sc = ((ServerSocketChannel) k .channel()).accept(); InetAddress ia = sc.socket().getInetAddress(); int cnxncount = getClientCnxnCount(ia); SelectionKey.OP_READ); NIOServerCnxn cnxn = createConnection(sc, sk); sk.attach(cnxn); addCnxn(cnxn);
protected void accept( SelectionKey key ) throws IOException { // Would only get accepts on a server channel ServerSocketChannel serverChan = (ServerSocketChannel)key.channel(); // Setup the connection to be non-blocking SocketChannel remoteChan = serverChan.accept(); remoteChan.configureBlocking(false); // And disable Nagle's buffering algorithm... we want // data to go when we put it there. Socket sock = remoteChan.socket(); sock.setTcpNoDelay(true); // Let the selector know we're interested in reading // data from the channel SelectionKey endKey = remoteChan.register( selector, SelectionKey.OP_READ ); // And now create a new endpoint NioEndpoint p = addEndpoint( remoteChan ); endKey.attach(p); endpointKeys.put(p, endKey); }
/** * Tests that a connect and disconnect in a single poll invocation results in the channel id being * in `disconnected`, but not `connected`. */ @Test public void testConnectDisconnectDuringInSinglePoll() throws Exception { // channel is connected, not ready and it throws an exception during prepare KafkaChannel kafkaChannel = mock(KafkaChannel.class); when(kafkaChannel.id()).thenReturn("1"); when(kafkaChannel.socketDescription()).thenReturn(""); when(kafkaChannel.state()).thenReturn(ChannelState.NOT_CONNECTED); when(kafkaChannel.finishConnect()).thenReturn(true); when(kafkaChannel.isConnected()).thenReturn(true); when(kafkaChannel.ready()).thenReturn(false); doThrow(new IOException()).when(kafkaChannel).prepare(); SelectionKey selectionKey = mock(SelectionKey.class); when(kafkaChannel.selectionKey()).thenReturn(selectionKey); when(selectionKey.channel()).thenReturn(SocketChannel.open()); when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_CONNECT); selectionKey.attach(kafkaChannel); Set<SelectionKey> selectionKeys = Utils.mkSet(selectionKey); selector.pollSelectionKeys(selectionKeys, false, System.nanoTime()); assertFalse(selector.connected().contains(kafkaChannel.id())); assertTrue(selector.disconnected().containsKey(kafkaChannel.id())); assertNull(selectionKey.attachment()); verify(kafkaChannel, atLeastOnce()).ready(); verify(kafkaChannel).disconnect(); verify(kafkaChannel).close(); verify(selectionKey).cancel(); }
void doAccept(SelectionKey key) throws InterruptedException, IOException, OutOfMemoryError { ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel; while ((channel = server.accept()) != null) { channel.configureBlocking(false); channel.socket().setTcpNoDelay(tcpNoDelay); channel.socket().setKeepAlive(tcpKeepAlive); Reader reader = getReader(); SimpleServerRpcConnection c = connectionManager.register(channel); // If the connectionManager can't take it, close the connection. if (c == null) { if (channel.isOpen()) { IOUtils.cleanup(null, channel); } continue; } key.attach(c); // so closeCurrentConnection can get the object reader.addConnection(c); } }
key = s.register(selector, s.validOps()); key.attach(i); } catch (Exception e) { stats.incNumConnectErrorServers(); SocketChannel sChannel = (SocketChannel)selKey.channel(); sChannel.finishConnect(); } catch (Exception e) { SocketChannel sChannel = (SocketChannel)selKey.channel(); sChannel.write(sendBuf[index]); } catch (Exception e) { SocketChannel sChannel = (SocketChannel)selKey.channel(); int bytesRead = sChannel.read(recvBuf[index]);
NioPipeStreamConnection(final WorkerThread workerThread, final SelectionKey sourceKey, final SelectionKey sinkKey) { super(workerThread); if (sourceKey != null) { setSourceConduit(sourceConduit = new NioPipeSourceConduit(workerThread, sourceKey, this)); sourceKey.attach(sourceConduit); sourceChannel = (Pipe.SourceChannel) sourceKey.channel(); } else { sourceConduit = null; sourceChannel = null; } if (sinkKey != null) { setSinkConduit(sinkConduit = new NioPipeSinkConduit(workerThread, sinkKey, this)); sinkKey.attach(sinkConduit); sinkChannel = (Pipe.SinkChannel) sinkKey.channel(); } else { sinkConduit = null; sinkChannel = null; } }
while (itr.hasNext()) { SelectionKey serverSocketkey = itr.next(); final SelectableChannel channel = serverSocketkey.channel(); AbstractChannelReader reader = null; if (serverSocketkey.isValid() && serverSocketkey.isAcceptable()) { final ServerSocketChannel ssChannel = (ServerSocketChannel) serverSocketkey.channel(); final SocketChannel sChannel = ssChannel.accept(); if (sChannel != null) { channelReaderFrequencyMilliseconds.get(), TimeUnit.MILLISECONDS); reader.setScheduledFuture(readerFuture); socketChannelKey.attach(reader);
void doAccept(SelectionKey key) throws InterruptedException, IOException, OutOfMemoryError { ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel; while ((channel = server.accept()) != null) { channel.configureBlocking(false); channel.socket().setTcpNoDelay(tcpNoDelay); channel.socket().setKeepAlive(true); Reader reader = getReader(); Connection c = connectionManager.register(channel); // If the connectionManager can't take it, close the connection. if (c == null) { if (channel.isOpen()) { IOUtils.cleanup(null, channel); } connectionManager.droppedConnections.getAndIncrement(); continue; } key.attach(c); // so closeCurrentConnection can get the object reader.addConnection(c); } }
doAccept(SelectionKey key){ //create the new socket SocketChannel socket = ((ServerSocketChannel)key.channel()).accept(); //make it non-blocking as well socket.configureBlocking(false); ... //here you would likely have some code to init your game objects / communication protocol, etc. and generate an identifier object (used below). //and be able to find the socket created above ... //Since it is non blocking it needs a selector as well, and we register for both read and write events SelectionKey socketKey = socket.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE); // so we can identify the events as they come in socketKey.attach(someSocketIndentifier); }