Class MessageInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    class MessageInputStream
    extends InputStream
    Stream that can be given messages out of order yet present them in order.

    I2PSession -> MessageHandler -> PacketHandler -> ConnectionPacketHandler -> MessageInputStream

    This buffers unlimited data via messageReceived() - limiting / blocking is done in ConnectionPacketHandler.receivePacket().

    • Constructor Detail

      • MessageInputStream

        public MessageInputStream​(I2PAppContext ctx,
                                  int maxMessageSize,
                                  int maxWindowSize,
                                  int maxBufferSize)
    • Method Detail

      • getHighestReadyBlockId

        public long getHighestReadyBlockId()
        What is the highest block ID we've completely received through?
        Returns:
        highest data block ID completely received or -1 for none
      • getHighestBlockId

        public long getHighestBlockId()
        Returns:
        highest data block ID received or -1 for none
      • isLocallyClosed

        public boolean isLocallyClosed()
        Returns:
        true if this has been closed on the read side with close()
      • canAccept

        public boolean canAccept​(long messageId,
                                 int payloadSize)
        Determine if this packet will fit in our buffering limits. Always returns true for zero payloadSize and dups, even if locally closed. Returns false if there is no room, OR it's not a dup and the stream has been closed on the read side with close(). If this returns false, you probably want to call isLocallyClosed() to find out why.
        Returns:
        true if we have room. If false, do not call messageReceived()
        Since:
        0.9.20 moved from ConnectionPacketHandler.receivePacket() so it can all be under one lock, and we can efficiently do several checks
      • getNacks

        public long[] getNacks()
        Retrieve the message IDs that are holes in our sequence - ones past the highest ready ID and below the highest received message ID. This may return null if there are no such IDs.
        Returns:
        array of message ID holes, or null if none
      • updateAcks

        public void updateAcks​(PacketLocal packet)
        Adds the ack-through and nack fields to a packet we are building for transmission
      • getReadTimeout

        public int getReadTimeout()
        how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)
        Returns:
        how long read calls should block, 0 for nonblocking, negative to indefinitely block
      • setReadTimeout

        public void setReadTimeout​(int timeout)
        how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)
        Parameters:
        timeout - how long read calls should block, 0 for nonblocking, negative to indefinitely block
      • closeReceived

        public void closeReceived()
        There is no more data coming from the I2P side. Does NOT clear pending data. messageReceived() MUST have been called previously with the messageId of the CLOSE packet.
      • notifyActivity

        public void notifyActivity()
      • messageReceived

        public boolean messageReceived​(long messageId,
                                       ByteArray payload)
        A new message has arrived - toss it on the appropriate queue (moving previously pending messages to the ready queue if it fills the gap, etc). This does no limiting of pending data - see canAccept() for limiting. Warning - returns true if locally closed.
        Parameters:
        messageId - ID of the message
        payload - message payload, may be null or have null or zero-length data
        Returns:
        true if this is a new packet, false if it is a dup
      • read

        public int read()
                 throws IOException
        On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned -1.
        Specified by:
        read in class InputStream
        Throws:
        IOException
      • read

        public int read​(byte[] target)
                 throws IOException
        On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned 0.
        Overrides:
        read in class InputStream
        Throws:
        IOException
      • read

        public int read​(byte[] target,
                        int offset,
                        int length)
                 throws IOException
        On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned 0.
        Overrides:
        read in class InputStream
        Throws:
        IOException
      • getTotalReadySize

        public int getTotalReadySize()
        Same as available() but doesn't throw IOE
      • streamErrorOccurred

        void streamErrorOccurred​(IOException ioe)
        Stream b0rked, die with the given error