start writing the reveive part

This commit is contained in:
Green Sky 2023-01-15 20:46:09 +01:00
parent 3e0100efdf
commit c712afb7b2
No known key found for this signature in database
7 changed files with 134 additions and 3 deletions

View File

@ -30,6 +30,9 @@ add_executable(tox_ngc_ft1_tool
./states/send_start_sha1.hpp ./states/send_start_sha1.hpp
./states/send_start_sha1.cpp ./states/send_start_sha1.cpp
./states/receive_start_sha1.hpp
./states/receive_start_sha1.cpp
./states/sha1.hpp ./states/sha1.hpp
./states/sha1.cpp ./states/sha1.cpp
) )

View File

@ -10,8 +10,8 @@
struct SHA1Digest { struct SHA1Digest {
std::array<uint8_t, 20> data; std::array<uint8_t, 20> data;
SHA1Digest(void) = default;
SHA1Digest(const std::vector<uint8_t>& v); SHA1Digest(const std::vector<uint8_t>& v);
SHA1Digest(const uint8_t* d, size_t s); SHA1Digest(const uint8_t* d, size_t s);
bool operator==(const SHA1Digest& other) const { return data == other.data; } bool operator==(const SHA1Digest& other) const { return data == other.data; }

View File

@ -0,0 +1,83 @@
#include "./receive_start_sha1.hpp"
#include "./sha1.hpp"
#include "../tox_utils.hpp"
#include "../ft_sha1_info.hpp"
#include <mio/mio.hpp>
#include <iostream>
#include <exception>
#include <memory>
namespace States {
ReceiveStartSHA1::ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl) : StateI(tcl) {
if (cl.receive_id.empty()) {
throw std::runtime_error("receiver missing id");
}
_sha1_info_hash = hex2bin(cl.receive_id);
assert(_sha1_info_hash.size() == 20);
std::cout << "ReceiveStartSHA1 downloading info for " << cl.receive_id << "\n";
}
bool ReceiveStartSHA1::iterate(float delta) {
// TODO: return true if done
return false;
}
std::unique_ptr<StateI> ReceiveStartSHA1::nextState(void) {
std::cout << "ReceiveStartSHA1 switching state to SHA1\n";
FTInfoSHA1 sha1_info;
// from buffer
// open file for writing (pre allocate?)
mio::mmap_sink file_map;
std::vector<bool> have_chunk(sha1_info.chunks.size(), false);
return std::make_unique<SHA1>(
_tcl,
std::move(file_map),
std::move(sha1_info),
std::move(_sha1_info_data),
std::move(_sha1_info_hash),
std::move(have_chunk)
);
}
// sha1_info
void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_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) {
// 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) {
}
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) {
}
// sha1_chunk
void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Chunk(uint32_t, uint32_t, const uint8_t*, size_t) {
}
bool ReceiveStartSHA1::onFT1ReceiveInitSHA1Chunk(uint32_t, uint32_t, const uint8_t*, size_t, const uint8_t, const size_t) {
return false; // nope, not interested in chunks yet
}
void ReceiveStartSHA1::onFT1ReceiveDataSHA1Chunk(uint32_t, uint32_t, uint8_t, size_t, const uint8_t*, size_t) {
}
void ReceiveStartSHA1::onFT1SendDataSHA1Chunk(uint32_t, uint32_t, uint8_t, size_t, uint8_t*, size_t) {
}
} // States

View File

@ -0,0 +1,41 @@
#pragma once
#include "../state.hpp"
#include "../command_line.hpp"
#include "../ft_sha1_info.hpp"
#include <vector>
namespace States {
// we have the info hash and request the info until we have it
struct ReceiveStartSHA1 final : public StateI {
public: // general interface
ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl);
~ReceiveStartSHA1(void) override = default;
bool iterate(float delta) override;
std::unique_ptr<StateI> 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:
//FTInfoSHA1 _sha1_info;
std::vector<uint8_t> _sha1_info_data;
SHA1Digest _sha1_info_hash; // treat as const
};
} // States

View File

@ -11,7 +11,8 @@ SHA1::SHA1(
mio::mmap_source&& file_map, mio::mmap_source&& file_map,
const FTInfoSHA1&& sha1_info, const FTInfoSHA1&& sha1_info,
const std::vector<uint8_t>&& sha1_info_data, const std::vector<uint8_t>&& sha1_info_data,
const std::vector<uint8_t>&& sha1_info_hash, //const std::vector<uint8_t>&& sha1_info_hash,
const SHA1Digest&& sha1_info_hash,
std::vector<bool>&& have_chunk std::vector<bool>&& have_chunk
) : ) :
StateI(tcl), StateI(tcl),

View File

@ -20,7 +20,8 @@ struct SHA1 final : public StateI {
mio::mmap_source&& file_map, mio::mmap_source&& file_map,
const FTInfoSHA1&& sha1_info, const FTInfoSHA1&& sha1_info,
const std::vector<uint8_t>&& sha1_info_data, const std::vector<uint8_t>&& sha1_info_data,
const std::vector<uint8_t>&& sha1_info_hash, //const std::vector<uint8_t>&& sha1_info_hash,
const SHA1Digest&& sha1_info_hash,
std::vector<bool>&& have_chunk std::vector<bool>&& have_chunk
); );
~SHA1(void) override = default; ~SHA1(void) override = default;

View File

@ -4,6 +4,7 @@
#include "./tox_callbacks.hpp" #include "./tox_callbacks.hpp"
#include "./states/send_start_sha1.hpp" #include "./states/send_start_sha1.hpp"
#include "./states/receive_start_sha1.hpp"
#include <memory> #include <memory>
#include <sodium.h> #include <sodium.h>
@ -127,6 +128,7 @@ ToxClient::ToxClient(const CommandLine& cl) :
if (!cl.send_path.empty()) { if (!cl.send_path.empty()) {
_state = std::make_unique<States::SendStartSHA1>(*this, cl); _state = std::make_unique<States::SendStartSHA1>(*this, cl);
} else { // receiver } else { // receiver
_state = std::make_unique<States::ReceiveStartSHA1>(*this, cl);
} }
} }