From 20c41e1a023c0b631ec0764073c9902af664c506 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 19 Jan 2023 23:33:35 +0100 Subject: [PATCH] tweak defaults, better peer connection state tracking (none :P) --- src/command_line.hpp | 8 ++++---- src/states/receive_start_sha1.cpp | 13 ++++++++----- src/states/sha1.cpp | 25 +++++++++++++++++++++---- src/states/sha1.hpp | 11 +++++------ src/tox_client.cpp | 22 +++++++++++++--------- src/tox_client.hpp | 10 ++++------ 6 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/command_line.hpp b/src/command_line.hpp index 6366148..1c92a4f 100644 --- a/src/command_line.hpp +++ b/src/command_line.hpp @@ -57,15 +57,15 @@ struct CommandLine { // advanced FT1: // --ft_ack_per_packet - size_t ft_acks_per_packet {5}; + size_t ft_acks_per_packet {3}; // --ft_init_retry_timeout_after - float ft_init_retry_timeout_after {10.f}; + float ft_init_retry_timeout_after {5.f}; // --ft_sending_resend_without_ack_after - float ft_sending_resend_without_ack_after {5.f}; + float ft_sending_resend_without_ack_after {3.f}; // --ft_sending_give_up_after float ft_sending_give_up_after {30.f}; // --ft_packet_window_size - size_t ft_packet_window_size {5}; + size_t ft_packet_window_size {8}; // ---- TODO ---- diff --git a/src/states/receive_start_sha1.cpp b/src/states/receive_start_sha1.cpp index 685b6c4..10c0fa6 100644 --- a/src/states/receive_start_sha1.cpp +++ b/src/states/receive_start_sha1.cpp @@ -47,22 +47,25 @@ bool ReceiveStartSHA1::iterate(float delta) { } } else if (_time_since_last_request >= 15.f) { // blast ever 15sec _time_since_last_request = 0.f; - // TODO: select random and try, not blas - // ... and we are blasing + _tcl.forEachGroup([this](const uint32_t group_number) { - _tcl.forEachGroupPeer(group_number, [this, group_number](uint32_t peer_number, Tox_Connection connection_status) { + _tcl.forEachGroupPeer(group_number, [this, group_number](uint32_t peer_number) { _tcl.sendFT1RequestPrivate( group_number, peer_number, NGC_FT1_file_kind::HASH_SHA1_INFO, _sha1_info_hash.data.data(), _sha1_info_hash.size() ); - std::cout << "ReceiveStartSHA1 sendig info request to " << group_number << ":" << peer_number << " over " << (connection_status == Tox_Connection::TOX_CONNECTION_TCP ? "tcp" : "udp") <<"\n"; + std::cout + << "ReceiveStartSHA1 sendig info request to " + << group_number << ":" << peer_number + << " over " << (_tcl.getGroupPeerConnectionStatus(group_number, peer_number) == Tox_Connection::TOX_CONNECTION_TCP ? "tcp" : "udp") + << "\n" + ; }); }); } // if not transfer, request from random peer (equal dist!!) - // TODO: return true if done return _done; } diff --git a/src/states/sha1.cpp b/src/states/sha1.cpp index 12c5dc3..7bbab29 100644 --- a/src/states/sha1.cpp +++ b/src/states/sha1.cpp @@ -30,6 +30,7 @@ SHA1::SHA1( { assert(_have_chunk.size() == _sha1_info.chunks.size()); + _udp_only = cl.request_only_from_udp_peer; _max_concurrent_in = cl.max_incoming_transfers; _max_concurrent_out = cl.max_incoming_transfers; @@ -194,12 +195,21 @@ bool SHA1::iterate(float delta) { _peer_speed_mesurement_interval_timer = 0.f; // we lose some time here, but precision is not the issue _peer_in_bytes_array_index = (_peer_in_bytes_array_index + 1) % _peer_speed_mesurement_interval_count; - for (const auto& [peer, array] : _peer_in_bytes_array) { + //for (const auto& [peer, array] : _peer_in_bytes_array) { + for (auto it = _peer_in_bytes_array.begin(); it != _peer_in_bytes_array.end();) { + const auto& [peer, array] = *it; + float avg {0.f}; for (size_t i = 0; i < array.size(); i++) { avg += array[i]; } + if (avg == 0.f || _tcl.getGroupPeerConnectionStatus(peer.first, peer.second) == Tox_Connection::TOX_CONNECTION_NONE) { + _peer_in_speed.erase(peer); + it = _peer_in_bytes_array.erase(it); + continue; + } + // if 6 mesurment every 0.5sec -> avg is over 3sec -> /3 for /s avg /= _peer_speed_mesurement_interval * _peer_speed_mesurement_interval_count; @@ -207,12 +217,14 @@ bool SHA1::iterate(float delta) { _peer_in_bytes_array[peer][_peer_in_bytes_array_index] = 0; _peer_in_speed[peer] = avg; + + it++; } _peer_in_targets.clear(); _tcl.forEachGroup([this](uint32_t group_number) { - _tcl.forEachGroupPeer(group_number, [group_number, this](uint32_t peer_number, Tox_Connection connection_status) { - if (connection_status == Tox_Connection::TOX_CONNECTION_UDP || !_udp_only) { + _tcl.forEachGroupPeer(group_number, [group_number, this](uint32_t peer_number) { + if (!_udp_only || _tcl.getGroupPeerConnectionStatus(group_number, peer_number) == Tox_Connection::TOX_CONNECTION_UDP) { _peer_in_targets.push_back({group_number, peer_number}); } }); @@ -278,7 +290,12 @@ bool SHA1::iterate(float delta) { std::cout << "SHA1 cwq:" << _chunk_want_queue.size() << " cwqr:" << _chunks_requested.size() << " trc:" << _transfers_receiving_chunk.size() << " tsc:" << _transfers_sending_chunk.size() << "\n"; std::cout << "SHA1 peer down speeds:\n"; for (const auto& [peer, speed] : _peer_in_speed) { - std::cout << " " << peer.first << ":" << peer.second << "(" << _tcl.getGroupPeerName(peer.first, peer.second) << ")" << "\t" << speed / 1024.f << "KiB/s\n"; + std::cout + << " " << peer.first << ":" << peer.second + << " " << (_tcl.getGroupPeerConnectionStatus(peer.first, peer.second) == Tox_Connection::TOX_CONNECTION_TCP ? "tcp" : "udp") + << " (" << _tcl.getGroupPeerName(peer.first, peer.second) << ")" + << " " << speed / 1024.f << "KiB/s\n" + ; } } diff --git a/src/states/sha1.hpp b/src/states/sha1.hpp index ae36d9e..616a3f1 100644 --- a/src/states/sha1.hpp +++ b/src/states/sha1.hpp @@ -57,9 +57,13 @@ struct SHA1 final : public StateI { size_t chunkSize(size_t chunk_index) const; bool haveChunk(const SHA1Digest& hash) const; - private: + public: // config bool _udp_only {false}; + size_t _max_concurrent_in {32}; + size_t _max_concurrent_out {16}; + + private: mio::mmap_sink _file_map; // writable if not all const FTInfoSHA1 _sha1_info; const std::vector _sha1_info_data; @@ -79,11 +83,7 @@ struct SHA1 final : public StateI { // chunk_index -> time since request std::map _chunks_requested; - size_t _max_concurrent_in {32}; - size_t _max_concurrent_out {16}; - std::minstd_rand _rng {1337}; - std::uniform_int_distribution _distrib; std::unordered_map _chunk_hash_to_index; @@ -114,7 +114,6 @@ struct SHA1 final : public StateI { // _peer_in_speed feeds directly into _peer_in_targets_dist std::vector> _peer_in_targets; std::discrete_distribution _peer_in_targets_dist; - }; } // States diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 40f50cf..327aa3c 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -238,6 +238,10 @@ std::string_view ToxClient::getGroupPeerName(uint32_t group_number, uint32_t pee } } +TOX_CONNECTION ToxClient::getGroupPeerConnectionStatus(uint32_t group_number, uint32_t peer_number) const { + return tox_group_peer_get_connection_status(_tox, group_number, peer_number, nullptr); +} + void ToxClient::onToxSelfConnectionStatus(TOX_CONNECTION connection_status) { std::cout << "TCL self status: "; switch (connection_status) { @@ -269,14 +273,14 @@ void ToxClient::onToxGroupPeerName(uint32_t group_number, uint32_t peer_id, std: _groups[group_number][peer_id].name = name; } -void ToxClient::onToxGroupPeerConnection(uint32_t group_number, uint32_t peer_id, TOX_CONNECTION connection_status) { - std::cout << "TCL peer " << group_number << ":" << peer_id << " status: "; - switch (connection_status) { - case TOX_CONNECTION::TOX_CONNECTION_NONE: std::cout << "offline\n"; break; - case TOX_CONNECTION::TOX_CONNECTION_TCP: std::cout << "TCP-relayed\n"; break; - case TOX_CONNECTION::TOX_CONNECTION_UDP: std::cout << "UDP-direct\n"; break; - } -} +//void ToxClient::onToxGroupPeerConnection(uint32_t group_number, uint32_t peer_id, TOX_CONNECTION connection_status) { + //std::cout << "TCL peer " << group_number << ":" << peer_id << " status: "; + //switch (connection_status) { + //case TOX_CONNECTION::TOX_CONNECTION_NONE: std::cout << "offline\n"; break; + //case TOX_CONNECTION::TOX_CONNECTION_TCP: std::cout << "TCP-relayed\n"; break; + //case TOX_CONNECTION::TOX_CONNECTION_UDP: std::cout << "UDP-direct\n"; break; + //} +//} void ToxClient::onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length) { // TODO: signal private? @@ -310,7 +314,7 @@ void ToxClient::onToxGroupPeerJoin(uint32_t group_number, uint32_t peer_id) { tmp_name.push_back('\0'); // make sure its null terminated _groups[group_number][peer_id] = { - tox_group_peer_get_connection_status(_tox, group_number, peer_id, nullptr), + //tox_group_peer_get_connection_status(_tox, group_number, peer_id, nullptr), reinterpret_cast(tmp_name.data()) }; diff --git a/src/tox_client.hpp b/src/tox_client.hpp index 1a40d75..2c74760 100644 --- a/src/tox_client.hpp +++ b/src/tox_client.hpp @@ -34,6 +34,7 @@ struct ToxClient { std::string getOwnAddress(void) const; std::string_view getGroupPeerName(uint32_t group_number, uint32_t peer_number) const; + TOX_CONNECTION getGroupPeerConnectionStatus(uint32_t group_number, uint32_t peer_number) const; template void forEachGroup(FN&& fn) const { @@ -46,8 +47,7 @@ struct ToxClient { void forEachGroupPeer(uint32_t group_number, FN&& fn) const { if (_groups.count(group_number)) { for (const auto& [peer_number, peer] : _groups.at(group_number)) { - const auto& [connection_status, name] = peer; - fn(peer_number, connection_status); + fn(peer_number); } } } @@ -56,7 +56,7 @@ struct ToxClient { void onToxSelfConnectionStatus(TOX_CONNECTION connection_status); void onToxFriendRequest(const uint8_t* public_key, std::string_view message); void onToxGroupPeerName(uint32_t group_number, uint32_t peer_id, std::string_view name); - void onToxGroupPeerConnection(uint32_t group_number, uint32_t peer_id, TOX_CONNECTION connection_status); + //void onToxGroupPeerConnection(uint32_t group_number, uint32_t peer_id, TOX_CONNECTION connection_status); void onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length); void onToxGroupCustomPrivatePacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length); void onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_data, size_t invite_length, std::string_view group_name); @@ -90,12 +90,10 @@ struct ToxClient { std::unique_ptr _state; - // key groupid, value set of peer ids - //std::map> _groups; struct Peer { - Tox_Connection connection_status {Tox_Connection::TOX_CONNECTION_NONE}; std::string name; }; + // key groupid, key peerid std::map> _groups; };