java.lang.Object
java.lang.Thread
net.i2p.util.I2PThread
net.i2p.util.I2PAppThread
net.i2p.i2ptunnel.I2PTunnelOutproxyRunner
- All Implemented Interfaces:
Runnable
I2PTunnelOutproxyRunner forwards data bidirectionally between two connected sockets: one representing an external socket connection and the other an I2P socket connection. It continuously transfers data until either side closes the connection or an error occurs.
The class manages two internal forwarding threads to handle each direction of the data flow, maintains counters of bytes sent and received, and allows optional initial data to be sent on either socket upon startup.
A user-provided callback can be invoked if no data (beyond initial data) is received before the connection closes, enabling handling of timeouts or failures.
This class is designed for internal use within the I2P tunnel infrastructure and is not maintained as a stable public API.
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class I2PThread
I2PThread.OOMEventListenerModifier and TypeClassDescriptionstatic interfaceListener for out-of-memory events.Nested classes/interfaces inherited from class Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandlerField Summary
FieldsModifier and TypeFieldDescriptionprivate static final AtomicLongSequence number of the created runners.protected final LogClass log, also used as the prefix for each runner's per-connection log.private final longThis runner's position in that sequence.private final CountDownLatchLatch signalling both directions have finished.private final SocketThe I2P side socket to the client.private final byte[]Data already read from the I2P side.private final byte[]Data already read from the clear side.private static final intMax bytes streamed in a packet - smaller packets might be filled up to this size.private static final intSocket buffer size, in bytes.private final I2PTunnelRunner.FailCallbackThread that forwards data between local socket and I2PSocket.private final SocketThe clear side socket to the outproxy.private final ObjectLock for closing the sockets.private final longWhen the runner started up.private final AtomicLongBytes received from the outproxy.private final AtomicLongBytes sent to the outproxy, counted across the copy threads.Fields inherited from class Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITYConstructor Summary
ConstructorsConstructorDescriptionI2PTunnelOutproxyRunner(Socket s, Socket i2ps, Object slock, byte[] initialI2PData, byte[] initialSocketData, I2PTunnelRunner.FailCallback onTimeout) Create a runner that forwards between one external socket and one tunnel.Method Summary
Public MethodsModifier and TypeMethodDescriptionvoidCalled to signal an error occurred and notify waiting threads.protected InputStreamGet the input stream from the external socket.protected OutputStreamGet the output stream from the external socket.longGet when this runner started transferring data.voidrun()Start forwarding data bidirectionally between the external socket and the I2P socket.Methods inherited from class I2PAppThread
addOOMEventThreadListener, fireOOM, removeOOMEventThreadListenerModifier and TypeMethodDescriptionvoidRegister a new component that wants notification of OOM events.protected voidfireOOM(OutOfMemoryError oom) Notify listeners that an OutOfMemoryError occurred.voidUnregister a component that wants notification of OOM events.Methods inherited from class I2PThread
addOOMEventListener, removeOOMEventListener, startModifier and TypeMethodDescriptionstatic voidRegister a new component that wants notification of OOM events.static voidUnregister a component that wants notification of OOM events.voidstart()Overridden to provide useful info to users on OOM, and to prevent shutting down the whole JVM for what is most likely not a heap issue.Methods inherited from class Thread
activeCount, clone, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, startVirtualThread, threadId, toString, yield
Field Details
_log
Class log, also used as the prefix for each runner's per-connection log.__runnerId
Sequence number of the created runners._runnerId
private final long _runnerIdThis runner's position in that sequence.MAX_PACKET_SIZE
private static final int MAX_PACKET_SIZEMax bytes streamed in a packet - smaller packets might be filled up to this size.- See Also:
NETWORK_BUFFER_SIZE
private static final int NETWORK_BUFFER_SIZESocket buffer size, in bytes.- See Also:
s
The clear side socket to the outproxy.i2ps
The I2P side socket to the client.slock
Lock for closing the sockets.finishLatch
Latch signalling both directions have finished.initialI2PData
private final byte[] initialI2PDataData already read from the I2P side.initialSocketData
private final byte[] initialSocketDataData already read from the clear side.startedOn
private final long startedOnWhen the runner started up.onTimeout
Thread that forwards data between local socket and I2PSocket.Stripped-down version of I2PTunnelRunner without SSL or read timeout support. Intended for outproxy use only. Not maintained as stable public API.
totalSent
Bytes sent to the outproxy, counted across the copy threads.totalReceived
Bytes received from the outproxy.
Constructor Details
I2PTunnelOutproxyRunner
public I2PTunnelOutproxyRunner(Socket s, Socket i2ps, Object slock, byte[] initialI2PData, byte[] initialSocketData, I2PTunnelRunner.FailCallback onTimeout) Create a runner that forwards between one external socket and one tunnel.Does not start itself. Caller must call start().
- Parameters:
s- external socket connectioni2ps- I2P socket connectionslock- socket lock object, non-nullinitialI2PData- initial data to send to I2P side, nullableinitialSocketData- initial data to send to socket side, nullableonTimeout- fail callback if no data received (except initial), nullable
Method Details
getStartedOn
public long getStartedOn()Get when this runner started transferring data.- Returns:
- timestamp in ms
getSocketIn
Get the input stream from the external socket.- Returns:
- the socket's input stream
- Throws:
IOException- if an I/O error occurs
getSocketOut
Get the output stream from the external socket.- Returns:
- the socket's output stream
- Throws:
IOException- if an I/O error occurs
run
public void run()Start forwarding data bidirectionally between the external socket and the I2P socket. Sends any initial data first, then starts two forwarding threads and waits for both to complete. Invokes the timeout callback if no data was received.errorOccurred
public void errorOccurred()Called to signal an error occurred and notify waiting threads.