forked from I2P_Developers/i2p.i2p
* Streaming:
- Channel cleanups and comments - New I2PSocketAddress
This commit is contained in:
@@ -7,11 +7,19 @@ import java.net.ConnectException;
|
|||||||
import java.nio.channels.SelectableChannel;
|
import java.nio.channels.SelectableChannel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* As this does not (yet) extend ServerSocketChannel it cannot be returned by StandardServerSocket.getChannel(),
|
||||||
|
* until we implement an I2P SocketAddress class.
|
||||||
|
*
|
||||||
|
* Warning, this interface and implementation is preliminary and subject to change without notice.
|
||||||
|
*
|
||||||
* @since 0.8.11
|
* @since 0.8.11
|
||||||
*/
|
*/
|
||||||
public abstract class AcceptingChannel extends SelectableChannel {
|
public abstract class AcceptingChannel extends SelectableChannel {
|
||||||
|
|
||||||
abstract I2PSocket accept() throws I2PException, ConnectException;
|
abstract I2PSocket accept() throws I2PException, ConnectException;
|
||||||
I2PSocketManager _socketManager;
|
|
||||||
|
protected final I2PSocketManager _socketManager;
|
||||||
|
|
||||||
AcceptingChannel(I2PSocketManager manager) {
|
AcceptingChannel(I2PSocketManager manager) {
|
||||||
this._socketManager = manager;
|
this._socketManager = manager;
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,107 @@
|
|||||||
|
package net.i2p.client.streaming;
|
||||||
|
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
|
import net.i2p.I2PAppContext;
|
||||||
|
import net.i2p.data.Destination;
|
||||||
|
import net.i2p.data.DataHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A SocketAddress (Destination + port) so we can have SocketChannels.
|
||||||
|
* Ports are not widely used in I2P, in most cases the port will be zero.
|
||||||
|
* See InetSocketAddress for javadocs.
|
||||||
|
*
|
||||||
|
* Warning, this interface and implementation is preliminary and subject to change without notice.
|
||||||
|
*
|
||||||
|
* @since 0.9.1
|
||||||
|
*/
|
||||||
|
public class I2PSocketAddress extends SocketAddress {
|
||||||
|
|
||||||
|
private final int _port;
|
||||||
|
private final Destination _dest;
|
||||||
|
private final String _host;
|
||||||
|
|
||||||
|
// no constructor for port-only "wildcard" address
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does not do a reverse lookup. Host will be null.
|
||||||
|
*/
|
||||||
|
public I2PSocketAddress(Destination dest, int port) {
|
||||||
|
_port = port;
|
||||||
|
_dest = dest;
|
||||||
|
_host = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does a naming service lookup to resolve the dest.
|
||||||
|
* May take several seconds for b32.
|
||||||
|
*/
|
||||||
|
public I2PSocketAddress(String host, int port) {
|
||||||
|
_port = port;
|
||||||
|
_dest = I2PAppContext.getGlobalContext().namingService().lookup(host);
|
||||||
|
_host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static I2PSocketAddress createUnresolved(String host, int port) {
|
||||||
|
return new I2PSocketAddress(port, host);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** unresolved */
|
||||||
|
private I2PSocketAddress(int port, String host) {
|
||||||
|
_port = port;
|
||||||
|
_dest = null;
|
||||||
|
_host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return _port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Destination getAddress() {
|
||||||
|
return _dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the host only if given in the constructor. Does not do a reverse lookup.
|
||||||
|
*/
|
||||||
|
public String getHostName() {
|
||||||
|
return _host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isUnresolved() {
|
||||||
|
return _dest == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
if (_dest != null)
|
||||||
|
buf.append(_dest.calculateHash().toString());
|
||||||
|
else
|
||||||
|
buf.append(_host);
|
||||||
|
buf.append(':');
|
||||||
|
buf.append(_port);
|
||||||
|
return buf.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == null || !(obj instanceof I2PSocketAddress))
|
||||||
|
return false;
|
||||||
|
I2PSocketAddress o = (I2PSocketAddress) obj;
|
||||||
|
if (_port != o._port)
|
||||||
|
return false;
|
||||||
|
if (_dest != null)
|
||||||
|
return _dest.equals(o._dest);
|
||||||
|
if (o._dest != null)
|
||||||
|
return false;
|
||||||
|
if (_host != null)
|
||||||
|
return _host.equals(o._host);
|
||||||
|
return o._host == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return DataHelper.hashCode(_dest) ^ DataHelper.hashCode(_host) ^ _port;
|
||||||
|
}
|
||||||
|
}
|
@@ -13,16 +13,21 @@ import java.nio.channels.spi.AbstractSelectionKey;
|
|||||||
import java.nio.channels.spi.SelectorProvider;
|
import java.nio.channels.spi.SelectorProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* As this does not (yet) extend ServerSocketChannel it cannot be returned by StandardServerSocket.getChannel(),
|
||||||
|
* until we implement an I2P SocketAddress class.
|
||||||
|
*
|
||||||
|
* Warning, this interface and implementation is preliminary and subject to change without notice.
|
||||||
|
*
|
||||||
* @since 0.8.11
|
* @since 0.8.11
|
||||||
*/
|
*/
|
||||||
public class AcceptingChannelImpl extends AcceptingChannel {
|
class AcceptingChannelImpl extends AcceptingChannel {
|
||||||
boolean _isRegistered = false;
|
private boolean _isRegistered;
|
||||||
SelectionKey whichKey = null;
|
private SelectionKey whichKey;
|
||||||
SelectorProvider provider = null;
|
private SelectorProvider provider;
|
||||||
Selector sel = null;
|
private Selector sel;
|
||||||
Object lock = null;
|
private Object lock;
|
||||||
I2PSocket next = null;
|
private volatile I2PSocket next;
|
||||||
I2PServerSocket socket;
|
private final I2PServerSocket socket;
|
||||||
|
|
||||||
I2PSocket accept() throws I2PException, ConnectException {
|
I2PSocket accept() throws I2PException, ConnectException {
|
||||||
I2PSocket sock;
|
I2PSocket sock;
|
||||||
@@ -31,9 +36,11 @@ public class AcceptingChannelImpl extends AcceptingChannel {
|
|||||||
} catch(SocketTimeoutException ex) {
|
} catch(SocketTimeoutException ex) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
I2PSocket temp = next;
|
synchronized (this) {
|
||||||
next = sock;
|
I2PSocket temp = next;
|
||||||
return temp;
|
next = sock;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AcceptingChannelImpl(I2PSocketManager manager) {
|
AcceptingChannelImpl(I2PSocketManager manager) {
|
||||||
@@ -96,7 +103,7 @@ public class AcceptingChannelImpl extends AcceptingChannel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readyOps() {
|
public int readyOps() {
|
||||||
if((operations & OP_ACCEPT) != 0)
|
if((operations & OP_ACCEPT) != 0) {
|
||||||
if(next != null) {
|
if(next != null) {
|
||||||
return OP_ACCEPT;
|
return OP_ACCEPT;
|
||||||
} else {
|
} else {
|
||||||
@@ -107,6 +114,7 @@ public class AcceptingChannelImpl extends AcceptingChannel {
|
|||||||
if(next != null)
|
if(next != null)
|
||||||
return OP_ACCEPT;
|
return OP_ACCEPT;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -136,8 +144,9 @@ public class AcceptingChannelImpl extends AcceptingChannel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void implCloseChannel() throws IOException {
|
protected void implCloseChannel() throws IOException {
|
||||||
if(next != null) {
|
I2PSocket nxt = next;
|
||||||
next.close();
|
if(nxt != null) {
|
||||||
|
nxt.close();
|
||||||
}
|
}
|
||||||
_socketManager.destroySocketManager();
|
_socketManager.destroySocketManager();
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,7 @@ import net.i2p.I2PException;
|
|||||||
*/
|
*/
|
||||||
class I2PServerSocketFull implements I2PServerSocket {
|
class I2PServerSocketFull implements I2PServerSocket {
|
||||||
private final I2PSocketManagerFull _socketManager;
|
private final I2PSocketManagerFull _socketManager;
|
||||||
|
private volatile AcceptingChannel _channel;
|
||||||
|
|
||||||
public I2PServerSocketFull(I2PSocketManagerFull mgr) {
|
public I2PServerSocketFull(I2PSocketManagerFull mgr) {
|
||||||
_socketManager = mgr;
|
_socketManager = mgr;
|
||||||
@@ -28,8 +29,10 @@ class I2PServerSocketFull implements I2PServerSocket {
|
|||||||
/**
|
/**
|
||||||
* @since 0.8.11
|
* @since 0.8.11
|
||||||
*/
|
*/
|
||||||
public AcceptingChannel getChannel() {
|
public synchronized AcceptingChannel getChannel() {
|
||||||
return new AcceptingChannelImpl(_socketManager);
|
if (_channel == null)
|
||||||
|
_channel = new AcceptingChannelImpl(_socketManager);
|
||||||
|
return _channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getSoTimeout() {
|
public long getSoTimeout() {
|
||||||
|
@@ -16,6 +16,7 @@ class I2PSocketFull implements I2PSocket {
|
|||||||
private Connection _connection;
|
private Connection _connection;
|
||||||
private Destination _remotePeer;
|
private Destination _remotePeer;
|
||||||
private Destination _localPeer;
|
private Destination _localPeer;
|
||||||
|
private volatile MessageChannel _channel;
|
||||||
|
|
||||||
public I2PSocketFull(Connection con) {
|
public I2PSocketFull(Connection con) {
|
||||||
_connection = con;
|
_connection = con;
|
||||||
@@ -70,8 +71,10 @@ class I2PSocketFull implements I2PSocket {
|
|||||||
/**
|
/**
|
||||||
* @since 0.8.9
|
* @since 0.8.9
|
||||||
*/
|
*/
|
||||||
public SelectableChannel getChannel() {
|
public synchronized SelectableChannel getChannel() {
|
||||||
return new MessageChannel(this);
|
if (_channel == null)
|
||||||
|
_channel = new MessageChannel(this);
|
||||||
|
return _channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -15,18 +15,23 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* As this does not (yet) extend SocketChannel it cannot be returned by StandardSocket.getChannel(),
|
||||||
|
* until we implement an I2P SocketAddress class.
|
||||||
|
*
|
||||||
|
* Warning, this interface and implementation is preliminary and subject to change without notice.
|
||||||
|
*
|
||||||
* @since 0.8.9
|
* @since 0.8.9
|
||||||
*/
|
*/
|
||||||
public class MessageChannel extends SelectableChannel implements ReadableByteChannel, WritableByteChannel {
|
public class MessageChannel extends SelectableChannel implements ReadableByteChannel, WritableByteChannel {
|
||||||
|
|
||||||
final MessageInputStream in;
|
private final MessageInputStream in;
|
||||||
final MessageOutputStream out;
|
private final MessageOutputStream out;
|
||||||
boolean _isRegistered = false;
|
private boolean _isRegistered;
|
||||||
SelectionKey whichKey = null;
|
private SelectionKey whichKey;
|
||||||
SelectorProvider provider = null;
|
private SelectorProvider provider;
|
||||||
Selector sel = null;
|
private Selector sel;
|
||||||
Object lock = null;
|
private Object lock;
|
||||||
I2PSocket socket;
|
private final I2PSocket socket;
|
||||||
|
|
||||||
MessageChannel(I2PSocket socket) {
|
MessageChannel(I2PSocket socket) {
|
||||||
try {
|
try {
|
||||||
@@ -145,10 +150,10 @@ public class MessageChannel extends SelectableChannel implements ReadableByteCha
|
|||||||
* returns 0, which happens when there's
|
* returns 0, which happens when there's
|
||||||
* no more data available.
|
* no more data available.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public int read(ByteBuffer buf) throws IOException {
|
public int read(ByteBuffer buf) throws IOException {
|
||||||
int amount = 0;
|
int amount = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
// TODO if buf.hasArray() ... getArray() ... getArrayOffset() ...
|
||||||
byte[] lbuf = new byte[buf.remaining()];
|
byte[] lbuf = new byte[buf.remaining()];
|
||||||
int samount = in.read(lbuf);
|
int samount = in.read(lbuf);
|
||||||
if (samount <= 0) {
|
if (samount <= 0) {
|
||||||
@@ -167,12 +172,12 @@ public class MessageChannel extends SelectableChannel implements ReadableByteCha
|
|||||||
* already set buffer size. Once it starts to fail
|
* already set buffer size. Once it starts to fail
|
||||||
* (wait timeout is 0) then put the bytes back and return.
|
* (wait timeout is 0) then put the bytes back and return.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public int write(ByteBuffer buf) throws IOException {
|
public int write(ByteBuffer buf) throws IOException {
|
||||||
int written = 0;
|
int written = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if(buf.remaining()==0)
|
if(buf.remaining()==0)
|
||||||
return written;
|
return written;
|
||||||
|
// TODO if buf.hasArray() ... getArray() ... getArrayOffset() ...
|
||||||
byte[] lbuf = new byte[Math.min(buf.remaining(), 0x1000)];
|
byte[] lbuf = new byte[Math.min(buf.remaining(), 0x1000)];
|
||||||
buf.get(lbuf);
|
buf.get(lbuf);
|
||||||
try {
|
try {
|
||||||
|
@@ -72,10 +72,11 @@ class StandardServerSocket extends ServerSocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return null always
|
* @return null always, see AcceptingChannelImpl for more info
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ServerSocketChannel getChannel() {
|
public ServerSocketChannel getChannel() {
|
||||||
|
//return _socket.getChannel();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -65,10 +65,11 @@ class StandardSocket extends Socket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return null always
|
* @return null always, see MessageChannel for more info
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public SocketChannel getChannel() {
|
public SocketChannel getChannel() {
|
||||||
|
//return _socket.getChannel();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
history.txt
14
history.txt
@@ -1,3 +1,15 @@
|
|||||||
|
2012-06-13 zzz
|
||||||
|
* I2PSocketEepGet: Set port to 80
|
||||||
|
* I2PTunnel:
|
||||||
|
- Pass port through HTTP client proxy
|
||||||
|
- HTTP server proxy sets host header to
|
||||||
|
the value of "spoofedhost.xx" option for port xx
|
||||||
|
- Set client options more efficiently
|
||||||
|
* i2psnark: Possible fix for piece-after-choke
|
||||||
|
* Streaming:
|
||||||
|
- Channel cleanups and comments
|
||||||
|
- New I2PSocketAddress
|
||||||
|
|
||||||
2012-06-11 zzz
|
2012-06-11 zzz
|
||||||
* i2psnark:
|
* i2psnark:
|
||||||
- Display torrent file downloads in torrent area
|
- Display torrent file downloads in torrent area
|
||||||
@@ -8,7 +20,7 @@
|
|||||||
- Reduce delay between peer adds for faster startup
|
- Reduce delay between peer adds for faster startup
|
||||||
- Thread the announces and reduce timeout when stopping
|
- Thread the announces and reduce timeout when stopping
|
||||||
* NativeBigInteger: Workaround for Raspberry Pi to load the correct lib
|
* NativeBigInteger: Workaround for Raspberry Pi to load the correct lib
|
||||||
* Router: Don't let shutdown tasks hang the shutdown
|
* Router: Don't let shutdown tasks hang the shutdown (tickets #564, #566)
|
||||||
|
|
||||||
2012-06-08 zzz
|
2012-06-08 zzz
|
||||||
* i2psnark:
|
* i2psnark:
|
||||||
|
@@ -18,7 +18,7 @@ public class RouterVersion {
|
|||||||
/** deprecated */
|
/** deprecated */
|
||||||
public final static String ID = "Monotone";
|
public final static String ID = "Monotone";
|
||||||
public final static String VERSION = CoreVersion.VERSION;
|
public final static String VERSION = CoreVersion.VERSION;
|
||||||
public final static long BUILD = 13;
|
public final static long BUILD = 14;
|
||||||
|
|
||||||
/** for example "-test" */
|
/** for example "-test" */
|
||||||
public final static String EXTRA = "";
|
public final static String EXTRA = "";
|
||||||
|
Reference in New Issue
Block a user