java.lang.Object
net.i2p.client.streaming.impl.I2PSocketManagerFull
All Implemented Interfaces:
I2PSocketManager

public class I2PSocketManagerFullextends Object implements I2PSocketManager
Centralize the coordination and multiplexing of the local client's streaming. There should be one I2PSocketManager for each I2PSession, and if an application is sending and receiving data through the streaming library using an I2PSocketManager, it should not attempt to call I2PSession's setSessionListener or receive any messages with its .receiveMessage

This is what I2PSocketManagerFactory.createManager() returns. Direct instantiation by others is deprecated.

  • Field Details

    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private final Log _log
      Logger for the socket manager.
    • _session

      private final I2PSession _session
      The session serving this manager.
    • _subsessions

      private final Set<I2PSession> _subsessions
      Extra sessions opened for bandwidth.
    • _subsessionsOptions

      private final Map<I2PSession, ConnectionOptions> _subsessionsOptions
      Options each subsession was opened with.
    • _serverSocket

      private final I2PServerSocketFull _serverSocket
      The server socket wrapper for this manager.
    • _realServerSocket

      private StandardServerSocket _realServerSocket
      The real server socket, null until bound.
    • _defaultOptions

      private final ConnectionOptions _defaultOptions
      Default options for new connections.
    • _acceptTimeout

      private long _acceptTimeout
      Time accept() waits, in ms.
    • _name

      private String _name
      Name of this manager, for the logs.
    • _managerId

      private static final AtomicInteger _managerId
      Counter numbering the managers.
    • _connectionManager

      private final ConnectionManager _connectionManager
      The manager of the streaming connections.
    • _isDestroyed

      private final AtomicBoolean _isDestroyed
      True once the manager is destroyed.
    • _ecUnsupported

      private static final Set<Hash> _ecUnsupported
      Does not support EC.
    • EC_UNSUPPORTED_HASHES

      private static final String[] EC_UNSUPPORTED_HASHES
      Signature types an EC destination cannot use.
    • _edUnsupported

      private static final Set<Hash> _edUnsupported
      Does not support Ed.
    • ED_UNSUPPORTED_HASHES

      private static final String[] ED_UNSUPPORTED_HASHES
      Signature types an EdDSA destination cannot use.
    • userDsaList

      private static volatile String userDsaList
      Cached property value to detect changes.
    • _userDsaOnly

      private static final Set<Hash> _userDsaOnly
      Destinations pinned to DSA by configuration.
    • PROP_DSALIST

      private static final String PROP_DSALIST
      Config key listing the DSA-only destinations.
      See Also:
    • ACCEPT_TIMEOUT_DEFAULT

      private static final long ACCEPT_TIMEOUT_DEFAULT
      How long to wait for the client app to accept() before sending back CLOSE? This includes the time waiting in the queue. Set to 10 seconds.
      See Also:
    • _pcapInitLock

      private static final Object _pcapInitLock
      Lock for the pcap initialization.
    • pcapInitialized

      private static boolean pcapInitialized
      True once the pcap writer is set up.
    • pcapWriter

      static PcapWriter pcapWriter
      The PCAP writer.
    • PROP_PCAP

      static final String PROP_PCAP
      The PCAP property.
      See Also:
    • PCAP_FILE

      private static final String PCAP_FILE
      File name of the streaming pcap capture.
      See Also:
    • _maxSYNQueueSize

      private static volatile int _maxSYNQueueSize
      Largest SYN queue allowed, Tuner override.
    • _rttSynTimeoutScale

      private static volatile int _rttSynTimeoutScale
      SYN timeout scale, Tuner override.
    • _synExprExpireThresh

      private static volatile int _synExprExpireThresh
      SYN expiry threshold, Tuner override.
  • Constructor Details

  • Method Details

    • buildOptions

      public I2PSocketOptions buildOptions()
      Create a copy of the current options, to be used in a setDefaultOptions(I2PSocketOptions) call.
      Specified by:
      buildOptions in interface I2PSocketManager
      Returns:
      a copy of the current default options
    • buildOptions

      public I2PSocketOptions buildOptions(Properties opts)
      Create a modified copy of the current options, to be used in a setDefaultOptions() call.

      Defaults in opts are honored: a property absent from opts keeps its current value rather than reverting to the default.

      Specified by:
      buildOptions in interface I2PSocketManager
      Parameters:
      opts - The new options, may be null
      Returns:
      a new options object with the specified modifications
    • getSession

      public I2PSession getSession()
      The I2P session for this socket manager.
      Specified by:
      getSession in interface I2PSocketManager
      Returns:
      the session, non-null
    • addSubsession

      public I2PSession addSubsession(InputStream privateKeyStream, Properties opts) throws I2PSessionException
      For a server, you must call connect(Destination, I2PSocketOptions) on the returned object. Connecting the primary session does NOT connect any subsessions. If the primary session is not connected, connecting a subsession will connect the primary session first.
      Specified by:
      addSubsession in interface I2PSocketManager
      Parameters:
      privateKeyStream - null for transient, if non-null must have same encryption keys as primary session and different signing keys
      opts - subsession options if any, may be null
      Returns:
      a new subsession, non-null
      Throws:
      I2PSessionException - if the router does not support sub-sessions, if a sub-session request is already pending, or if the new session duplicates an existing one by private key
    • getSigType

      private SigType getSigType(Properties opts) throws I2PSessionException
      Sig type from the options, or the default if unset or unsupported.
      Parameters:
      opts - may be null
      Returns:
      the sig type
      Throws:
      I2PSessionException - if the options name a sig type that is unavailable on this router
    • removeSubsession

      public void removeSubsession(I2PSession session)
      Remove the subsession.
      Specified by:
      removeSubsession in interface I2PSocketManager
      Parameters:
      session - the subsession to remove
    • getSubsessions

      public List<I2PSession> getSubsessions()
      Subsession list, not including the primary session.
      Specified by:
      getSubsessions in interface I2PSocketManager
      Returns:
      a list of subsessions, non-null, does not include the primary session
    • getConnectionManager

      public ConnectionManager getConnectionManager()
      Connection manager for this socket manager.
      Returns:
      the connection manager
    • receiveSocket

      The accept() call.

      This only listens on the primary session. There is no way to get incoming connections on a subsession.

      Returns:
      connected I2PSocket, never null
      Throws:
      I2PException - if session is closed
      RouterRestartException - (extends I2PException) if the router is apparently restarting
      ConnectException - if the I2PServerSocket is closed, or if interrupted
      SocketTimeoutException - if a timeout was previously set with setSoTimeout and the timeout has been reached.
    • ping

      public boolean ping(Destination peer, long timeoutMs)
      Ping the specified peer, returning true if they replied to the ping within the timeout specified, false otherwise. This call blocks.

      Uses the ports from the default options.

      Specified by:
      ping in interface I2PSocketManager
      Parameters:
      peer - Destination to ping
      timeoutMs - timeout in ms, greater than zero
      Returns:
      true on success, false on failure
      Throws:
      IllegalArgumentException - if timeoutMs is not greater than zero
    • ping

      public boolean ping(Destination peer, int localPort, int remotePort, long timeoutMs)
      Ping the specified peer, returning true if they replied to the ping within the timeout specified, false otherwise. This call blocks.

      Uses the ports specified.

      Specified by:
      ping in interface I2PSocketManager
      Parameters:
      peer - Destination to ping
      localPort - 0 - 65535
      remotePort - 0 - 65535
      timeoutMs - timeout in ms, greater than zero
      Returns:
      success or failure
      Throws:
      IllegalArgumentException - if ports or timeout are invalid
    • ping

      public boolean ping(Destination peer, I2PSession session, int localPort, int remotePort, long timeoutMs)
      Ping the specified peer through a specific subsession, returning true if they replied to the ping within the timeout specified, false otherwise. This call blocks.
      Parameters:
      peer - Destination to ping
      session - the subsession to use
      localPort - 0 - 65535
      remotePort - 0 - 65535
      timeoutMs - timeout in ms, greater than zero
      Returns:
      success or failure
      Throws:
      IllegalArgumentException - if ports or timeout are invalid
    • ping

      public byte[] ping(Destination peer, int localPort, int remotePort, long timeoutMs, byte[] payload)
      Ping the specified peer, returning true if they replied to the ping within the timeout specified, false otherwise. This call blocks.

      Uses the ports specified.

      Specified by:
      ping in interface I2PSocketManager
      Parameters:
      peer - Destination to ping
      localPort - 0 - 65535
      remotePort - 0 - 65535
      timeoutMs - timeout in ms, greater than zero
      payload - to include in the ping
      Returns:
      the payload received in the pong, zero-length if none, null on failure or timeout
      Throws:
      IllegalArgumentException - if ports or timeout are invalid
    • ping

      public byte[] ping(Destination peer, I2PSession session, int localPort, int remotePort, long timeoutMs, byte[] payload)
      Ping the specified peer through a specific subsession, returning true if they replied to the ping within the timeout specified, false otherwise. This call blocks.
      Parameters:
      peer - Destination to ping
      session - the subsession to use
      localPort - 0 - 65535
      remotePort - 0 - 65535
      timeoutMs - timeout in ms, greater than zero
      payload - to include in the ping
      Returns:
      the payload received in the pong, zero-length if none, null on failure or timeout
      Throws:
      IllegalArgumentException - if ports or timeout are invalid
    • setAcceptTimeout

      public void setAcceptTimeout(long ms)
      Set the accept timeout in milliseconds.
      Specified by:
      setAcceptTimeout in interface I2PSocketManager
      Parameters:
      ms - the accept timeout in milliseconds
    • getAcceptTimeout

      public long getAcceptTimeout()
      Time accept() waits for an incoming connection.
      Specified by:
      getAcceptTimeout in interface I2PSocketManager
      Returns:
      the timeout, in ms
    • setDefaultOptions

      public void setDefaultOptions(I2PSocketOptions options)
      Update the options on a running socket manager. Parameters in the I2PSocketOptions interface may be changed directly with the setters; no need to use this method for those. This does NOT update the underlying I2CP or tunnel options; use getSession().updateOptions() for that.
      Specified by:
      setDefaultOptions in interface I2PSocketManager
      Parameters:
      options - as created from a call to buildOptions(properties), non-null
    • setDefaultOptions

      public void setDefaultOptions(I2PSession session, I2PSocketOptions options)
      Update the options on a running subsession. Parameters in the I2PSocketOptions interface may be changed directly with the setters; no need to use this method for those. This does NOT update the underlying I2CP or tunnel options.
      Parameters:
      session - the subsession to update
      options - as created from a call to buildOptions(properties), non-null
    • getDefaultOptions

      public I2PSocketOptions getDefaultOptions()
      Returns the current default options. This does NOT update the underlying I2CP or tunnel options; use getSession().updateOptions() for that.
      Specified by:
      getDefaultOptions in interface I2PSocketManager
      Returns:
      the default options
    • getServerSocket

      public I2PServerSocket getServerSocket()
      Returns non-null socket. This method does not throw exceptions, but methods on the returned socket may throw exceptions if the socket or socket manager is closed.

      This only listens on the primary session. There is no way to get incoming connections on a subsession.

      Specified by:
      getServerSocket in interface I2PSocketManager
      Returns:
      non-null
    • getStandardServerSocket

      public ServerSocket getStandardServerSocket() throws IOException
      Like getServerSocket but returns a real ServerSocket for easier porting of apps.

      This only listens on the primary session. There is no way to get incoming connections on a subsession.

      Specified by:
      getStandardServerSocket in interface I2PSocketManager
      Returns:
      the standard server socket
      Throws:
      IOException - on I/O error
    • verifySession

      private void verifySession() throws I2PException
      Verify the session is connected, reconnecting if needed.
      Throws:
      I2PException - if session is closed; this is an I2PSessionException which extends I2PException
    • verifySession

      private void verifySession(I2PSession session) throws I2PException
      Throw unless the session is alive and ready.
      Parameters:
      session - the session to check, reconnected if it is closed
      Throws:
      I2PException - if this manager is destroyed or the closed session cannot be reconnected
    • connect

      Create a new connected socket. Blocks until the socket is created, unless the connectDelay option (i2p.streaming.connectDelay) is set and greater than zero. If so this will return immediately, and the client may quickly write initial data to the socket and this data will be bundled in the SYN packet.
      Specified by:
      connect in interface I2PSocketManager
      Parameters:
      peer - Destination to connect to
      options - I2P socket options to be used for connecting, may be null
      Returns:
      I2PSocket if successful
      Throws:
      NoRouteToHostException - if the peer is not found or not reachable
      I2PException - if there is some other I2P-related problem
    • updateUserDsaList

      private void updateUserDsaList()
      Update the global user DSA-only list. This does not affect the hardcoded Ex_UNSUPPORTED_HASHES lists above, the user can only add, not remove.
    • connect

      Create a new connected socket. Blocks until the socket is created, unless the connectDelay option (i2p.streaming.connectDelay) is set and greater than zero in the default options. If so this will return immediately, and the client may quickly write initial data to the socket and this data will be bundled in the SYN packet.
      Specified by:
      connect in interface I2PSocketManager
      Parameters:
      peer - Destination to connect to
      Returns:
      I2PSocket if successful
      Throws:
      NoRouteToHostException - if the peer is not found or not reachable
      I2PException - if there is some other I2P-related problem
    • connectToSocket

      public Socket connectToSocket(Destination peer) throws IOException
      Like connect() but returns a real Socket, and throws only IOE, for easier porting of apps.
      Specified by:
      connectToSocket in interface I2PSocketManager
      Parameters:
      peer - destination to connect to
      Returns:
      a connected Socket
      Throws:
      IOException - on I/O error
    • connectToSocket

      public Socket connectToSocket(Destination peer, int timeout) throws IOException
      Like connect() but returns a real Socket, and throws only IOE, for easier porting of apps.
      Specified by:
      connectToSocket in interface I2PSocketManager
      Parameters:
      peer - destination to connect to
      timeout - ms if > 0, forces blocking (disables connectDelay)
      Returns:
      a connected Socket
      Throws:
      IOException - on I/O error
    • connectToSocket

      private Socket connectToSocket(Destination peer, I2PSocketOptions options) throws IOException
      Like connect() but returns a real Socket, and throws only IOE, for easier porting of apps.
      Parameters:
      peer - the destination to connect to
      options - may be null
      Returns:
      a socket wrapping the established I2P socket
      Throws:
      IOException - if the connect fails, with the I2P failure as its cause
    • destroySocketManager

      public void destroySocketManager()
      Destroy the socket manager, freeing all the associated resources. This method will block until all the managed sockets are closed.

      CANNOT be restarted.

      Specified by:
      destroySocketManager in interface I2PSocketManager
    • isDestroyed

      public boolean isDestroyed()
      Has the socket manager been destroyed?
      Specified by:
      isDestroyed in interface I2PSocketManager
      Returns:
      true once destroySocketManager() has completed; the flag is set at the start of that method, so it also guards against a second destroy
    • listSockets

      public Set<I2PSocket> listSockets()
      Retrieve a set of currently connected I2PSockets, either initiated locally or remotely.
      Specified by:
      listSockets in interface I2PSocketManager
      Returns:
      set of currently connected I2PSockets
    • getName

      public String getName()
      For logging / diagnostics only.
      Specified by:
      getName in interface I2PSocketManager
      Returns:
      the name this manager was constructed with, suffixed with a sequence number that distinguishes concurrent managers
    • setName

      public void setName(String name)
      For logging / diagnostics only.
      Specified by:
      setName in interface I2PSocketManager
      Parameters:
      name - the label reported in logs and console output
    • addDisconnectListener

      public void addDisconnectListener(I2PSocketManager.DisconnectListener lsnr)
      Add a disconnect listener.
      Specified by:
      addDisconnectListener in interface I2PSocketManager
      Parameters:
      lsnr - the disconnect listener to add
    • removeDisconnectListener

      public void removeDisconnectListener(I2PSocketManager.DisconnectListener lsnr)
      Remove a disconnect listener.
      Specified by:
      removeDisconnectListener in interface I2PSocketManager
      Parameters:
      lsnr - the disconnect listener to remove
    • debugInit

      private static void debugInit(I2PAppContext ctx)
      Initialize PCAP logging if the property is set.
      Parameters:
      ctx - the I2P application context
    • getInitialWindowSize

      public static int getInitialWindowSize()
      Initial window size, Tuner delegation to ConnectionOptions.
      Returns:
      the initial window size
    • setInitialWindowSize

      public static void setInitialWindowSize(int val)
      Initial window size.
      Parameters:
      val - the new initial window size
    • getInitialRTO

      public static int getInitialRTO()
      Initial retransmission timeout used for new connections.
      Returns:
      the timeout, in ms
    • setInitialRTO

      public static void setInitialRTO(int val)
      Initial retransmission timeout.
      Parameters:
      val - the new initial retransmission timeout
    • getDefaultInitialAckDelay

      public static int getDefaultInitialAckDelay()
      Default delay before the first ACK is sent.
      Returns:
      the delay, in ms
    • setDefaultInitialAckDelay

      public static void setDefaultInitialAckDelay(int val)
      Default initial ACK delay.
      Parameters:
      val - the new default initial ACK delay
    • getDefaultPassiveFlushDelay

      public static int getDefaultPassiveFlushDelay()
      Default delay before a passive stream is flushed.
      Returns:
      the delay, in ms
    • setDefaultPassiveFlushDelay

      public static void setDefaultPassiveFlushDelay(int val)
      Default passive flush delay.
      Parameters:
      val - the new default passive flush delay
    • getDefaultInactivityTimeout

      public static int getDefaultInactivityTimeout()
      Default time an idle connection is kept before closing.
      Returns:
      the timeout, in ms
    • setDefaultInactivityTimeout

      public static void setDefaultInactivityTimeout(int val)
      Default inactivity timeout.
      Parameters:
      val - the new default inactivity timeout
    • getMaxInboundBuffer

      public static int getMaxInboundBuffer()
      Maximum inbound buffer cap.
      Returns:
      the maximum inbound buffer cap (bytes)
    • setMaxInboundBuffer

      public static void setMaxInboundBuffer(int val)
      Maximum inbound buffer cap.
      Parameters:
      val - the new maximum inbound buffer cap (bytes)
    • getMaxStreamsOverride

      public static int getMaxStreamsOverride()
      The Tuner's reactive cap on concurrent streams; 0 = not armed (each manager uses its own configured maxConcurrentStreams).
      Returns:
      the current override, or 0 if disabled
    • setMaxStreamsOverride

      public static void setMaxStreamsOverride(int val)
      Arm or clear the Tuner's reactive stream-cap override. Clamped to [0, maxMaxConcurrentStreams]; a manager enforces min(config, override).
      Parameters:
      val - the new override cap, or 0 to disable
    • getReceiveWorkerThreads

      public static int getReceiveWorkerThreads()
      The Tuner-managed default for inbound packet receive workers; 0 = no override (each manager uses its configured value or the core count).
      Returns:
      the current default worker count, or 0 if disabled
    • setReceiveWorkerThreads

      public static void setReceiveWorkerThreads(int val)
      Set the Tuner-managed default for inbound packet receive workers. Clamped to [0, 8]; 0 disables the override.
      Parameters:
      val - the new default worker count, or 0 to disable
    • resizeReceiveWorkers

      public static void resizeReceiveWorkers(int val)
      Apply a new receive-worker default and resize every live dispatcher to match, so existing managers (not just ones created afterwards) follow the Tuner. The resize drains each dispatcher's shard queues before rebuilding, so per-connection ordering and the never-drop guarantee are preserved.
      Parameters:
      val - the new worker count, clamped to [1, 8]; 0 only updates the default for future managers
    • getMaxMaxConcurrentStreams

      public static int getMaxMaxConcurrentStreams()
      The operator-tunable ceiling for the Tuner's stream-cap override. Reads from i2p.streaming.maxMaxConcurrentStreams in router.config.
      Returns:
      the current ceiling
    • setMaxMaxConcurrentStreams

      public static void setMaxMaxConcurrentStreams(int val)
      Set the operator-tunable ceiling for the Tuner's stream-cap override.
      Parameters:
      val - the new ceiling, clamped to [64, 8192]
    • getMaxSYNQueueSize

      public static int getMaxSYNQueueSize()
      Max SYN queue size, dynamically adjustable by the Tuner.
      Returns:
      the current max SYN queue size
    • setMaxSYNQueueSize

      public static void setMaxSYNQueueSize(int val)
      Max SYN queue size, dynamically adjustable by the Tuner.
      Parameters:
      val - the new max SYN queue size
    • getRttSynTimeoutScale

      public static int getRttSynTimeoutScale()
      Scale factor applied to a sampled recent round-trip time when computing the adaptive SYN accept-queue timeout under tunnel stress. A slow-but-alive fabric (high RTT) must keep a longer window than a fast one so that SYNs aren't expired before the handshake completes. Dynamically adjustable by the Tuner.

      0 (the unset default) selects ConnectionHandler.SYN_RTT_SCALE_DEFAULT (4), so the RTT-aware floor stays active out of the box.

      Returns:
      the RTT scale factor, or 0 for the built-in default
    • setRttSynTimeoutScale

      public static void setRttSynTimeoutScale(int val)
      Multiplier applied to recent RTT when sizing the adaptive SYN timeout.
      Parameters:
      val - the new scale factor; 0 or a negative value selects the built-in default (4)
    • getSynExprExpireThresh

      public static int getSynExprExpireThresh()
      Fraction (percent) of recent SYN-queue entries that must expire un-accepted before the adaptive timeout clamp is armed, in addition to low tunnel build success. Requiring positive stall evidence keeps a slow-but-alive server from being punished by its own latency. Dynamically adjustable by the Tuner.
      Returns:
      the expire-rate threshold as a percent [0,100], 100 disables the clamp
    • setSynExprExpireThresh

      public static void setSynExprExpireThresh(int val)
      Fraction of recent SYNs that must expire un-accepted to arm the clamp.
      Parameters:
      val - the new threshold percent; 100 disables the clamp entirely
    • getAcceptWorkerThreads

      public static int getAcceptWorkerThreads()
      The Tuner-managed accept-worker override; 0 = no override, so each manager's configured i2p.streaming.acceptWorkerThreads applies.
      Returns:
      the current override worker count, or 0 if disabled
    • setAcceptWorkerThreads

      public static void setAcceptWorkerThreads(int val)
      Set the Tuner-managed accept-worker override. Clamped to [0, 16]; 0 removes the override.
      Parameters:
      val - the new override worker count, or 0 to disable