use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class SAMv3StreamSession method accept.
/**
* Accept a single incoming STREAM on the socket stolen from the handler.
* As of version 3.2 (0.9.24), multiple simultaneous accepts are allowed.
* Accepts and forwarding may not be done at the same time.
*
* @param handler The handler that communicates with the requesting client
* @param verbose If true, SAM will send the Base64-encoded peer Destination of an
* incoming socket as the first line of data sent to its client
* on the handler socket
*
* @throws DataFormatException if the destination is not valid
* @throws ConnectException if the destination refuses connections
* @throws NoRouteToHostException if the destination can't be reached
* @throws InterruptedIOException if the connection timeouts
* @throws I2PException if there's another I2P-related error
* @throws IOException
*/
public void accept(SAMv3Handler handler, boolean verbose) throws I2PException, InterruptedIOException, IOException, SAMException {
synchronized (this.socketServerLock) {
if (this.socketServer != null) {
if (_log.shouldWarn())
_log.warn("a forwarding server is already defined for this destination");
throw new SAMException("a forwarding server is already defined for this destination");
}
}
I2PSocket i2ps = null;
_acceptors.incrementAndGet();
try {
if (_acceptQueue != null)
i2ps = acceptSocket();
else
i2ps = socketMgr.getServerSocket().accept();
} finally {
_acceptors.decrementAndGet();
}
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if (rec == null || i2ps == null)
throw new InterruptedIOException();
if (verbose) {
handler.notifyStreamIncomingConnection(i2ps.getPeerDestination(), i2ps.getPort(), i2ps.getLocalPort());
}
handler.stealSocket();
ReadableByteChannel fromClient = handler.getClientSocket();
ReadableByteChannel fromI2P = Channels.newChannel(i2ps.getInputStream());
WritableByteChannel toClient = handler.getClientSocket();
WritableByteChannel toI2P = Channels.newChannel(i2ps.getOutputStream());
SAMBridge bridge = handler.getBridge();
(new I2PAppThread(rec.getThreadGroup(), new Pipe(fromClient, toI2P, bridge), "AcceptV3 SAMPipeClientToI2P")).start();
(new I2PAppThread(rec.getThreadGroup(), new Pipe(fromI2P, toClient, bridge), "AcceptV3 SAMPipeI2PToClient")).start();
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2PSnarkUtil method connect.
/**
* connect to the given destination
*/
I2PSocket connect(PeerID peer) throws IOException {
I2PSocketManager mgr = _manager;
if (mgr == null)
throw new IOException("No socket manager");
Destination addr = peer.getAddress();
if (addr == null)
throw new IOException("Null address");
if (addr.equals(getMyDestination()))
throw new IOException("Attempt to connect to myself");
Hash dest = addr.calculateHash();
if (_banlist.contains(dest))
throw new IOException("Not trying to contact " + dest.toBase64() + ", as they are banlisted");
try {
// TODO opts.setPort(xxx); connect(addr, opts)
// DHT moved above 6881 in 0.9.9
I2PSocket rv = _manager.connect(addr);
if (rv != null)
_banlist.remove(dest);
return rv;
} catch (I2PException ie) {
_banlist.add(dest);
_context.simpleTimer2().addEvent(new Unbanlist(dest), 10 * 60 * 1000);
IOException ioe = new IOException("Unable to reach the peer " + peer);
ioe.initCause(ie);
throw ioe;
}
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class I2Plistener method run.
/**
* Simply listen on I2P port, and thread connections
*/
public void run() {
boolean g = false;
I2PSocket sessSocket = null;
int conn = 0;
try {
try {
serverSocket.setSoTimeout(50);
while (lives.get()) {
try {
sessSocket = serverSocket.accept();
g = true;
} catch (ConnectException ce) {
g = false;
} catch (SocketTimeoutException ste) {
g = false;
}
if (g) {
g = false;
conn++;
// toss the connection to a new thread.
I2PtoTCP conn_c = new I2PtoTCP(sessSocket, info, database, lives);
Thread t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " I2PtoTCP " + conn);
t.start();
}
}
} catch (I2PException e) {
// bad stuff
System.out.println("Exception " + e);
}
} finally {
try {
serverSocket.close();
} catch (I2PException ex) {
}
// System.out.println("I2Plistener: Close");
}
}
Aggregations