From 0606db457b9484c2d88bf2a2d7b560b35af1e20f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 15 Jan 2023 02:26:23 +0100 Subject: [PATCH] sha1 info running state --- src/CMakeLists.txt | 7 ++- ...end_start_sha1.cpp => send_start_sha1.cpp} | 24 +++++--- ...end_start_sha1.hpp => send_start_sha1.hpp} | 0 src/states/sha1.cpp | 57 +++++++++++++++++++ src/states/sha1.hpp | 48 ++++++++++++++++ src/tox_client.cpp | 2 +- 6 files changed, 128 insertions(+), 10 deletions(-) rename src/states/{state_send_start_sha1.cpp => send_start_sha1.cpp} (79%) rename src/states/{state_send_start_sha1.hpp => send_start_sha1.hpp} (100%) create mode 100644 src/states/sha1.cpp create mode 100644 src/states/sha1.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c35fd70..d6ded60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,8 +24,11 @@ add_executable(tox_ngc_ft1_tool ./state.hpp - ./states/state_send_start_sha1.hpp - ./states/state_send_start_sha1.cpp + ./states/send_start_sha1.hpp + ./states/send_start_sha1.cpp + + ./states/sha1.hpp + ./states/sha1.cpp ) target_compile_features(tox_ngc_ft1_tool PUBLIC cxx_std_17) diff --git a/src/states/state_send_start_sha1.cpp b/src/states/send_start_sha1.cpp similarity index 79% rename from src/states/state_send_start_sha1.cpp rename to src/states/send_start_sha1.cpp index 101d33a..8f01933 100644 --- a/src/states/state_send_start_sha1.cpp +++ b/src/states/send_start_sha1.cpp @@ -1,10 +1,13 @@ -#include "./state_send_start_sha1.hpp" +#include "./send_start_sha1.hpp" #include "../tox_client.hpp" #include "../tox_utils.hpp" #include "../hash_utils.hpp" #include "../ft_sha1_info.hpp" +#include "./sha1.hpp" + +#include #include #include @@ -41,24 +44,31 @@ SendStartSHA1::SendStartSHA1(ToxClient& tcl, const CommandLine& cl) : StateI(tcl _file_map = mio::make_mmap_source(cl.send_path, 0, mio::map_entire_file, err); } - std::cout << "SHA1Start chunks: " << _sha1_info.chunks.size() << "\n"; + std::cout << "SendStartSHA1 chunks: " << _sha1_info.chunks.size() << "\n"; _sha1_info_data = _sha1_info.toBuffer(); - std::cout << "SHA1Start sha1_info size: " << _sha1_info_data.size() << "\n"; + std::cout << "SendStartSHA1 sha1_info size: " << _sha1_info_data.size() << "\n"; _sha1_info_hash = hash_sha1(_sha1_info_data.data(), _sha1_info_data.size()); - std::cout << "SHA1Start sha1_info_hash: " << bin2hex(_sha1_info_hash) << "\n"; + std::cout << "SendStartSHA1 sha1_info_hash: " << bin2hex(_sha1_info_hash) << "\n"; } bool SendStartSHA1::iterate(void) { - (void)_tcl._tox; - return false; + return true; // TODO: change hashing to async } std::unique_ptr SendStartSHA1::nextState(void) { - return nullptr; + std::cout << "SendStartSHA1 switching state to SHA1\n"; + // we are done setting up + return std::make_unique( + _tcl, + std::move(_file_map), + std::move(_sha1_info), + std::move(_sha1_info_data), + std::move(_sha1_info_hash) + ); } // sha1_info diff --git a/src/states/state_send_start_sha1.hpp b/src/states/send_start_sha1.hpp similarity index 100% rename from src/states/state_send_start_sha1.hpp rename to src/states/send_start_sha1.hpp diff --git a/src/states/sha1.cpp b/src/states/sha1.cpp new file mode 100644 index 0000000..45744cb --- /dev/null +++ b/src/states/sha1.cpp @@ -0,0 +1,57 @@ +#include "./sha1.hpp" + +namespace States { + +SHA1::SHA1( + ToxClient& tcl, + mio::mmap_source&& file_map, + const FTInfoSHA1&& sha1_info, + const std::vector&& sha1_info_data, + const std::vector&& sha1_info_hash +) : + StateI(tcl), + _file_map(std::move(file_map)), + _sha1_info(std::move(sha1_info)), + _sha1_info_data(std::move(sha1_info_data)), + _sha1_info_hash(std::move(sha1_info_hash)) +{ +} + +bool SHA1::iterate(void) { + return false; +} + +std::unique_ptr SHA1::nextState(void) { + return nullptr; +} + +// sha1_info +void SHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) { +} + +bool SHA1::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) { + return false; +} + +void SHA1::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 SHA1::onFT1SendDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) { +} + +// sha1_chunk +void SHA1::onFT1ReceiveRequestSHA1Chunk(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) { +} + +bool SHA1::onFT1ReceiveInitSHA1Chunk(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) { + return false; +} + +void SHA1::onFT1ReceiveDataSHA1Chunk(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 SHA1::onFT1SendDataSHA1Chunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) { +} + +} // States + diff --git a/src/states/sha1.hpp b/src/states/sha1.hpp new file mode 100644 index 0000000..bfa41b9 --- /dev/null +++ b/src/states/sha1.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "../state.hpp" + +#include "../ft_sha1_info.hpp" + +#include + +namespace States { + +// we are either sending or receiving +// we have full info +struct SHA1 final : public StateI { + public: // general interface + SHA1( + ToxClient& tcl, + mio::mmap_source&& file_map, + const FTInfoSHA1&& sha1_info, + const std::vector&& sha1_info_data, + const std::vector&& sha1_info_hash + ); + ~SHA1(void) override = default; + + bool iterate(void) override; + std::unique_ptr nextState(void) override; + + public: // callbacks + // sha1_info + void onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) override; + bool 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) override; + void 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) override; + void onFT1SendDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) override; + + // sha1_chunk + void onFT1ReceiveRequestSHA1Chunk(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) override; + bool onFT1ReceiveInitSHA1Chunk(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) override; + void onFT1ReceiveDataSHA1Chunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, const uint8_t* data, size_t data_size) override; + void onFT1SendDataSHA1Chunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) override; + + private: + mio::mmap_source _file_map; + const FTInfoSHA1 _sha1_info; + const std::vector _sha1_info_data; + const std::vector _sha1_info_hash; +}; + +} // States + diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 405a79a..8e83da8 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -3,7 +3,7 @@ #include "./tox_utils.hpp" #include "./tox_callbacks.hpp" -#include "./states/state_send_start_sha1.hpp" +#include "./states/send_start_sha1.hpp" #include #include