diff --git a/src/states/receive_start_sha1.cpp b/src/states/receive_start_sha1.cpp index ee60d15..6b61d8a 100644 --- a/src/states/receive_start_sha1.cpp +++ b/src/states/receive_start_sha1.cpp @@ -5,11 +5,14 @@ #include "../tox_utils.hpp" #include "../ft_sha1_info.hpp" +#include "../tox_client.hpp" + #include #include #include #include +#include namespace States { @@ -25,8 +28,28 @@ ReceiveStartSHA1::ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl) : Stat } bool ReceiveStartSHA1::iterate(float delta) { + _time_since_last_request += delta; + + // iterate and timeout + if (_transfer.has_value()) { + float& time_since_remote_activity = std::get(_transfer.value()); + time_since_remote_activity += delta; + + // timout if not heard after 10s + if (time_since_remote_activity >= 10.f) { + std::cerr << "ReceiveStartSHA1 info tansfer timed out " << std::get<0>(*_transfer) << ":" << std::get<1>(*_transfer) << "." << std::get<2>(*_transfer) << "\n"; + + _transfer.reset(); + } + } else if (_time_since_last_request >= 15.f) { // blast ever 15sec + _time_since_last_request = 0.f; + //_tcl.sendFT1RequestPrivate( + + } + + // if not transfer, request from random peer (equal dist!!) // TODO: return true if done - return false; + return _done; } std::unique_ptr ReceiveStartSHA1::nextState(void) { @@ -51,18 +74,52 @@ std::unique_ptr ReceiveStartSHA1::nextState(void) { } // sha1_info -void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) { +void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Info(uint32_t, uint32_t, const uint8_t*, size_t) { + // shrug, we dont have it either } bool ReceiveStartSHA1::onFT1ReceiveInitSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size, const uint8_t transfer_id, const size_t file_size) { + if (file_id_size != _sha1_info_hash.size()) { + std::cerr << "ReceiveStartSHA1 got request for sha1_info of wrong size!!\n"; + return false; + } + + SHA1Digest requested_hash(file_id, file_id_size); + + if (requested_hash != _sha1_info_hash) { + std::cout << "ReceiveStartSHA1 ignoring different info request " << requested_hash << "\n"; + return false; + } + + if (_transfer.has_value()) { + // TODO: log? + return false; // allready in progress + } + + _sha1_info_data.resize(file_size); + + _transfer = std::make_tuple(group_number, peer_number, transfer_id, 0.f); + std::cout << "ReceiveStartSHA1 accepted info transfer" << group_number << ":" << peer_number << "." << transfer_id << "\n"; + // accept return true; } void ReceiveStartSHA1::onFT1ReceiveDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, const uint8_t* data, size_t data_size) { + // TODO: test if not current transfer + + for (size_t i = 0; i < data_size; i++) { + _sha1_info_data[data_offset+i] = data[i]; + } + + if (data_offset + data_size == _sha1_info_data.size()) { + std::cout << "ReceiveStartSHA1 info tansfer finished " << group_number << ":" << peer_number << "." << transfer_id << "\n"; + _done = true; + } } -void ReceiveStartSHA1::onFT1SendDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) { +void ReceiveStartSHA1::onFT1SendDataSHA1Info(uint32_t, uint32_t, uint8_t, size_t, uint8_t*, size_t) { + // we cant send what we dont have } // sha1_chunk diff --git a/src/states/receive_start_sha1.hpp b/src/states/receive_start_sha1.hpp index 3b2fa8b..8264e69 100644 --- a/src/states/receive_start_sha1.hpp +++ b/src/states/receive_start_sha1.hpp @@ -6,6 +6,7 @@ #include "../ft_sha1_info.hpp" #include +#include namespace States { @@ -35,6 +36,13 @@ struct ReceiveStartSHA1 final : public StateI { //FTInfoSHA1 _sha1_info; std::vector _sha1_info_data; SHA1Digest _sha1_info_hash; // treat as const + + // group_number, peer_number, transfer_id, time_since_remote_activity + std::optional> _transfer; + + bool _done {false}; + + float _time_since_last_request {5.f}; }; } // States diff --git a/src/states/sha1.cpp b/src/states/sha1.cpp index 0871329..3af79bf 100644 --- a/src/states/sha1.cpp +++ b/src/states/sha1.cpp @@ -97,7 +97,8 @@ void SHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_numb SHA1Digest requested_hash(file_id, file_id_size); if (requested_hash != _sha1_info_hash) { - std::cout << "SHA1 ignoring diffenrent info request " << requested_hash << "\n"; + std::cout << "SHA1 ignoring different info request " << requested_hash << "\n"; + return; } // same hash, should respond diff --git a/src/tox_client.cpp b/src/tox_client.cpp index a82f190..2e43757 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -5,6 +5,7 @@ #include "./states/send_start_sha1.hpp" #include "./states/receive_start_sha1.hpp" +#include "ngc_ft1.h" #include #include @@ -257,6 +258,18 @@ StateI& ToxClient::getState(void) { return *_state.get(); } +bool ToxClient::sendFT1RequestPrivate(uint32_t group_number, uint32_t peer_number, NGC_FT1_file_kind file_kind, const uint8_t* file_id, size_t file_id_size) { + NGC_FT1_send_request_private( + _tox, _ft1_ctx, + group_number, peer_number, + file_kind, + file_id, file_id_size + ); + + // TODO: return value + return true; +} + bool ToxClient::sendFT1InitPrivate(uint32_t group_number, uint32_t peer_number, NGC_FT1_file_kind file_kind, const uint8_t* file_id, size_t file_id_size, uint64_t file_size, uint8_t& transfer_id) { return NGC_FT1_send_init_private( _tox, _ft1_ctx, diff --git a/src/tox_client.hpp b/src/tox_client.hpp index 8bcd5b1..94527f0 100644 --- a/src/tox_client.hpp +++ b/src/tox_client.hpp @@ -43,6 +43,7 @@ struct ToxClient { StateI& getState(void); // public accessor for callbacks public: // FT1 sends + bool sendFT1RequestPrivate(uint32_t group_number, uint32_t peer_number, NGC_FT1_file_kind file_kind, const uint8_t* file_id, size_t file_id_size); bool sendFT1InitPrivate(uint32_t group_number, uint32_t peer_number, NGC_FT1_file_kind file_kind, const uint8_t* file_id, size_t file_id_size, uint64_t file_size, uint8_t& transfer_id); private: @@ -59,8 +60,5 @@ struct ToxClient { bool _tox_profile_dirty {false}; // set in callbacks std::unique_ptr _state; - - // TODO: this is a hack, make better? - friend States::SendStartSHA1; };