Changeset fef65c9


Ignore:
Timestamp:
Jun 8, 2015 10:18:14 PM (5 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
7b84676
Parents:
cbc2f89
Message:

Store the session in Packet, so we may more easily and efficiently
handle multisession, especially on the incoming side.
More refactoring to follow

Location:
apps/streaming/java/src/net/i2p/client/streaming/impl
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • apps/streaming/java/src/net/i2p/client/streaming/impl/Connection.java

    rcbc2f89 rfef65c9  
    311311        _resetSentOn.set(now);
    312312        if ( (_remotePeer == null) || (_sendStreamId.get() <= 0) ) return;
    313         PacketLocal reply = new PacketLocal(_context, _remotePeer);
     313        PacketLocal reply = new PacketLocal(_context, _remotePeer, this);
    314314        reply.setFlag(Packet.FLAG_RESET);
    315315        reply.setFlag(Packet.FLAG_SIGNATURE_INCLUDED);
     
    317317        reply.setReceiveStreamId(_receiveStreamId.get());
    318318        // TODO remove this someday, as of 0.9.20 we do not require it
    319         reply.setOptionalFrom(_connectionManager.getSession().getMyDestination());
     319        reply.setOptionalFrom();
    320320        reply.setLocalPort(_localPort);
    321321        reply.setRemotePort(_remotePort);
  • apps/streaming/java/src/net/i2p/client/streaming/impl/ConnectionDataReceiver.java

    rcbc2f89 rfef65c9  
    205205        if (isFirst) {
    206206            packet.setFlag(Packet.FLAG_SYNCHRONIZE);
    207             packet.setOptionalFrom(con.getSession().getMyDestination());
     207            packet.setOptionalFrom();
    208208            packet.setOptionalMaxSize(con.getOptions().getMaxMessageSize());
    209209        }
  • apps/streaming/java/src/net/i2p/client/streaming/impl/ConnectionHandler.java

    rcbc2f89 rfef65c9  
    244244            return;
    245245        }
    246         PacketLocal reply = new PacketLocal(_context, packet.getOptionalFrom());
     246        PacketLocal reply = new PacketLocal(_context, packet.getOptionalFrom(), packet.getSession());
    247247        reply.setFlag(Packet.FLAG_RESET);
    248248        reply.setFlag(Packet.FLAG_SIGNATURE_INCLUDED);
     
    251251        reply.setReceiveStreamId(0);
    252252        // TODO remove this someday, as of 0.9.20 we do not require it
    253         reply.setOptionalFrom(_manager.getSession().getMyDestination());
     253        reply.setOptionalFrom();
    254254        if (_log.shouldLog(Log.DEBUG))
    255255            _log.debug("Sending RST: " + reply + " because of " + packet);
     
    293293
    294294        public PoisonPacket() {
     295            super(null);
    295296            setOptionalDelay(POISON_MAX_DELAY_REQUEST);
    296297        }
  • apps/streaming/java/src/net/i2p/client/streaming/impl/ConnectionManager.java

    rcbc2f89 rfef65c9  
    271271                }
    272272            }
    273             PacketLocal reply = new PacketLocal(_context, from);
     273            PacketLocal reply = new PacketLocal(_context, from, synPacket.getSession());
    274274            reply.setFlag(Packet.FLAG_RESET);
    275275            reply.setFlag(Packet.FLAG_SIGNATURE_INCLUDED);
     
    277277            reply.setSendStreamId(synPacket.getReceiveStreamId());
    278278            reply.setReceiveStreamId(0);
    279             reply.setOptionalFrom(_session.getMyDestination());
     279            reply.setOptionalFrom();
    280280            reply.setLocalPort(synPacket.getLocalPort());
    281281            reply.setRemotePort(synPacket.getRemotePort());
     
    330330            }
    331331        }
    332         PacketLocal pong = new PacketLocal(_context, dest);
     332        PacketLocal pong = new PacketLocal(_context, dest, ping.getSession());
    333333        pong.setFlag(Packet.FLAG_ECHO | Packet.FLAG_NO_ACK);
    334334        pong.setReceiveStreamId(ping.getSendStreamId());
     
    735735        PingRequest req = new PingRequest(notifier);
    736736        long id = assignPingId(req);
    737         PacketLocal packet = new PacketLocal(_context, peer);
     737        PacketLocal packet = new PacketLocal(_context, peer, _session);
    738738        packet.setSendStreamId(id);
    739739        packet.setFlag(Packet.FLAG_ECHO |
    740740                       Packet.FLAG_NO_ACK |
    741741                       Packet.FLAG_SIGNATURE_INCLUDED);
    742         packet.setOptionalFrom(_session.getMyDestination());
     742        packet.setOptionalFrom();
    743743        packet.setLocalPort(fromPort);
    744744        packet.setRemotePort(toPort);
     
    781781        PingRequest req = new PingRequest(null);
    782782        long id = assignPingId(req);
    783         PacketLocal packet = new PacketLocal(_context, peer);
     783        PacketLocal packet = new PacketLocal(_context, peer, _session);
    784784        packet.setSendStreamId(id);
    785785        packet.setFlag(Packet.FLAG_ECHO |
    786786                       Packet.FLAG_NO_ACK |
    787787                       Packet.FLAG_SIGNATURE_INCLUDED);
    788         packet.setOptionalFrom(_session.getMyDestination());
     788        packet.setOptionalFrom();
    789789        packet.setLocalPort(fromPort);
    790790        packet.setRemotePort(toPort);
  • apps/streaming/java/src/net/i2p/client/streaming/impl/MessageHandler.java

    rcbc2f89 rfef65c9  
    7171                       " proto: " + proto +
    7272                       " fromPort: " + fromPort + " toPort: " + toPort);
    73         Packet packet = new Packet();
     73        Packet packet = new Packet(session);
    7474        try {
    7575            packet.readPacket(data, 0, data.length);
  • apps/streaming/java/src/net/i2p/client/streaming/impl/Packet.java

    rcbc2f89 rfef65c9  
    66
    77import net.i2p.I2PAppContext;
     8import net.i2p.client.I2PSession;
    89import net.i2p.crypto.SigType;
    910import net.i2p.data.Base64;
     
    6869 */
    6970class Packet {
     71    protected final I2PSession _session;
    7072    private long _sendStreamId;
    7173    private long _receiveStreamId;
     
    168170     *  See readPacket() for inbound packets, and the setters for outbound packets.
    169171     */
    170     public Packet() { }
    171    
     172    public Packet(I2PSession session) {
     173        _session = session;
     174    }
     175   
     176    /** @since 0.9.21 */
     177    public I2PSession getSession() {
     178        return _session;
     179    }
     180
    172181    private boolean _sendStreamIdSet = false;
    173182
     
    335344    public Destination getOptionalFrom() { return _optionFrom; }
    336345
    337     public void setOptionalFrom(Destination from) {
    338         setFlag(FLAG_FROM_INCLUDED, from != null);
    339         if (from == null) throw new RuntimeException("from is null!?");
    340         _optionFrom = from;
     346    /**
     347     * This sets the from field in the packet to the Destination for the session
     348     * provided in the constructor.
     349     */
     350    public void setOptionalFrom() {
     351        setFlag(FLAG_FROM_INCLUDED, true);
     352        _optionFrom = _session.getMyDestination();
    341353    }
    342354   
     
    509521    }
    510522   
    511    
    512523    /**
    513524     * how large would this packet be if we wrote it
    514525     * @return How large the current packet would be
     526     *
    515527     * @throws IllegalStateException
    516528     */
     
    547559        return size;
    548560    }
     561   
     562
    549563    /**
    550564     * Read the packet from the buffer (starting at the offset) and return
     
    620634                Destination optionFrom = Destination.create(bais);
    621635                cur += optionFrom.size();
    622                 setOptionalFrom(optionFrom);
     636                _optionFrom = optionFrom;
    623637            } catch (IOException ioe) {
    624638                throw new IllegalArgumentException("Bad from field", ioe);
  • apps/streaming/java/src/net/i2p/client/streaming/impl/PacketHandler.java

    rcbc2f89 rfef65c9  
    232232     *  If the packet has no 'optional from' or valid signature, this does nothing.
    233233     *  This is not associated with a connection, so no con stats are updated.
     234     *
     235     *  @param packet incoming packet to be replied to
    234236     */
    235237    private void sendReset(Packet packet) {
     
    243245            return;
    244246        }
    245         PacketLocal reply = new PacketLocal(_context, from);
     247        PacketLocal reply = new PacketLocal(_context, from, packet.getSession());
    246248        reply.setFlag(Packet.FLAG_RESET);
    247249        reply.setFlag(Packet.FLAG_SIGNATURE_INCLUDED);
     
    249251        reply.setReceiveStreamId(packet.getSendStreamId());
    250252        // TODO remove this someday, as of 0.9.20 we do not require it
    251         reply.setOptionalFrom(_manager.getSession().getMyDestination());
     253        reply.setOptionalFrom();
    252254        reply.setLocalPort(packet.getLocalPort());
    253255        reply.setRemotePort(packet.getRemotePort());
  • apps/streaming/java/src/net/i2p/client/streaming/impl/PacketLocal.java

    rcbc2f89 rfef65c9  
    77
    88import net.i2p.I2PAppContext;
     9import net.i2p.client.I2PSession;
    910import net.i2p.data.Destination;
    1011import net.i2p.data.SessionKey;
     
    3839   
    3940    /** not bound to a connection */
    40     public PacketLocal(I2PAppContext ctx, Destination to) {
    41         this(ctx, to, null);
    42     }
    43 
     41    public PacketLocal(I2PAppContext ctx, Destination to, I2PSession session) {
     42        super(session);
     43        _context = ctx;
     44        _createdOn = ctx.clock().now();
     45        _log = ctx.logManager().getLog(PacketLocal.class);
     46        _to = to;
     47        _connection = null;
     48        _lastSend = -1;
     49        _cancelledOn = -1;
     50    }
     51
     52    /** bound to a connection */
    4453    public PacketLocal(I2PAppContext ctx, Destination to, Connection con) {
     54        super(con.getSession());
    4555        _context = ctx;
    4656        _createdOn = ctx.clock().now();
     
    204214     * @since 0.9.20 moved from Packet
    205215     */
    206     public int writeSignedPacket(byte buffer[], int offset, I2PAppContext ctx, SigningPrivateKey key) throws IllegalStateException {
     216    public int writeSignedPacket(byte buffer[], int offset) throws IllegalStateException {
    207217        setFlag(FLAG_SIGNATURE_INCLUDED);
     218        SigningPrivateKey key = _session.getPrivateKey();
    208219        int size = writePacket(buffer, offset, key.getType().getSigLen());
    209         _optionSignature = ctx.dsa().sign(buffer, offset, size, key);
     220        _optionSignature = _context.dsa().sign(buffer, offset, size, key);
    210221        //if (false) {
    211222        //    Log l = ctx.logManager().getLog(Packet.class);
     
    259270            if (con != null) {
    260271                buf.append(" from ");
    261                 Destination local = con.getSession().getMyDestination();
     272                Destination local = _session.getMyDestination();
    262273                if (local != null)
    263274                    buf.append(local.calculateHash().toBase64().substring(0,4));
     
    276287        return buf;
    277288    }
    278    
     289
    279290    ////// begin WriteStatus methods
    280291
  • apps/streaming/java/src/net/i2p/client/streaming/impl/PacketQueue.java

    rcbc2f89 rfef65c9  
    100100            long beforeWrite = System.currentTimeMillis();
    101101            if (packet.shouldSign())
    102                 size = packet.writeSignedPacket(buf, 0, _context, _session.getPrivateKey());
     102                size = packet.writeSignedPacket(buf, 0);
    103103            else
    104104                size = packet.writePacket(buf, 0);
     
    198198            packet.incrementSends();
    199199            Connection c = packet.getConnection();
    200             if (c != null) {
     200            if (c != null && _log.shouldDebug()) {
    201201                String suffix = "wsize " + c.getOptions().getWindowSize() + " rto " + c.getOptions().getRTO();
    202202                c.getConnectionManager().getPacketHandler().displayPacket(packet, "SEND", suffix);
Note: See TracChangeset for help on using the changeset viewer.