use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PTunnelServer method blockingHandle.
/**
* This is run in a thread from a limited-size thread pool via Handler.run(),
* except for a standard server (this class, no extension, as determined in getUsePool()),
* it is run directly in the acceptor thread (see run()).
*
* In either case, this method and any overrides must spawn a thread and return quickly.
* If blocking while reading the headers (as in HTTP and IRC), the thread pool
* may be exhausted.
*
* See PROP_USE_POOL, DEFAULT_USE_POOL, PROP_HANDLER_COUNT, DEFAULT_HANDLER_COUNT
*/
protected void blockingHandle(I2PSocket socket) {
if (_log.shouldLog(Log.INFO))
_log.info("Incoming connection to '" + toString() + "' port " + socket.getLocalPort() + " from: " + socket.getPeerDestination().calculateHash() + " port " + socket.getPort());
long afterAccept = getTunnel().getContext().clock().now();
long afterSocket = -1;
// threads.
try {
socket.setReadTimeout(readTimeout);
Socket s = getSocket(socket.getPeerDestination().calculateHash(), socket.getLocalPort());
afterSocket = getTunnel().getContext().clock().now();
Thread t = new I2PTunnelRunner(s, socket, slock, null, null, null, (I2PTunnelRunner.FailCallback) null);
// run in the unlimited client pool
// t.start();
_clientExecutor.execute(t);
long afterHandle = getTunnel().getContext().clock().now();
long timeToHandle = afterHandle - afterAccept;
if ((timeToHandle > 1000) && (_log.shouldLog(Log.WARN)))
_log.warn("Took a while to handle the request for " + remoteHost + ':' + remotePort + " [" + timeToHandle + ", socket create: " + (afterSocket - afterAccept) + "]");
} catch (SocketException ex) {
try {
socket.reset();
} catch (IOException ioe) {
}
if (_log.shouldLog(Log.ERROR))
_log.error("Error connecting to server " + remoteHost + ':' + remotePort, ex);
} catch (IOException ex) {
_log.error("Error while waiting for I2PConnections", ex);
}
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PTunnelDCCClient method clientConnectionRun.
/**
* Accept one connection only.
*/
protected void clientConnectionRun(Socket s) {
I2PSocket i2ps = null;
if (_log.shouldLog(Log.INFO))
_log.info("Opening DCC connection to " + _dest + ':' + _remotePort);
Destination dest = _context.namingService().lookup(_dest);
if (dest == null) {
_log.error("Could not find leaseset for DCC connection to " + _dest + ':' + _remotePort);
closeSocket(s);
stop();
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
return;
}
I2PSocketOptions opts = sockMgr.buildOptions();
opts.setPort(_remotePort);
try {
i2ps = createI2PSocket(dest, opts);
Thread t = new Runner(s, i2ps);
// we are called from an unlimited thread pool, so run inline
// t.start();
t.run();
} catch (IOException ex) {
_log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
} catch (I2PException ex) {
_log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
} finally {
// only because we are running it inline
closeSocket(s);
if (i2ps != null) {
try {
i2ps.close();
} catch (IOException ioe) {
}
}
}
stop();
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PTunnelDCCServer method close.
@Override
public boolean close(boolean forced) {
_outgoing.clear();
_active.clear();
for (I2PSocket s : _sockList) {
try {
s.close();
} catch (IOException ioe) {
}
}
_sockList.clear();
return super.close(forced);
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PTunnelDCCServer method blockingHandle.
/**
* An incoming DCC connection, only accept for a known port.
* Passed through without filtering.
*/
@Override
protected void blockingHandle(I2PSocket socket) {
if (_log.shouldLog(Log.INFO))
_log.info("Incoming connection to '" + toString() + "' from: " + socket.getPeerDestination().calculateHash().toBase64());
try {
expireOutbound();
int myPort = socket.getLocalPort();
// Port is a one-time-use only
LocalAddress local = _outgoing.remove(Integer.valueOf(myPort));
if (local == null) {
if (_log.shouldLog(Log.WARN))
_log.warn("Rejecting incoming DCC connection for unknown port " + myPort);
try {
socket.close();
} catch (IOException ioe) {
}
return;
}
if (_log.shouldLog(Log.WARN))
_log.warn("Incoming DCC connection for I2P port " + myPort + " sending to " + local.ia + ':' + local.port);
try {
Socket s = new Socket(local.ia, local.port);
_sockList.add(socket);
Thread t = new I2PTunnelRunner(s, socket, slock, null, null, _sockList, (I2PTunnelRunner.FailCallback) null);
// run in the unlimited client pool
// t.start();
_clientExecutor.execute(t);
local.socket = socket;
local.expire = getTunnel().getContext().clock().now() + OUTBOUND_EXPIRE;
_active.put(Integer.valueOf(myPort), local);
} catch (SocketException ex) {
try {
socket.reset();
} catch (IOException ioe) {
}
_log.error("Error relaying incoming DCC connection to IRC client at " + local.ia + ':' + local.port, ex);
}
} catch (IOException ex) {
_log.error("Error while waiting for I2PConnections", ex);
}
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PSOCKSTunnel method clientConnectionRun.
protected void clientConnectionRun(Socket s) {
I2PSocket destSock = null;
try {
try {
s.setSoTimeout(INITIAL_SO_TIMEOUT);
} catch (SocketException ioe) {
}
SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(_context, s, getTunnel().getClientOptions());
Socket clientSock = serv.getClientSocket();
try {
s.setSoTimeout(0);
} catch (SocketException ioe) {
}
destSock = serv.getDestinationI2PSocket(this);
Thread t = new I2PTunnelRunner(clientSock, destSock, sockLock, null, null, mySockets, (I2PTunnelRunner.FailCallback) null);
// we are called from an unlimited thread pool, so run inline
// t.start();
t.run();
} catch (SOCKSException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Error from SOCKS connection", e);
} finally {
// only because we are running it inline
closeSocket(s);
if (destSock != null)
try {
destSock.close();
} catch (IOException ioe) {
}
}
}
Aggregations