Refine search
public static Selector openSelector() throws IOException { Selector result = null; if (isLinuxPlatform()) { try { final Class<?> providerClazz = Class.forName("sun.nio.ch.EPollSelectorProvider"); if (providerClazz != null) { try { final Method method = providerClazz.getMethod("provider"); if (method != null) { final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null); if (selectorProvider != null) { result = selectorProvider.openSelector(); } } } catch (final Exception e) { log.warn("Open ePoll Selector for linux platform exception", e); } } } catch (final Exception e) { // ignore } } if (result == null) { result = Selector.open(); } return result; }
/** * Returns a selector returned by {@link SelectorProvider#provider}'s * {@link SelectorProvider#openSelector} method. * * @throws IOException * if an I/O error occurs. */ public static Selector open() throws IOException { return SelectorProvider.provider().openSelector(); }
/** * Creates an open and unbound server-socket channel. * <p> * This channel is created by calling {@code openServerSocketChannel} method * of the default {@code SelectorProvider} instance. * * @return the new channel which is open but unbound. * @throws IOException * if an I/O error occurs. */ public static ServerSocketChannel open() throws IOException { return SelectorProvider.provider().openServerSocketChannel(); }
/** * Creates an opened and not-connected datagram channel. * <p> * This channel is created by calling the <code>openDatagramChannel</code> * method of the default {@link SelectorProvider} instance. * * @return the new channel which is open but not connected. * @throws IOException * if some I/O error occurs. */ public static DatagramChannel open() throws IOException { return SelectorProvider.provider().openDatagramChannel(); }
/** * Creates an open and unconnected socket channel. * <p> * This channel is created by calling {@code openSocketChannel()} of the * default {@link SelectorProvider} instance. * * @return the new channel which is open but unconnected. * @throws IOException * if an I/O error occurs. */ public static SocketChannel open() throws IOException { return SelectorProvider.provider().openSocketChannel(); }
/** @param udpPort May be null. */ public void bind (InetSocketAddress tcpPort, InetSocketAddress udpPort) throws IOException { close(); synchronized (updateLock) { selector.wakeup(); try { serverChannel = selector.provider().openServerSocketChannel(); serverChannel.socket().bind(tcpPort); serverChannel.configureBlocking(false); serverChannel.register(selector, SelectionKey.OP_ACCEPT); if (DEBUG) debug("kryonet", "Accepting connections on port: " + tcpPort + "/TCP"); if (udpPort != null) { udp = new UdpConnection(serialization, objectBufferSize); udp.bind(selector, udpPort); if (DEBUG) debug("kryonet", "Accepting connections on port: " + udpPort + "/UDP"); } } catch (IOException ex) { close(); throw ex; } } if (INFO) info("kryonet", "Server opened."); }
/** * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}. */ public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) { this(nThreads, threadFactory, SelectorProvider.provider()); }
private SelectorTuple openSelector() { final Selector unwrappedSelector; try { unwrappedSelector = provider.openSelector(); } catch (IOException e) { throw new ChannelException("failed to open a new selector", e);
public Selector open() throws IOException { return provider.openSelector(); }
private static DatagramChannel newSocket(SelectorProvider provider) { try { /** * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in * {@link SelectorProvider#provider()} which is called by each DatagramChannel.open() otherwise. * * See <a href="https://github.com/netty/netty/issues/2308">#2308</a>. */ return provider.openDatagramChannel(); } catch (IOException e) { throw new ChannelException("Failed to open a socket.", e); } }
/** * Returns the inherited channel from the creator of the current virtual * machine. * * @return the inherited {@link Channel} or {@code null} if none exists. * @throws IOException * if an I/O error occurred. * @see SelectorProvider * @see SelectorProvider#inheritedChannel() */ public static Channel inheritedChannel() throws IOException { return SelectorProvider.provider().inheritedChannel(); }
private static SocketChannel newSocket(SelectorProvider provider) { try { /** * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in * {@link SelectorProvider#provider()} which is called by each SocketChannel.open() otherwise. * * See <a href="https://github.com/netty/netty/issues/2308">#2308</a>. */ return provider.openSocketChannel(); } catch (IOException e) { throw new ChannelException("Failed to open a socket.", e); } }
@Override public ServerSocketChannel openServerSocketChannel() throws IOException { return wrap(delegate.openServerSocketChannel()); }
private static ServerSocketChannel newSocket(SelectorProvider provider) { try { /** * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in * {@link SelectorProvider#provider()} which is called by each ServerSocketChannel.open() otherwise. * * See <a href="https://github.com/netty/netty/issues/2308">#2308</a>. */ return provider.openServerSocketChannel(); } catch (IOException e) { throw new ChannelException( "Failed to open a server socket.", e); } }
@Override public UDPNIOServerConnection bind(SocketAddress socketAddress, int backlog) throws IOException { return bindToChannel( udpTransport.getSelectorProvider().openDatagramChannel(), socketAddress); }
/** * Creates a new instance registered with the given channel group. * * @param group the channel group * @throws IOException if an I/O error occurs */ AsyncSocketChannelImpl(AsyncGroupImpl group) throws IOException { this(group, group.selectorProvider().openSocketChannel()); }
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; } }
@NotNull @Override public ServerSocketChannel channelFor(Selector selector) throws IOException { ServerSocketChannel myServerSocketChannel = selector.provider().openServerSocketChannel(); myServerSocketChannel.configureBlocking(false); int bindPort = -1; for (int port = firstTryPort; port <= lastTryPort; port++) { try { InetSocketAddress tryAddress = new InetSocketAddress(port); myServerSocketChannel.socket().bind(tryAddress); bindPort = tryAddress.getPort(); break; } catch (IOException e) { //try next port logger.debug("Could not bind to port {}, trying next port...", port); } } if (bindPort == -1) { logger.error(String.format( "No available ports in range [%d, %d] for the BitTorrent client!", firstTryPort, lastTryPort )); throw new IOException("No available port for the BitTorrent client!"); } return myServerSocketChannel; } }
public void connect (Selector selector, SocketAddress remoteAddress, int timeout) throws IOException { close(); writeBuffer.clear(); readBuffer.clear(); readBuffer.flip(); currentObjectLength = 0; try { SocketChannel socketChannel = selector.provider().openSocketChannel(); Socket socket = socketChannel.socket(); socket.setTcpNoDelay(true); // socket.setTrafficClass(IPTOS_LOWDELAY); socket.connect(remoteAddress, timeout); // Connect using blocking mode for simplicity. socketChannel.configureBlocking(false); this.socketChannel = socketChannel; selectionKey = socketChannel.register(selector, SelectionKey.OP_READ); selectionKey.attach(this); if (DEBUG) { debug("kryonet", "Port " + socketChannel.socket().getLocalPort() + "/TCP connected to: " + socketChannel.socket().getRemoteSocketAddress()); } lastReadTime = lastWriteTime = System.currentTimeMillis(); } catch (IOException ex) { close(); IOException ioEx = new IOException("Unable to connect to: " + remoteAddress); ioEx.initCause(ex); throw ioEx; } }