public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErrorListener, LimitOutputStream.DoneCallback
I2PTunnelRunner implements the core bidirectional data forwarding between I2P and TCP network connections. It spawns two StreamForwarder threads: one for each direction of communication (I2P to TCP and TCP to I2P).
Connection Flow:
Keep-Alive Support: When keep-alive is enabled for either connection, the runner may skip spawning one direction of forwarding if no data is expected. This optimization is used for simple GET requests that don't require responses.
Thread Safety: This class uses locks (slock) to coordinate socket access and prevent concurrent writes from multiple threads. The finishLock ensures thread-safe state transitions.
toI2P,
fromI2P,
I2PTunnelServer| Modifier and Type | Class and Description |
|---|---|
static interface |
I2PTunnelRunner.FailCallback
For use in new constructor
|
static interface |
I2PTunnelRunner.SuccessCallback
Callback interface for successful tunnel operation completion.
|
I2PThread.OOMEventListenerThread.State, Thread.UncaughtExceptionHandler| Modifier and Type | Field and Description |
|---|---|
protected boolean |
_keepAliveI2P |
protected boolean |
_keepAliveSocket |
protected Log |
_log |
(package private) static int |
MAX_PACKET_SIZE
Max bytes streamed in a packet - smaller ones might be filled up to this size.
|
(package private) static int |
NETWORK_BUFFER_SIZE |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY| Constructor and Description |
|---|
I2PTunnelRunner(Socket s,
I2PSocket i2ps,
Object slock,
byte[] initialI2PData,
byte[] initialSocketData,
List<I2PSocket> sockList,
I2PTunnelRunner.FailCallback onFail)
Recommended new constructor.
|
I2PTunnelRunner(Socket s,
I2PSocket i2ps,
Object slock,
byte[] initialI2PData,
byte[] initialSocketData,
List<I2PSocket> sockList,
I2PTunnelRunner.FailCallback onFail,
boolean keepAliveI2P,
boolean keepAliveSocket)
With keepAlive args.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
close(OutputStream out,
InputStream in,
OutputStream i2pout,
InputStream i2pin,
Socket s,
I2PSocket i2ps,
Thread t1,
Thread t2)
Warning - overridden in I2PTunnelHTTPClientRunner.
|
(package private) boolean |
getKeepAliveI2P()
Checks if the I2P socket should be kept open after data transfer.
|
(package private) boolean |
getKeepAliveSocket()
Checks if the local socket should be kept open after data transfer.
|
protected InputStream |
getSocketIn()
Gets the TCP socket input stream.
|
protected OutputStream |
getSocketOut()
Gets the TCP socket output stream.
|
long |
getStartedOn()
Returns the timestamp when this runner started.
|
void |
run() |
void |
setSuccessCallback(I2PTunnelRunner.SuccessCallback sc)
Sets a callback to be invoked on successful data transfer.
|
void |
streamDone()
The DoneCallback for the I2P socket.
|
addOOMEventThreadListener, fireOOM, removeOOMEventThreadListeneraddOOMEventListener, removeOOMEventListener, startactiveCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yieldequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waiterrorOccurredprotected volatile boolean _keepAliveI2P
protected volatile boolean _keepAliveSocket
protected final Log _log
static int MAX_PACKET_SIZE
static final int NETWORK_BUFFER_SIZE
public I2PTunnelRunner(Socket s, I2PSocket i2ps, Object slock, byte[] initialI2PData, byte[] initialSocketData, List<I2PSocket> sockList, I2PTunnelRunner.FailCallback onFail)
slock - the socket lock, non-nullinitialI2PData - may be nullinitialSocketData - may be nullsockList - may be null. Caller must add i2ps to the list! It will be removed here on completion.
Will synchronize on slock when removing.onFail - May be null. If non-null and no data (except initial data) was received,
it will be run before closing s.public I2PTunnelRunner(Socket s, I2PSocket i2ps, Object slock, byte[] initialI2PData, byte[] initialSocketData, List<I2PSocket> sockList, I2PTunnelRunner.FailCallback onFail, boolean keepAliveI2P, boolean keepAliveSocket)
slock - the socket lock, non-nullinitialI2PData - may be nullinitialSocketData - may be nullsockList - may be null. Caller must add i2ps to the list! It will be removed here on completion.
Will synchronize on slock when removing.onFail - May be null. If non-null and no data (except initial data) was received,
it will be run before closing s.keepAliveI2P - Do not close the I2P socket when done.keepAliveSocket - Do not close the local socket when done.
For client side only; must be false for server side.
NO data will be forwarded from the socket to the i2psocket other than
initialI2PData if this is true.protected void close(OutputStream out, InputStream in, OutputStream i2pout, InputStream i2pin, Socket s, I2PSocket i2ps, Thread t1, Thread t2) throws InterruptedException
out - may be nullin - may be nulli2pout - may be nulli2pin - may be nullt1 - may be nullt2 - may be null, ignored, we only join t1InterruptedExceptionboolean getKeepAliveI2P()
On the client side, this is true only if the browser and server both support HTTP keep-alive. On the server side, it's true only if the client supports keep-alive.
boolean getKeepAliveSocket()
Usually true for client-side connections (browser to proxy). Always false for server-side connections (I2P to local service).
protected InputStream getSocketIn() throws IOException
This method is protected to allow subclasses to override socket access for testing or special handling (e.g., SSL unwrapping).
IOException - if the socket is closedprotected OutputStream getSocketOut() throws IOException
IOException - if the socket is closedpublic long getStartedOn()
This value is set at construction time and represents when the runner was created, not when it started executing.
public void setSuccessCallback(I2PTunnelRunner.SuccessCallback sc)
The callback is invoked after the first byte of data is received from the destination, not when the entire transfer completes. Only one of SuccessCallback, onTimeout, or onFail will be called.
sc - the callback to invoke on success, may be nullpublic void streamDone()
streamDone in interface LimitOutputStream.DoneCallback