/** * Create a new broadcaster * * @param address - multicast group address * @param srcAddress - address of interface we should use to broadcast. * @param port - udp port to use * @param ttl - packet ttl * @throws IOException */ public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl) throws IOException, JdpException { this.addr = address; this.port = port; ProtocolFamily family = (address instanceof Inet6Address) ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; channel = DatagramChannel.open(family); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); // with srcAddress equal to null, this constructor do exactly the same as // if srcAddress is not passed if (srcAddress != null) { // User requests particular interface to bind to NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress); try { channel.bind(new InetSocketAddress(srcAddress, 0)); } catch (UnsupportedAddressTypeException ex) { throw new JdpException("Unable to bind to source address"); } channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); } }
public NIOAcceptor(String name, String bindIp,int port, FrontendConnectionFactory factory, NIOReactorPool reactorPool) throws IOException { super.setName(name); this.port = port; this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.configureBlocking(false); /** 设置TCP属性 */ serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2); // backlog=100 serverChannel.bind(new InetSocketAddress(bindIp, port), 100); // 注册OP_ACCEPT,监听客户端连接 // 准备好接受新的连接 // 监听到之后是图-MySql第2步,(接受TCP连接) this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); //FrontendConnectionFactory,用来封装channel成为FrontendConnection this.factory = factory; //NIOReactor池 this.reactorPool = reactorPool; }
/** * This method sets the receiving buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @param size Size of the receiving buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public void SetRcvBufferSize(int size) { if(connected) { try { socketchannel.setOption(StandardSocketOptions.SO_RCVBUF, size); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } }
/** * This method sets the sending buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @param size Size of the sending buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public void SetSndBufferSize(int size) { if(connected) { try { socketchannel.setOption(StandardSocketOptions.SO_SNDBUF, size); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } }
/** * This method sets the TCP_NO_DELAY of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @param b The value of the TCP_NO_DELAY parameter of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public void SetNoDelay(boolean b) { if(connected) { try { socketchannel.setOption(StandardSocketOptions.TCP_NODELAY, b); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } }
/** * This method returns the receiving buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @return The Size of the receiving buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public int GetRcvBufferSize() { if(connected) { try { return socketchannel.getOption(StandardSocketOptions.SO_RCVBUF); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } return -1; }
/** * This method returns the sending buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @return The Size of the sending buffer size of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public int GetSndBufferSize() { if(connected) { try { return socketchannel.getOption(StandardSocketOptions.SO_SNDBUF); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } return -1; }
/** * This method returns the TCP_NO_DELAY of the {@link SocketChannel} wrapped by this {@link RapidConnection} * @return The value of the TCP_NO_DELAY parameter of the {@link SocketChannel} wrapped by this {@link RapidConnection} */ public boolean GetNoDelay() { if(connected) { try { return socketchannel.getOption(StandardSocketOptions.TCP_NODELAY); } catch (IOException e) { if(infoANDdebug.getDEBUG()) { e.printStackTrace(); } } } return false; }
public void open() { SocketAddress address = new InetSocketAddress(port); try ( DatagramChannel channel = DatagramChannel.open() .setOption(StandardSocketOptions.SO_REUSEADDR, true) .bind(address) ) { channel.configureBlocking(true); logger.info("Listening: {}", port); while (isAlive) { long sequence = ringBuffer.next(); ByteBufferContainer container = ringBuffer.get(sequence); container.clear(); channel.receive(container.getBuffer()); container.flip(); ringBuffer.publish(sequence); } } catch (Exception e) { logger.error("Got exception...", e); } }
private static Map<RegistryKey,OptionKey> options() { Map<RegistryKey,OptionKey> map = new HashMap<RegistryKey,OptionKey>(); map.put(new RegistryKey(StandardSocketOptions.SO_BROADCAST, Net.UNSPEC), new OptionKey(1, 6)); map.put(new RegistryKey(StandardSocketOptions.SO_KEEPALIVE, Net.UNSPEC), new OptionKey(1, 9)); map.put(new RegistryKey(StandardSocketOptions.SO_LINGER, Net.UNSPEC), new OptionKey(1, 13)); map.put(new RegistryKey(StandardSocketOptions.SO_SNDBUF, Net.UNSPEC), new OptionKey(1, 7)); map.put(new RegistryKey(StandardSocketOptions.SO_RCVBUF, Net.UNSPEC), new OptionKey(1, 8)); map.put(new RegistryKey(StandardSocketOptions.SO_REUSEADDR, Net.UNSPEC), new OptionKey(1, 2)); map.put(new RegistryKey(StandardSocketOptions.TCP_NODELAY, Net.UNSPEC), new OptionKey(6, 1)); map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET), new OptionKey(0, 1)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(0, 32)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(0, 33)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(0, 34)); map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET6), new OptionKey(41, 67)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(41, 17)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(41, 18)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(41, 19)); map.put(new RegistryKey(ExtendedSocketOption.SO_OOBINLINE, Net.UNSPEC), new OptionKey(1, 10)); return map; }
@Override public final <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind isReuseAddress = (Boolean)value; } else { Net.setSocketOption(fd, Net.UNSPEC, name, value); } return this; } finally { end(); } }
@Override @SuppressWarnings("unchecked") public final <T> T getOption(SocketOption<T> name) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind return (T)Boolean.valueOf(isReuseAddress); } return (T) Net.getSocketOption(fd, Net.UNSPEC, name); } finally { end(); } }
@Override public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (writeShutdown) throw new IOException("Connection has been shutdown for writing"); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind isReuseAddress = (Boolean)value; } else { Net.setSocketOption(fd, Net.UNSPEC, name, value); } return this; } finally { end(); } }
private static Map<RegistryKey,OptionKey> options() { Map<RegistryKey,OptionKey> map = new HashMap<RegistryKey,OptionKey>(); map.put(new RegistryKey(StandardSocketOptions.SO_BROADCAST, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.Broadcast)); map.put(new RegistryKey(StandardSocketOptions.SO_KEEPALIVE, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.KeepAlive)); map.put(new RegistryKey(StandardSocketOptions.SO_LINGER, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.Linger)); map.put(new RegistryKey(StandardSocketOptions.SO_SNDBUF, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.SendBuffer)); map.put(new RegistryKey(StandardSocketOptions.SO_RCVBUF, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer)); map.put(new RegistryKey(StandardSocketOptions.SO_REUSEADDR, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress)); map.put(new RegistryKey(StandardSocketOptions.TCP_NODELAY, Net.UNSPEC), new OptionKey(SocketOptionLevel.Tcp, SocketOptionName.NoDelay)); map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.TypeOfService)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.MulticastInterface)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)); map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, ikvm.internal.Winsock.IPV6_TCLASS)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.IpTimeToLive)); map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback)); map.put(new RegistryKey(ExtendedSocketOption.SO_OOBINLINE, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.OutOfBandInline)); return map; }
public void setSocketParams(Connection con, boolean isFrontChannel) throws IOException { int sorcvbuf = 0; int sosndbuf = 0; int soNoDelay = 0; if (isFrontChannel) { sorcvbuf = netConfig.getFrontsocketsorcvbuf(); sosndbuf = netConfig.getFrontsocketsosndbuf(); soNoDelay = netConfig.getFrontSocketNoDelay(); } else { sorcvbuf = netConfig.getBacksocketsorcvbuf(); sosndbuf = netConfig.getBacksocketsosndbuf(); soNoDelay = netConfig.getBackSocketNoDelay(); } NetworkChannel channel = con.getChannel(); channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); }
public NIOAcceptor(String name, String bindIp, int port, ConnectionFactory factory, NIOReactorPool reactorPool) throws IOException { super.setName(name); this.port = port; this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.configureBlocking(false); /** 设置TCP属性 */ this.serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); this.serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 32); // 32K // backlog=200 this.serverChannel.bind(new InetSocketAddress(bindIp, port), 200); this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); this.factory = factory; this.reactorPool = reactorPool; }
public void setSocketParams(boolean isFrontChannel) throws IOException { SystemConfig system = DbleServer.getInstance().getConfig().getSystem(); int soRcvBuf; int soSndBuf; int soNoDelay; if (isFrontChannel) { soRcvBuf = system.getFrontSocketSoRcvbuf(); soSndBuf = system.getFrontSocketSoSndbuf(); soNoDelay = system.getFrontSocketNoDelay(); } else { soRcvBuf = system.getBackSocketSoRcvbuf(); soSndBuf = system.getBackSocketSoSndbuf(); soNoDelay = system.getBackSocketNoDelay(); } channel.setOption(StandardSocketOptions.SO_RCVBUF, soRcvBuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, soSndBuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); this.setMaxPacketSize(system.getMaxPacketSize()); this.setIdleTimeout(system.getIdleTimeout()); this.setCharacterSet(system.getCharset()); this.setReadBufferChunk(soRcvBuf); }
/** * Creates a new client and adds it to the list of connections. * Sets the clients handler to the initial state of NameReader * * @param channel the newly accepted channel */ private void handleNewConnection(AsynchronousSocketChannel channel) { try { channel.setOption(StandardSocketOptions.TCP_NODELAY, true); } catch (IOException e) { // ignore // e.printStackTrace(); } //new dealer and channel AioServerDataDealer dealer = null; dealer = aioDataDealerFactory.getAioServerDataDealer(); int channelId = getChannelId(); AioServerChannel aioChannel = new AioServerChannel(channelId, channel, dealer, this); connections.put(channelId, aioChannel); //start channel aioChannel.run(null); }
@Override public void listen(int thread, int port, AioServerListener listener) { this.port = port; this.listener = listener; try { channelGroup = AsynchronousChannelGroup.withFixedThreadPool(thread, Executors.defaultThreadFactory()); serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup); serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverSocketChannel.bind(new InetSocketAddress(port)); serverSocketChannel.accept(null, this); if (logger.isInfoEnable()) logger.info("启动AIO监听[{}]服务。", port); } catch (IOException e) { logger.warn(e, "启动AIO监听[{}]服务时发生异常!", port); } }
void start() { if(!writeState.compareAndSet(NOT_INITIALIZED, INITIALIZING)) { return; } try { timeoutFilter.reset(); channel = DatagramChannel.open(dh_table.getType().PROTO_FAMILY); channel.configureBlocking(false); channel.setOption(StandardSocketOptions.SO_RCVBUF, 2*1024*1024); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.bind(new InetSocketAddress(addr, port)); connectionManager = dh_table.getConnectionManager(); connectionManager.register(this); if(!writeState.compareAndSet(INITIALIZING, WRITE_STATE_IDLE)) { writeState.set(INITIALIZING); close(); } } catch (IOException e) { e.printStackTrace(); } }
@Override public void start(Collection<DHT> dhts, ConfigReader config) { try { channel = DatagramChannel.open(StandardProtocolFamily.INET); channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); // we only need to send, not to receive, so need to bind to a specific port channel.bind(new InetSocketAddress(0)); channel.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] {(byte) 224,0,23,5}), 9696)); } catch (IOException e) { e.printStackTrace(); return; } t.setDaemon(true); t.setName("opentracker-sync"); t.start(); // OT-sync only supports ipv4 atm dhts.stream().filter(d -> d.getType().PREFERRED_ADDRESS_TYPE == Inet4Address.class).forEach(d -> { d.addIncomingMessageListener(this::incomingPacket); }); }
/** * Initialize the SocketStream with a new Socket. * * @param s the new socket. */ public void init(SocketChannel s) { _s = s; try { s.setOption(StandardSocketOptions.TCP_NODELAY, true); } catch (Exception e) { e.printStackTrace();; } //_is = null; //_os = null; _needsFlush = false; _readBuffer.clear().flip(); _writeBuffer.clear(); }
void setupSocketChannel(SocketChannel socketChannel) throws IOException { socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, keepAlive); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, tcpNoDelay); if (rcvBufferSize > 0) { socketChannel.setOption(StandardSocketOptions.SO_RCVBUF, rcvBufferSize); } if (sndBufferSize > 0) { socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, sndBufferSize); } if (lingerMs >= 0) { socketChannel.setOption(StandardSocketOptions.SO_LINGER, lingerMs); } }
public TCPNIOAcceptor(String bindIp, int port, NIOReactorPool reactorPool, int backlog, AcceptModel aModel) throws IOException { super.setName("nioacceptor"); this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.configureBlocking(false); /** 设置TCP属性 */ serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2); serverChannel.bind(new InetSocketAddress(bindIp, port), backlog); this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); this.reactorPool = reactorPool; if(AcceptModel.MEMCACHE == aModel){ Settings.binding_protocol=Protocol.negotiating; }else if(AcceptModel.REDIS == aModel){ Settings.binding_protocol=Protocol.resp; } }
public void setSocketParams(Connection con, boolean isFrontChannel) throws IOException { int sorcvbuf = 0; int sosndbuf = 0; int soNoDelay = 0; if (isFrontChannel) { sorcvbuf = netConfig.getFrontsocketsorcvbuf(); sosndbuf = netConfig.getFrontsocketsosndbuf(); soNoDelay = netConfig.getFrontSocketNoDelay(); } else { sorcvbuf = netConfig.getBacksocketsorcvbuf(); sosndbuf = netConfig.getBacksocketsosndbuf(); soNoDelay = netConfig.getBackSocketNoDelay(); } NetworkChannel channel = con.getChannel(); channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf); channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf); channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); con.setMaxPacketSize(netConfig.getMaxPacketSize()); con.setPacketHeaderSize(netConfig.getPacketHeaderSize()); }
public NIOAcceptor(String name, String bindIp, int port, ConnectionFactory factory, NIOReactorPool reactorPool) throws IOException { super.setName(name); this.port = port; this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.configureBlocking(false); /** 设置TCP属性 */ serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2); // backlog=100 serverChannel.bind(new InetSocketAddress(bindIp, port), 100); this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); this.factory = factory; this.reactorPool = reactorPool; }