cleaned up slice processing

reduced max queued messages per connection to 10 (additional ones are immediately marked as failed)
update the I2P_FLAG byte to '*' making this NOT BACKWARDS COMPATIBLE
formatting
This commit is contained in:
jrandom
2004-05-20 10:51:22 +00:00
committed by zzz
parent 242b9a6af9
commit 60e05e270a
2 changed files with 151 additions and 145 deletions

View File

@ -29,8 +29,8 @@ class SocketCreator implements Runnable {
public boolean couldEstablish() { return _established; } public boolean couldEstablish() { return _established; }
/** the first byte sent and received must be 0x22 */ /** the first byte sent and received must be 0x2A */
public final static int I2P_FLAG = 0x22; public final static int I2P_FLAG = 0x2A;
public void run() { public void run() {
if (_keepOpen) { if (_keepOpen) {

View File

@ -69,7 +69,7 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
protected RouterContext _context; protected RouterContext _context;
public final static String PARAM_MAX_QUEUED_MESSAGES = "i2np.tcp.maxQueuedMessages"; public final static String PARAM_MAX_QUEUED_MESSAGES = "i2np.tcp.maxQueuedMessages";
private final static int DEFAULT_MAX_QUEUED_MESSAGES = 20; private final static int DEFAULT_MAX_QUEUED_MESSAGES = 10;
public TCPConnection(RouterContext context, Socket s, boolean locallyInitiated) { public TCPConnection(RouterContext context, Socket s, boolean locallyInitiated) {
_context = context; _context = context;
@ -272,7 +272,7 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
if (fail) { if (fail) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("too many queued messages to " + _remoteIdentity.getHash().toBase64()); _log.error("too many queued messages to " + _remoteIdentity.getHash().toBase64() + ": " + totalPending);
msg.timestamp("TCPConnection.addMessage exceeded max queued"); msg.timestamp("TCPConnection.addMessage exceeded max queued");
_transport.afterSend(msg, false); _transport.afterSend(msg, false);
@ -415,26 +415,24 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
private boolean _running; private boolean _running;
public void run() { public void run() {
_running = true; _running = true;
try {
while (_running) { while (_running) {
long startSlice = _context.clock().now(); long startSlice = _context.clock().now();
_lastSliceRun = startSlice; _lastSliceRun = startSlice;
processSlice(); boolean processOk = processSlice();
long endSlice = _context.clock().now(); if (!processOk) {
closeConnection();
return;
} }
} catch (IOException ioe) { long endSlice = _context.clock().now();
if (_log.shouldLog(Log.ERROR))
_log.error("Connection runner failed with an IO exception to "
+ _remoteIdentity.getHash().toBase64(), ioe);
closeConnection();
} catch (Throwable t) {
if (_log.shouldLog(Log.ERROR))
_log.error("Somehow we ran into an uncaught exception running the connection!", t);
closeConnection();
} }
} }
private void processSlice() throws IOException { /**
* Process a slice (push a message if available).
*
* @return true if the operation succeeded, false if there was a critical error
*/
private boolean processSlice() {
long start = _context.clock().now(); long start = _context.clock().now();
OutNetMessage msg = null; OutNetMessage msg = null;
@ -451,7 +449,7 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
} catch (InterruptedException ie) {} } catch (InterruptedException ie) {}
} }
remaining = _toBeSent.size(); remaining = _toBeSent.size();
if (remaining <= 0) return; if (remaining <= 0) return true;
msg = (OutNetMessage)_toBeSent.remove(0); msg = (OutNetMessage)_toBeSent.remove(0);
remaining--; remaining--;
if ( (msg.getExpiration() > 0) && (msg.getExpiration() < start) ) { if ( (msg.getExpiration() > 0) && (msg.getExpiration() < start) ) {
@ -469,7 +467,7 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
+ _remoteIdentity.getHash().toBase64() + ": " + msg); + _remoteIdentity.getHash().toBase64() + ": " + msg);
msg.timestamp("TCPConnection.runner.processSlice expired"); msg.timestamp("TCPConnection.runner.processSlice expired");
_transport.afterSend(msg, false); _transport.afterSend(msg, false);
return; return true;
} }
if (remaining > 0) { if (remaining > 0) {
@ -484,21 +482,33 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
msg.timestamp("TCPConnection.runner.processSlice fetched"); msg.timestamp("TCPConnection.runner.processSlice fetched");
//_log.debug("Processing slice - msg to be sent"); //_log.debug("Processing slice - msg to be sent");
try {
byte data[] = msg.getMessageData(); byte data[] = msg.getMessageData();
if (data == null) {
if (_log.shouldLog(Log.ERROR))
_log.error("wtf, for some reason, an I2NPMessage couldn't be serialized...");
return true;
}
msg.timestamp("TCPConnection.runner.processSlice before sending " msg.timestamp("TCPConnection.runner.processSlice before sending "
+ data.length + " bytes"); + data.length + " bytes");
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Sending " + data.length + " bytes in the slice... to " _log.debug("Sending " + data.length + " bytes in the slice... to "
+ _remoteIdentity.getHash().toBase64()); + _remoteIdentity.getHash().toBase64());
try {
synchronized (_out) { synchronized (_out) {
_out.write(data); _out.write(data);
_out.flush(); _out.flush();
} }
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("IO error writing out a " + data.length + " byte message to "
+ _remoteIdentity.getHash().toBase64());
return false;
}
msg.timestamp("TCPConnection.runner.processSlice sent and flushed"); msg.timestamp("TCPConnection.runner.processSlice sent and flushed");
long end = _context.clock().now(); long end = _context.clock().now();
long timeLeft = msg.getMessage().getMessageExpiration().getTime() - end; long timeLeft = msg.getMessage().getMessageExpiration().getTime() - end;
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Message " + msg.getMessage().getClass().getName() _log.info("Message " + msg.getMessage().getClass().getName()
@ -534,12 +544,8 @@ class TCPConnection implements I2NPMessageReader.I2NPMessageEventListener {
+ "ms) sending " + data.length + " bytes to " + "ms) sending " + data.length + " bytes to "
+ _remoteIdentity.getHash().toBase64()); + _remoteIdentity.getHash().toBase64());
} }
} catch (IOException ioe) {
msg.timestamp("TCPConnection.runner.processSlice failed to send/flushflushed");
_transport.afterSend(msg, false);
throw ioe;
}
} }
return true;
} }
public void stopRunning() { public void stopRunning() {