/*
    SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef UTP_REMOTEWINDOW_H
#define UTP_REMOTEWINDOW_H

#include <QList>
#include <QMutex>
#include <QSharedPointer>
#include <ktorrent_export.h>
#include <util/constants.h>
#include <utp/packetbuffer.h>
#include <utp/timevalue.h>

namespace utp
{
struct SelectiveAck;
struct Header;

/*!
 * \headerfile utp/remotewindow.h
 * \brief A packet that we have sent that has not yet been acked by the remote window.
 */
struct UnackedPacket {
    UnackedPacket(const PacketBuffer &packet, bt::Uint16 seq_nr, bt::TimeStamp send_time);
    ~UnackedPacket();

    PacketBuffer packet;
    bt::Uint16 seq_nr;
    bt::TimeStamp send_time;
    bool retransmitted;
};

/*!
 * \headerfile utp/remotewindow.h
 * \brief An interface class to retransmit packets.
 */
class KTORRENT_EXPORT Retransmitter
{
public:
    virtual ~Retransmitter()
    {
    }

    //! Update the RTT time
    virtual void updateRTT(const Header *hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size) = 0;

    //! Retransmit a packet
    virtual void retransmit(PacketBuffer &packet, bt::Uint16 p_seq_nr) = 0;

    //! Get the current timeout
    [[nodiscard]] virtual bt::Uint32 currentTimeout() const = 0;
};

/*!
 * \headerfile utp/remotewindow.h
 * \brief Keeps track of the remote sides window including all packets in-flight.
 *
 * An in-flight packet is one which has been sent but not yet acked.
 */
class KTORRENT_EXPORT RemoteWindow
{
public:
    RemoteWindow();
    virtual ~RemoteWindow();

    /*!
     * A packet was received (update window size and check for acks)
     *
     * Returns the proportion of bytes in the window that were acked by this packet.
     */
    double packetReceived(const Header *hdr, const SelectiveAck *sack, Retransmitter *conn);

    //! Add a packet to the remote window (should include headers)
    void addPacket(const PacketBuffer &packet, bt::Uint16 seq_nr, bt::TimeStamp send_time);

    //! Are we allowed to send
    [[nodiscard]] bool allowedToSend(bt::Uint32 packet_size) const
    {
        return cur_window + packet_size <= qMin(wnd_size, max_window);
    }

    //! Calculates how much window space is availabe
    [[nodiscard]] bt::Uint32 availableSpace() const
    {
        const bt::Uint32 m = qMin(wnd_size, max_window);
        if (cur_window > m) {
            return 0;
        } else {
            return m - cur_window;
        }
    }

    //! See if all packets are acked
    [[nodiscard]] bool allPacketsAcked() const
    {
        return unacked_packets.isEmpty();
    }

    //! Get the number of unacked packets
    [[nodiscard]] bt::Uint32 numUnackedPackets() const
    {
        return unacked_packets.count();
    }

    //! A timeout occurred
    void timeout(Retransmitter *conn);

    //! Update the window size
    void updateWindowSize(double scaled_gain);

    /*!
     * Returns the number of bytes in-flight.
     */
    [[nodiscard]] bt::Uint32 currentWindow() const
    {
        return cur_window;
    }

    /*!
     * Returns the maximum number of bytes that we allow to be in-flight.
     */
    [[nodiscard]] bt::Uint32 maxWindow() const
    {
        return max_window;
    }

    /*!
     * Returns the maximum number of bytes that the remote will allow to be in-flight.
     */
    [[nodiscard]] bt::Uint32 windowSize() const
    {
        return wnd_size;
    }

    //! Clear the window
    void clear();

private:
    void checkLostPackets(const Header *hdr, const SelectiveAck *sack, Retransmitter *conn);
    bt::Uint16 lost(const SelectiveAck *sack);

private:
    bt::Uint32 cur_window;
    bt::Uint32 max_window;
    bt::Uint32 wnd_size; // advertised window size from the other side
    QList<UnackedPacket> unacked_packets;
    bt::Uint16 last_ack_nr;
    bt::Uint32 last_ack_receive_count;
};

}

#endif // UTP_REMOTEWINDOW_H
