Changeset e7898b5


Ignore:
Timestamp:
Mar 16, 2012 12:16:05 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
045627a
Parents:
080f435
Message:

for speed and to reduce object churn

Location:
router/java/src/net/i2p/router/tunnel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/FragmentHandler.java

    r080f435 re7898b5  
    107107        _context = context;
    108108        _log = context.logManager().getLog(FragmentHandler.class);
    109         _fragmentedMessages = new HashMap(8);
     109        _fragmentedMessages = new HashMap(16);
    110110        _receiver = receiver;
    111111        // all createRateStat in TunnelDispatcher
     
    352352        offset += 2;
    353353       
    354         FragmentedMessage msg = null;
    355354        if (fragmented) {
     355            FragmentedMessage msg;
    356356            synchronized (_fragmentedMessages) {
    357357                msg = _fragmentedMessages.get(Long.valueOf(messageId));
     
    361361                }
    362362            }
    363         } else {
    364             msg = new FragmentedMessage(_context);
    365         }
    366        
    367         if (fragmented) {
     363
    368364            // synchronized is required, fragments may be arriving in different threads
    369365            synchronized(msg) {
     
    388384                }
    389385            }
    390         } else {       
    391             // synchronized not required if !fragmented
    392             boolean ok = msg.receive(messageId, preprocessed, offset, size, true, router, tunnelId);
    393             if (!ok) return -1;
     386        } else {
     387            // Unfragmented
     388            // synchronized not required
    394389            // always complete, never an expire event
    395             receiveComplete(msg);
     390            receiveComplete(preprocessed, offset, size, router, tunnelId);
    396391        }
    397392       
    398393        offset += size;
    399394       
    400         if (_log.shouldLog(Log.DEBUG))
    401             _log.debug("Handling finished message " + msg.getMessageId() + " at offset " + offset);
     395        //if (_log.shouldLog(Log.DEBUG))
     396        //    _log.debug("Handling finished message " + msg.getMessageId() + " at offset " + offset);
    402397        return offset;
    403398    }
     
    463458    }
    464459   
     460   
    465461    private void receiveComplete(FragmentedMessage msg) {
    466462        if (msg == null)
     
    501497    }
    502498
     499    /**
     500     *  Zero-copy reception of an unfragmented message
     501     *  @since 0.9
     502     */
     503    private void receiveComplete(byte[] data, int offset, int len, Hash router, TunnelId tunnelId) {
     504        _completed++;
     505        try {
     506            if (_log.shouldLog(Log.DEBUG))
     507                _log.debug("RECV unfrag(" + len + ')');
     508
     509            // TODO read in as unknown message for outbound tunnels,
     510            // since this will just be packaged in a TunnelGatewayMessage.
     511            // Not a big savings since most everything is a GarlicMessage
     512            // and so the readMessage() call is fast.
     513            // The unencrypted messages at the OBEP are (V)TBMs
     514            // and perhaps an occasional DatabaseLookupMessage
     515            I2NPMessageHandler h = new I2NPMessageHandler(_context);
     516            h.readMessage(data, offset, len);
     517            I2NPMessage m = h.lastRead();
     518            noteReception(m.getUniqueId(), 0, "complete: ");// + msg.toString());
     519            noteCompletion(m.getUniqueId());
     520            _receiver.receiveComplete(m, router, tunnelId);
     521        } catch (I2NPMessageException ime) {
     522            if (_log.shouldLog(Log.WARN)) {
     523                _log.warn("Error receiving unfragmented message (corrupt?)", ime);
     524                _log.warn("DUMP:\n" + HexDump.dump(data, offset, len));
     525                _log.warn("RAW:\n" + Base64.encode(data, offset, len));
     526            }
     527        }
     528    }
     529
    503530    protected void noteReception(long messageId, int fragmentId, Object status) {}
    504531    protected void noteCompletion(long messageId) {}
     
    524551   
    525552    private class RemoveFailed implements SimpleTimer.TimedEvent {
    526         private FragmentedMessage _msg;
     553        private final FragmentedMessage _msg;
     554
    527555        public RemoveFailed(FragmentedMessage msg) {
    528556            _msg = msg;
    529557        }
     558
    530559        public void timeReached() {
    531560            boolean removed = false;
  • router/java/src/net/i2p/router/tunnel/FragmentedMessage.java

    r080f435 re7898b5  
    11package net.i2p.router.tunnel;
    22
    3 import java.io.IOException;
    4 import java.io.InputStream;
    5 import java.io.OutputStream;
    6 
    73import net.i2p.I2PAppContext;
    8 import net.i2p.data.Base64;
    94import net.i2p.data.ByteArray;
    105import net.i2p.data.DataHelper;
    116import net.i2p.data.Hash;
    127import net.i2p.data.TunnelId;
    13 import net.i2p.data.i2np.DataMessage;
    14 import net.i2p.data.i2np.I2NPMessage;
    15 import net.i2p.data.i2np.I2NPMessageHandler;
    168import net.i2p.util.ByteCache;
    179import net.i2p.util.Log;
     
    3123    private Hash _toRouter;
    3224    private TunnelId _toTunnel;
    33     private ByteArray _fragments[];
     25    private final ByteArray _fragments[];
    3426    private boolean _lastReceived;
    3527    private int _highFragmentNum;
     
    9486        ba.setOffset(0);
    9587        //System.arraycopy(payload, offset, ba.getData(), 0, length);
    96         if (_log.shouldLog(Log.DEBUG))
    97             _log.debug("fragment[" + fragmentNum + "/" + offset + "/" + length + "]: "
    98                        + Base64.encode(ba.getData(), ba.getOffset(), ba.getValid()));
     88        //if (_log.shouldLog(Log.DEBUG))
     89        //    _log.debug("fragment[" + fragmentNum + "/" + offset + "/" + length + "]: "
     90        //               + Base64.encode(ba.getData(), ba.getOffset(), ba.getValid()));
    9991
    10092        _fragments[fragmentNum] = ba;
     
    146138        ba.setOffset(0);
    147139        //System.arraycopy(payload, offset, ba.getData(), 0, length);
    148         if (_log.shouldLog(Log.DEBUG))
    149             _log.debug("fragment[0/" + offset + "/" + length + "]: "
    150                        + Base64.encode(ba.getData(), ba.getOffset(), ba.getValid()));
     140        //if (_log.shouldLog(Log.DEBUG))
     141        //    _log.debug("fragment[0/" + offset + "/" + length + "]: "
     142        //               + Base64.encode(ba.getData(), ba.getOffset(), ba.getValid()));
    151143        _fragments[0] = ba;
    152144        _lastReceived = _lastReceived || isLast;
     
    161153    public Hash getTargetRouter() { return _toRouter; }
    162154    public TunnelId getTargetTunnel() { return _toTunnel; }
     155
    163156    public int getFragmentCount() {
    164157        int found = 0;
     
    205198   
    206199   
     200/****
    207201    public void writeComplete(OutputStream out) throws IOException {
    208202        if (_releasedAfter > 0) {
     
    217211        _completed = true;
    218212    }
    219     public void writeComplete(byte target[], int offset) {
     213****/
     214
     215    /** */
     216    private void writeComplete(byte target[], int offset) {
    220217        if (_releasedAfter > 0) {
    221218             RuntimeException e = new RuntimeException("use after free in FragmentedMessage");
     
    230227        _completed = true;
    231228    }
     229
    232230    public byte[] toByteArray() {
    233231        synchronized (this) {
Note: See TracChangeset for help on using the changeset viewer.