only block adding more *outbound* data, not handling data received from I2P. The data has already been received by the router and delivered to the streaming lib (and is sitting in RAM anyway...)
logging
This commit is contained in:
@ -75,8 +75,10 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
remote = peer;
|
remote = peer;
|
||||||
_socketId = ++__socketId;
|
_socketId = ++__socketId;
|
||||||
local = mgr.getSession().getMyDestination();
|
local = mgr.getSession().getMyDestination();
|
||||||
in = new I2PInputStream();
|
String us = mgr.getSession().getMyDestination().calculateHash().toBase64().substring(0,4);
|
||||||
I2PInputStream pin = new I2PInputStream();
|
String name = us + (outgoing ? "->" : "<-") + peer.calculateHash().toBase64().subSequence(0,4);
|
||||||
|
in = new I2PInputStream(name + " in");
|
||||||
|
I2PInputStream pin = new I2PInputStream(name + " out");
|
||||||
out = new I2POutputStream(pin);
|
out = new I2POutputStream(pin);
|
||||||
new I2PSocketRunner(pin);
|
new I2PSocketRunner(pin);
|
||||||
this.localID = localID;
|
this.localID = localID;
|
||||||
@ -180,19 +182,9 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
public void queueData(byte[] data) {
|
public void queueData(byte[] data) {
|
||||||
_bytesRead += data.length;
|
_bytesRead += data.length;
|
||||||
try {
|
try {
|
||||||
in.queueData(data);
|
in.queueData(data, false);
|
||||||
} catch (InterruptedIOException iie) {
|
|
||||||
if (_log.shouldLog(Log.ERROR))
|
|
||||||
_log.error("Queue overflow, closing the stream", iie);
|
|
||||||
try {
|
|
||||||
close();
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR))
|
|
||||||
_log.error("Error closing the stream due to overflow", ioe);
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
if (_log.shouldLog(Log.ERROR))
|
_log.log(Log.CRIT, "wtf, we said DONT block, how can we timeout?", ioe);
|
||||||
_log.error("Connection closed while writing to the socket", ioe);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,16 +303,24 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private class I2PInputStream extends InputStream {
|
private class I2PInputStream extends InputStream {
|
||||||
|
private String streamName;
|
||||||
private ByteCollector bc = new ByteCollector();
|
private ByteCollector bc = new ByteCollector();
|
||||||
private boolean inStreamClosed = false;
|
private boolean inStreamClosed = false;
|
||||||
|
|
||||||
private long readTimeout = -1;
|
private long readTimeout = -1;
|
||||||
|
|
||||||
|
public I2PInputStream(String name) {
|
||||||
|
streamName = name;
|
||||||
|
}
|
||||||
|
|
||||||
public long getReadTimeout() {
|
public long getReadTimeout() {
|
||||||
return readTimeout;
|
return readTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getStreamPrefix() {
|
||||||
|
return getPrefix() + streamName + ": ";
|
||||||
|
}
|
||||||
|
|
||||||
public void setReadTimeout(long ms) {
|
public void setReadTimeout(long ms) {
|
||||||
readTimeout = ms;
|
readTimeout = ms;
|
||||||
}
|
}
|
||||||
@ -335,7 +335,8 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
|
|
||||||
public int read(byte[] b, int off, int len) throws IOException {
|
public int read(byte[] b, int off, int len) throws IOException {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug(getPrefix() + "Read called for " + len + " bytes (avail=" + bc.getCurrentSize() + "): " + this.hashCode());
|
_log.debug(getStreamPrefix() + "Read called for " + len + " bytes (avail="
|
||||||
|
+ bc.getCurrentSize() + "): " + this.hashCode());
|
||||||
if (len == 0) return 0;
|
if (len == 0) return 0;
|
||||||
long dieAfter = System.currentTimeMillis() + readTimeout;
|
long dieAfter = System.currentTimeMillis() + readTimeout;
|
||||||
byte[] read = null;
|
byte[] read = null;
|
||||||
@ -349,7 +350,9 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
synchronized (flagLock) {
|
synchronized (flagLock) {
|
||||||
if (closed) {
|
if (closed) {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug(getPrefix() + "Closed is set after reading " + _bytesRead + " and writing " + _bytesWritten + ", so closing stream: " + hashCode());
|
_log.debug(getStreamPrefix() + "Closed is set after reading "
|
||||||
|
+ _bytesRead + " and writing " + _bytesWritten
|
||||||
|
+ ", so closing stream: " + hashCode());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -365,7 +368,8 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
|
|
||||||
if ((readTimeout >= 0)
|
if ((readTimeout >= 0)
|
||||||
&& (System.currentTimeMillis() >= dieAfter)) {
|
&& (System.currentTimeMillis() >= dieAfter)) {
|
||||||
throw new InterruptedIOException(getPrefix() + "Timeout reading from I2PSocket (" + readTimeout + " msecs)");
|
throw new InterruptedIOException(getStreamPrefix() + "Timeout reading from I2PSocket ("
|
||||||
|
+ readTimeout + " msecs)");
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (bc) {
|
synchronized (bc) {
|
||||||
@ -377,7 +381,7 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
System.arraycopy(read, 0, b, off, read.length);
|
System.arraycopy(read, 0, b, off, read.length);
|
||||||
|
|
||||||
if (_log.shouldLog(Log.DEBUG)) {
|
if (_log.shouldLog(Log.DEBUG)) {
|
||||||
_log.debug(getPrefix() + "Read from I2PInputStream " + hashCode() + " returned "
|
_log.debug(getStreamPrefix() + "Read from I2PInputStream " + hashCode() + " returned "
|
||||||
+ read.length + " bytes");
|
+ read.length + " bytes");
|
||||||
}
|
}
|
||||||
//if (_log.shouldLog(Log.DEBUG)) {
|
//if (_log.shouldLog(Log.DEBUG)) {
|
||||||
@ -397,46 +401,54 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
/**
|
/**
|
||||||
* Add the data to the queue
|
* Add the data to the queue
|
||||||
*
|
*
|
||||||
|
* @param allowBlock if true, we will block if the buffer and the socket options
|
||||||
|
* say so, otherwise we simply take the data regardless.
|
||||||
* @throws InterruptedIOException if the queue's buffer is full, the socket has
|
* @throws InterruptedIOException if the queue's buffer is full, the socket has
|
||||||
* a write timeout, and that timeout is exceeded
|
* a write timeout, and that timeout is exceeded
|
||||||
* @throws IOException if the connection was closed while queueing up the data
|
* @throws IOException if the connection was closed while queueing up the data
|
||||||
*/
|
*/
|
||||||
public void queueData(byte[] data) throws InterruptedIOException, IOException {
|
void queueData(byte[] data, boolean allowBlock) throws InterruptedIOException, IOException {
|
||||||
queueData(data, 0, data.length);
|
queueData(data, 0, data.length, allowBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the data to the queue
|
* Add the data to the queue
|
||||||
*
|
*
|
||||||
|
* @param allowBlock if true, we will block if the buffer and the socket options
|
||||||
|
* say so, otherwise we simply take the data regardless.
|
||||||
* @throws InterruptedIOException if the queue's buffer is full, the socket has
|
* @throws InterruptedIOException if the queue's buffer is full, the socket has
|
||||||
* a write timeout, and that timeout is exceeded
|
* a write timeout, and that timeout is exceeded
|
||||||
* @throws IOException if the connection was closed while queueing up the data
|
* @throws IOException if the connection was closed while queueing up the data
|
||||||
*/
|
*/
|
||||||
public void queueData(byte[] data, int off, int len) throws InterruptedIOException, IOException {
|
public void queueData(byte[] data, int off, int len, boolean allowBlock) throws InterruptedIOException, IOException {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug(getPrefix() + "Insert " + len + " bytes into queue: " + hashCode());
|
_log.debug(getStreamPrefix() + "Insert " + len + " bytes into queue: " + hashCode());
|
||||||
Clock clock = I2PAppContext.getGlobalContext().clock();
|
Clock clock = I2PAppContext.getGlobalContext().clock();
|
||||||
long endAfter = clock.now() + _options.getWriteTimeout();
|
long endAfter = clock.now() + _options.getWriteTimeout();
|
||||||
synchronized (bc) {
|
synchronized (bc) {
|
||||||
if (_options.getMaxBufferSize() > 0) {
|
if (allowBlock) {
|
||||||
while (bc.getCurrentSize() > _options.getMaxBufferSize()) {
|
if (_options.getMaxBufferSize() > 0) {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
while (bc.getCurrentSize() > _options.getMaxBufferSize()) {
|
||||||
_log.debug("Buffer size exceeded: pending " + bc.getCurrentSize() + " limit " + _options.getMaxBufferSize());
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
if (_options.getWriteTimeout() > 0) {
|
_log.debug(getStreamPrefix() + "Buffer size exceeded: pending "
|
||||||
long timeLeft = endAfter - clock.now();
|
+ bc.getCurrentSize() + " limit " + _options.getMaxBufferSize());
|
||||||
if (timeLeft <= 0) {
|
if (_options.getWriteTimeout() > 0) {
|
||||||
long waited = _options.getWriteTimeout() - timeLeft;
|
long timeLeft = endAfter - clock.now();
|
||||||
throw new InterruptedIOException("Waited too long (" + waited + "ms) to write "
|
if (timeLeft <= 0) {
|
||||||
+ len + " with a buffer at " + bc.getCurrentSize());
|
long waited = _options.getWriteTimeout() - timeLeft;
|
||||||
|
throw new InterruptedIOException(getStreamPrefix() + "Waited too long ("
|
||||||
|
+ waited + "ms) to write "
|
||||||
|
+ len + " with a buffer at " + bc.getCurrentSize());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (inStreamClosed)
|
||||||
|
throw new IOException(getStreamPrefix() + "Stream closed while writing");
|
||||||
|
if (_closedOn > 0)
|
||||||
|
throw new IOException(getStreamPrefix() + "I2PSocket closed while writing");
|
||||||
|
try {
|
||||||
|
bc.wait(1000);
|
||||||
|
} catch (InterruptedException ie) {}
|
||||||
}
|
}
|
||||||
if (inStreamClosed)
|
|
||||||
throw new IOException("Stream closed while writing");
|
|
||||||
if (_closedOn > 0)
|
|
||||||
throw new IOException("I2PSocket closed while writing");
|
|
||||||
try {
|
|
||||||
bc.wait(1000);
|
|
||||||
} catch (InterruptedException ie) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bc.append(data, off, len);
|
bc.append(data, off, len);
|
||||||
@ -477,7 +489,7 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
|
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
_bytesWritten += len;
|
_bytesWritten += len;
|
||||||
sendTo.queueData(b, off, len);
|
sendTo.queueData(b, off, len, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
@ -536,6 +548,9 @@ class I2PSocketImpl implements I2PSocket {
|
|||||||
_log.warn(getPrefix() + "Error sending message to peer. Killing socket runner");
|
_log.warn(getPrefix() + "Error sending message to peer. Killing socket runner");
|
||||||
errorOccurred();
|
errorOccurred();
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug(getPrefix() + "Message sent to peer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user