@Override public void close() { //if (_log.shouldLog(Log.DEBUG)) // _log.debug("Closing " + _port); _running = false; _sockets.remove(_port); _acceptQueue.clear(); try { // use null streams as a poison _acceptQueue.put(new InternalSocket(null, null)); } catch (InterruptedException ie) {} }
/** * Supported as of 0.9.33, prior to that threw UnsupportedOperationException * @return 1 if connected, -1 if not */ @Override public int getLocalPort() { return isConnected() ? 1 : -1; }
/** * This is how the client connects. * * @param port > 0 */ static void internalConnect(int port, InternalSocket clientSock) throws IOException { InternalServerSocket iss = _sockets.get(Integer.valueOf(port)); if (iss == null) throw new IOException("No server for port: " + port); TimeoutPipedInputStream cis = new TimeoutPipedInputStream(64*1024); TimeoutPipedInputStream sis = new TimeoutPipedInputStream(64*1024); PipedOutputStream cos = new TimeoutPipedOutputStream(sis); PipedOutputStream sos = new TimeoutPipedOutputStream(cis); clientSock.setInputStream(cis); clientSock.setOutputStream(cos); iss.queueConnection(new InternalSocket(sis, sos)); }
/** * Connect to a HTTP proxy. * Proxy address must be in _proxyHost and _proxyPort. * Side effects: Sets _proxy, _proxyIn, _proxyOut, * and other globals via readHeaders() * * @param port what the proxy should connect to, probably 4444 * @since 0.9.33 */ private void internalHttpProxyConnect(String host, int port) throws IOException { // connect to the proxy _proxy = InternalSocket.getSocket(_proxyHost, _proxyPort); httpProxyConnect(_proxy, host, port); }
@Override public Socket accept() throws IOException { InternalSocket serverSock = null; while (_running) { //if (_log.shouldLog(Log.DEBUG)) // _log.debug("Accepting " + _port); try { serverSock = _acceptQueue.take(); } catch (InterruptedException ie) { if (_running) throw new InterruptedIOException(); throw new IOException("closed"); } if (serverSock.getInputStream() == null) // poison throw new IOException("closed"); //if (_log.shouldLog(Log.DEBUG)) // _log.debug("Accepted " + _port); break; } return serverSock; }
_proxy = InternalSocket.getSocket(_proxyHost, _proxyPort); } else { try {
/** * Supported as of 0.9.33, prior to that threw UnsupportedOperationException * @return if connected: actual port for clients, 1 for servers; -1 if not */ @Override public int getPort() { return isConnected() ? _port : 0; } /** @deprecated unsupported */
/** * Convenience method to return either a Socket or an InternalSocket * @param port > 0 */ public static Socket getSocket(String host, int port) throws IOException { if (I2PAppContext.getGlobalContext().isRouterContext() && (host.equals("127.0.0.1") || host.equals("localhost"))) { try { return new InternalSocket(port); } catch (IOException ioe) {} // guess it wasn't really internal... } return new Socket(host, port); }