more receiver logic

This commit is contained in:
Green Sky 2023-01-15 21:13:14 +01:00
parent c712afb7b2
commit 9249ac83d9
No known key found for this signature in database
5 changed files with 84 additions and 7 deletions

View File

@ -5,11 +5,14 @@
#include "../tox_utils.hpp" #include "../tox_utils.hpp"
#include "../ft_sha1_info.hpp" #include "../ft_sha1_info.hpp"
#include "../tox_client.hpp"
#include <mio/mio.hpp> #include <mio/mio.hpp>
#include <iostream> #include <iostream>
#include <exception> #include <exception>
#include <memory> #include <memory>
#include <tuple>
namespace States { namespace States {
@ -25,8 +28,28 @@ ReceiveStartSHA1::ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl) : Stat
} }
bool ReceiveStartSHA1::iterate(float delta) { bool ReceiveStartSHA1::iterate(float delta) {
_time_since_last_request += delta;
// iterate and timeout
if (_transfer.has_value()) {
float& time_since_remote_activity = std::get<float>(_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 // TODO: return true if done
return false; return _done;
} }
std::unique_ptr<StateI> ReceiveStartSHA1::nextState(void) { std::unique_ptr<StateI> ReceiveStartSHA1::nextState(void) {
@ -51,18 +74,52 @@ std::unique_ptr<StateI> ReceiveStartSHA1::nextState(void) {
} }
// sha1_info // 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) { 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 // accept
return true; 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) { 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];
} }
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) { 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, uint32_t, uint8_t, size_t, uint8_t*, size_t) {
// we cant send what we dont have
} }
// sha1_chunk // sha1_chunk

View File

@ -6,6 +6,7 @@
#include "../ft_sha1_info.hpp" #include "../ft_sha1_info.hpp"
#include <vector> #include <vector>
#include <optional>
namespace States { namespace States {
@ -35,6 +36,13 @@ struct ReceiveStartSHA1 final : public StateI {
//FTInfoSHA1 _sha1_info; //FTInfoSHA1 _sha1_info;
std::vector<uint8_t> _sha1_info_data; std::vector<uint8_t> _sha1_info_data;
SHA1Digest _sha1_info_hash; // treat as const SHA1Digest _sha1_info_hash; // treat as const
// group_number, peer_number, transfer_id, time_since_remote_activity
std::optional<std::tuple<uint32_t, uint32_t, uint8_t, float>> _transfer;
bool _done {false};
float _time_since_last_request {5.f};
}; };
} // States } // States

View File

@ -97,7 +97,8 @@ void SHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_numb
SHA1Digest requested_hash(file_id, file_id_size); SHA1Digest requested_hash(file_id, file_id_size);
if (requested_hash != _sha1_info_hash) { 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 // same hash, should respond

View File

@ -5,6 +5,7 @@
#include "./states/send_start_sha1.hpp" #include "./states/send_start_sha1.hpp"
#include "./states/receive_start_sha1.hpp" #include "./states/receive_start_sha1.hpp"
#include "ngc_ft1.h"
#include <memory> #include <memory>
#include <sodium.h> #include <sodium.h>
@ -257,6 +258,18 @@ StateI& ToxClient::getState(void) {
return *_state.get(); 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) { 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( return NGC_FT1_send_init_private(
_tox, _ft1_ctx, _tox, _ft1_ctx,

View File

@ -43,6 +43,7 @@ struct ToxClient {
StateI& getState(void); // public accessor for callbacks StateI& getState(void); // public accessor for callbacks
public: // FT1 sends 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); 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: private:
@ -59,8 +60,5 @@ struct ToxClient {
bool _tox_profile_dirty {false}; // set in callbacks bool _tox_profile_dirty {false}; // set in callbacks
std::unique_ptr<StateI> _state; std::unique_ptr<StateI> _state;
// TODO: this is a hack, make better?
friend States::SendStartSHA1;
}; };