mirror of
https://github.com/Green-Sky/tox_ngc_ft1.git
synced 2025-07-14 22:06:45 +02:00
Compare commits
4 Commits
95bfa2473c
...
master
Author | SHA1 | Date | |
---|---|---|---|
08a083f346 | |||
2a6dca69c7 | |||
27aeb03a61 | |||
304aae05c2 |
42
ledbat.cpp
42
ledbat.cpp
@ -5,6 +5,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <tuple>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
@ -15,26 +16,26 @@
|
|||||||
|
|
||||||
inline constexpr bool PLOTTING = false;
|
inline constexpr bool PLOTTING = false;
|
||||||
|
|
||||||
LEDBAT::LEDBAT(void) {
|
LEDBAT::LEDBAT(size_t maximum_segment_data_size) : MAXIMUM_SEGMENT_DATA_SIZE(maximum_segment_data_size) {
|
||||||
_time_start_offset = clock::now();
|
_time_start_offset = clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t LEDBAT::canSend(void) const {
|
size_t LEDBAT::canSend(void) const {
|
||||||
if (_in_flight.empty()) {
|
if (_in_flight.empty()) {
|
||||||
return 496u;
|
return MAXIMUM_SEGMENT_DATA_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int64_t cspace = _cwnd - _in_flight_bytes;
|
const int64_t cspace = _cwnd - _in_flight_bytes;
|
||||||
if (cspace < 496) {
|
if (cspace < MAXIMUM_SEGMENT_DATA_SIZE) {
|
||||||
return 0u;
|
return 0u;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int64_t fspace = _fwnd - _in_flight_bytes;
|
const int64_t fspace = _fwnd - _in_flight_bytes;
|
||||||
if (fspace < 496) {
|
if (fspace < MAXIMUM_SEGMENT_DATA_SIZE) {
|
||||||
return 0u;
|
return 0u;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t space = std::ceil(std::min(cspace, fspace) / 496.f) * 496.f;
|
size_t space = std::ceil(std::min<float>(cspace, fspace) / MAXIMUM_SEGMENT_DATA_SIZE) * MAXIMUM_SEGMENT_DATA_SIZE;
|
||||||
|
|
||||||
return space;
|
return space;
|
||||||
}
|
}
|
||||||
@ -56,14 +57,15 @@ std::vector<LEDBAT::SeqIDType> LEDBAT::getTimeouts(void) const {
|
|||||||
|
|
||||||
|
|
||||||
void LEDBAT::onSent(SeqIDType seq, size_t data_size) {
|
void LEDBAT::onSent(SeqIDType seq, size_t data_size) {
|
||||||
if (false) {
|
if (true) {
|
||||||
for (const auto& it : _in_flight) {
|
for (const auto& it : _in_flight) {
|
||||||
assert(std::get<0>(it) != seq);
|
assert(std::get<0>(it) != seq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_in_flight.push_back({seq, getTimeNow(), data_size + segment_overhead});
|
|
||||||
_in_flight_bytes += data_size + segment_overhead;
|
_in_flight.push_back({seq, getTimeNow(), data_size + SEGMENT_OVERHEAD});
|
||||||
_recently_sent_bytes += data_size + segment_overhead;
|
_in_flight_bytes += data_size + SEGMENT_OVERHEAD;
|
||||||
|
_recently_sent_bytes += data_size + SEGMENT_OVERHEAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LEDBAT::onAck(std::vector<SeqIDType> seqs) {
|
void LEDBAT::onAck(std::vector<SeqIDType> seqs) {
|
||||||
@ -88,7 +90,7 @@ void LEDBAT::onAck(std::vector<SeqIDType> seqs) {
|
|||||||
most_recent = std::max(most_recent, std::get<1>(*it));
|
most_recent = std::max(most_recent, std::get<1>(*it));
|
||||||
_in_flight_bytes -= std::get<2>(*it);
|
_in_flight_bytes -= std::get<2>(*it);
|
||||||
_recently_acked_data += std::get<2>(*it);
|
_recently_acked_data += std::get<2>(*it);
|
||||||
assert(_in_flight_bytes >= 0);
|
assert(_in_flight_bytes >= 0); // TODO: this triggers
|
||||||
_in_flight.erase(it);
|
_in_flight.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,6 +104,7 @@ void LEDBAT::onAck(std::vector<SeqIDType> seqs) {
|
|||||||
|
|
||||||
void LEDBAT::onLoss(SeqIDType seq, bool discard) {
|
void LEDBAT::onLoss(SeqIDType seq, bool discard) {
|
||||||
auto it = std::find_if(_in_flight.begin(), _in_flight.end(), [seq](const auto& v) -> bool {
|
auto it = std::find_if(_in_flight.begin(), _in_flight.end(), [seq](const auto& v) -> bool {
|
||||||
|
assert(!std::isnan(std::get<1>(v)));
|
||||||
return std::get<0>(v) == seq;
|
return std::get<0>(v) == seq;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -122,7 +125,9 @@ void LEDBAT::onLoss(SeqIDType seq, bool discard) {
|
|||||||
if (discard) {
|
if (discard) {
|
||||||
_in_flight_bytes -= std::get<2>(*it);
|
_in_flight_bytes -= std::get<2>(*it);
|
||||||
assert(_in_flight_bytes >= 0);
|
assert(_in_flight_bytes >= 0);
|
||||||
|
_in_flight.erase(it);
|
||||||
}
|
}
|
||||||
|
// TODO: reset timestamp?
|
||||||
|
|
||||||
updateWindows();
|
updateWindows();
|
||||||
}
|
}
|
||||||
@ -187,19 +192,19 @@ void LEDBAT::updateWindows(void) {
|
|||||||
if (now - _last_cwnd >= current_delay) {
|
if (now - _last_cwnd >= current_delay) {
|
||||||
const float queuing_delay {current_delay - _base_delay};
|
const float queuing_delay {current_delay - _base_delay};
|
||||||
|
|
||||||
_fwnd = max_byterate_allowed * getCurrentDelay();
|
_fwnd = max_byterate_allowed * current_delay;
|
||||||
_fwnd *= 1.3f; // try do balance conservative algo a bit, current_delay
|
_fwnd *= 1.3f; // try do balance conservative algo a bit, current_delay
|
||||||
|
|
||||||
//const float gain {1}; // TODO: move and increase
|
|
||||||
float gain {1.f / std::min(16.f, std::ceil(2.f*target_delay/_base_delay))};
|
float gain {1.f / std::min(16.f, std::ceil(2.f*target_delay/_base_delay))};
|
||||||
//gain *= 400.f; // from packets to bytes ~
|
//gain *= 400.f; // from packets to bytes ~
|
||||||
gain *= _recently_acked_data/10.f; // from packets to bytes ~
|
gain *= _recently_acked_data/5.f; // from packets to bytes ~
|
||||||
//gain *= 0.1f;
|
//gain *= 0.1f;
|
||||||
|
|
||||||
if (_recently_lost_data) {
|
if (_recently_lost_data) {
|
||||||
_cwnd = std::clamp(
|
_cwnd = std::clamp(
|
||||||
_cwnd / 2.f,
|
_cwnd / 2.f,
|
||||||
2.f * maximum_segment_size,
|
//_cwnd / 1.6f,
|
||||||
|
2.f * MAXIMUM_SEGMENT_SIZE,
|
||||||
_cwnd
|
_cwnd
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@ -207,23 +212,22 @@ void LEDBAT::updateWindows(void) {
|
|||||||
// "Multiplicative decrease"
|
// "Multiplicative decrease"
|
||||||
const float constant {2.f}; // spec recs 1
|
const float constant {2.f}; // spec recs 1
|
||||||
if (queuing_delay < target_delay) {
|
if (queuing_delay < target_delay) {
|
||||||
_cwnd += gain;
|
|
||||||
_cwnd = std::min(
|
_cwnd = std::min(
|
||||||
_cwnd + gain,
|
_cwnd + gain,
|
||||||
_fwnd
|
_fwnd
|
||||||
);
|
);
|
||||||
} else if (queuing_delay > target_delay) {
|
} else if (queuing_delay > target_delay) {
|
||||||
_cwnd = std::clamp(
|
_cwnd = std::clamp(
|
||||||
_cwnd + std::max( // TODO: where to put bytes_newly_acked
|
_cwnd + std::max(
|
||||||
gain - constant * _cwnd * (queuing_delay / target_delay - 1.f),
|
gain - constant * _cwnd * (queuing_delay / target_delay - 1.f),
|
||||||
-_cwnd/2.f // at most halve
|
-_cwnd/2.f // at most halve
|
||||||
),
|
),
|
||||||
|
|
||||||
// never drop below 2 "packets" in flight
|
// never drop below 2 "packets" in flight
|
||||||
//2.f * maximum_segment_size,
|
2.f * MAXIMUM_SEGMENT_SIZE,
|
||||||
2.f * 496,
|
|
||||||
|
|
||||||
current_delay * max_byterate_allowed // cap rate
|
// cap rate
|
||||||
|
_fwnd
|
||||||
);
|
);
|
||||||
} // no else, we on point. very unlikely with float
|
} // no else, we on point. very unlikely with float
|
||||||
}
|
}
|
||||||
|
18
ledbat.hpp
18
ledbat.hpp
@ -18,15 +18,20 @@ struct LEDBAT {
|
|||||||
static constexpr size_t UDP_HEADER_SIZE {8};
|
static constexpr size_t UDP_HEADER_SIZE {8};
|
||||||
|
|
||||||
// TODO: tcp AND IPv6 will be different
|
// TODO: tcp AND IPv6 will be different
|
||||||
static constexpr size_t segment_overhead {
|
static constexpr size_t SEGMENT_OVERHEAD {
|
||||||
4+ // ft overhead
|
4+ // ft overhead
|
||||||
46+ // tox?
|
46+ // tox?
|
||||||
UDP_HEADER_SIZE+
|
UDP_HEADER_SIZE+
|
||||||
IPV4_HEADER_SIZE
|
IPV4_HEADER_SIZE
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr size_t maximum_segment_size {496 + segment_overhead}; // tox 500 - 4 from ft
|
// TODO: make configurable, set with tox ngc lossy packet size
|
||||||
static_assert(maximum_segment_size == 574); // mesured in wireshark
|
//const size_t MAXIMUM_SEGMENT_DATA_SIZE {1000-4};
|
||||||
|
const size_t MAXIMUM_SEGMENT_DATA_SIZE {500-4};
|
||||||
|
|
||||||
|
//static constexpr size_t maximum_segment_size {496 + segment_overhead}; // tox 500 - 4 from ft
|
||||||
|
const size_t MAXIMUM_SEGMENT_SIZE {MAXIMUM_SEGMENT_DATA_SIZE + SEGMENT_OVERHEAD}; // tox 500 - 4 from ft
|
||||||
|
//static_assert(maximum_segment_size == 574); // mesured in wireshark
|
||||||
|
|
||||||
// ledbat++ says 60ms, we might need other values if relayed
|
// ledbat++ says 60ms, we might need other values if relayed
|
||||||
//const float target_delay {0.060f};
|
//const float target_delay {0.060f};
|
||||||
@ -38,11 +43,10 @@ struct LEDBAT {
|
|||||||
|
|
||||||
//static constexpr size_t rtt_buffer_size_max {2000};
|
//static constexpr size_t rtt_buffer_size_max {2000};
|
||||||
|
|
||||||
//float max_byterate_allowed {10*1024*1024}; // 10MiB/s
|
float max_byterate_allowed {10*1024*1024}; // 10MiB/s
|
||||||
float max_byterate_allowed {1*1024*1024};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LEDBAT(void);
|
LEDBAT(size_t maximum_segment_data_size);
|
||||||
|
|
||||||
// return the current believed window in bytes of how much data can be inflight,
|
// return the current believed window in bytes of how much data can be inflight,
|
||||||
// without overstepping the delay requirement
|
// without overstepping the delay requirement
|
||||||
@ -87,7 +91,7 @@ struct LEDBAT {
|
|||||||
private: // state
|
private: // state
|
||||||
//float _cto {2.f}; // congestion timeout value in seconds
|
//float _cto {2.f}; // congestion timeout value in seconds
|
||||||
|
|
||||||
float _cwnd {2.f * maximum_segment_size}; // in bytes
|
float _cwnd {2.f * MAXIMUM_SEGMENT_SIZE}; // in bytes
|
||||||
float _base_delay {2.f}; // lowest mesured delay in _rtt_buffer in seconds
|
float _base_delay {2.f}; // lowest mesured delay in _rtt_buffer in seconds
|
||||||
|
|
||||||
float _last_cwnd {0.f}; // timepoint of last cwnd correction
|
float _last_cwnd {0.f}; // timepoint of last cwnd correction
|
||||||
|
10
ngc_ft1.cpp
10
ngc_ft1.cpp
@ -119,7 +119,7 @@ struct NGC_FT1 {
|
|||||||
|
|
||||||
struct Group {
|
struct Group {
|
||||||
struct Peer {
|
struct Peer {
|
||||||
LEDBAT cca;
|
LEDBAT cca{500-4}; // TODO: replace with tox_group_max_custom_lossy_packet_length()-4
|
||||||
|
|
||||||
struct RecvTransfer {
|
struct RecvTransfer {
|
||||||
uint32_t file_kind;
|
uint32_t file_kind;
|
||||||
@ -277,7 +277,7 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
|
|||||||
|
|
||||||
// if chunks in flight < window size (2)
|
// if chunks in flight < window size (2)
|
||||||
//while (tf.ssb.size() < ngc_ft1_ctx->options.packet_window_size) {
|
//while (tf.ssb.size() < ngc_ft1_ctx->options.packet_window_size) {
|
||||||
int64_t can_packet_size {peer.cca.canSend()};
|
int64_t can_packet_size {static_cast<int64_t>(peer.cca.canSend())};
|
||||||
//if (can_packet_size) {
|
//if (can_packet_size) {
|
||||||
//std::cerr << "FT: can_packet_size: " << can_packet_size;
|
//std::cerr << "FT: can_packet_size: " << can_packet_size;
|
||||||
//}
|
//}
|
||||||
@ -289,8 +289,10 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
|
|||||||
//size_t chunk_size = std::min<size_t>(496u, tf.file_size - tf.file_size_current);
|
//size_t chunk_size = std::min<size_t>(496u, tf.file_size - tf.file_size_current);
|
||||||
//size_t chunk_size = std::min<size_t>(can_packet_size, tf.file_size - tf.file_size_current);
|
//size_t chunk_size = std::min<size_t>(can_packet_size, tf.file_size - tf.file_size_current);
|
||||||
size_t chunk_size = std::min<size_t>({
|
size_t chunk_size = std::min<size_t>({
|
||||||
496u,
|
//496u,
|
||||||
can_packet_size,
|
//996u,
|
||||||
|
peer.cca.MAXIMUM_SEGMENT_DATA_SIZE,
|
||||||
|
static_cast<size_t>(can_packet_size),
|
||||||
tf.file_size - tf.file_size_current
|
tf.file_size - tf.file_size_current
|
||||||
});
|
});
|
||||||
if (chunk_size == 0) {
|
if (chunk_size == 0) {
|
||||||
|
Reference in New Issue
Block a user