Commit 420be7fc authored by louiz’'s avatar louiz’

Fix TransferSender to work with any MessageHandler<TLSSocket> class

Instead of just RemoteClient
parent 23e6a7ad
...@@ -94,7 +94,11 @@ void RemoteClient::on_auth_success() ...@@ -94,7 +94,11 @@ void RemoteClient::on_auth_success()
void RemoteClient::send_file(const std::string& filename) void RemoteClient::send_file(const std::string& filename)
{ {
TransferSender* sender = new TransferSender(this, filename); TransferSender* sender = new TransferSender(this, filename,
[this](const TransferSender* ts)
{
this->on_transfer_ended(ts);
});
if (sender->start() == true) if (sender->start() == true)
{ {
this->senders.push_back(sender); this->senders.push_back(sender);
......
...@@ -9,11 +9,13 @@ static constexpr std::size_t CHUNK_SIZE = 262144u; ...@@ -9,11 +9,13 @@ static constexpr std::size_t CHUNK_SIZE = 262144u;
unsigned long int TransferSender::current_id = 0; unsigned long int TransferSender::current_id = 0;
TransferSender::TransferSender(RemoteClient* client, const std::string& filename): TransferSender::TransferSender(MessageHandler<TLSSocket>* client, const std::string& filename,
std::function<void(const TransferSender*)>&& end_callback):
client(client), client(client),
filename(filename) filename(filename),
end_callback(std::move(end_callback))
{ {
fs::path file_name = FILES_TO_SEND_DIRECTORY / this->filename; boost::filesystem::path file_name = FILES_TO_SEND_DIRECTORY / this->filename;
this->file.open(file_name, std::ofstream::binary); this->file.open(file_name, std::ofstream::binary);
this->id = std::to_string(TransferSender::current_id++); this->id = std::to_string(TransferSender::current_id++);
...@@ -69,5 +71,5 @@ void TransferSender::send_next_chunk() ...@@ -69,5 +71,5 @@ void TransferSender::send_next_chunk()
this->client->send(message, std::bind(&TransferSender::send_next_chunk, this)); this->client->send(message, std::bind(&TransferSender::send_next_chunk, this));
} }
else else
this->client->send(message, std::bind(&RemoteClient::on_transfer_ended, this->client, this)); this->client->send(message, [this]() { this->end_callback(this); });
} }
...@@ -18,20 +18,23 @@ ...@@ -18,20 +18,23 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
class TLSSocket;
template <typename SocketType>
class MessageHandler;
#ifndef __TRANSFER_SENDER_HPP__ #ifndef __TRANSFER_SENDER_HPP__
# define __TRANSFER_SENDER_HPP__ # define __TRANSFER_SENDER_HPP__
static const fs::path FILES_TO_SEND_DIRECTORY("./send/"); static const boost::filesystem::path FILES_TO_SEND_DIRECTORY("./send/");
class RemoteClient;
class TransferSender class TransferSender
{ {
public: public:
static unsigned long int current_id; static unsigned long int current_id;
TransferSender(RemoteClient*, const std::string&); TransferSender(MessageHandler<TLSSocket>* client, const std::string&,
std::function<void(const TransferSender*)>&& end_callback);
~TransferSender(); ~TransferSender();
/** /**
* Try to start the file transfer, returns true if there was * Try to start the file transfer, returns true if there was
...@@ -46,9 +49,11 @@ private: ...@@ -46,9 +49,11 @@ private:
TransferSender(const TransferSender&); TransferSender(const TransferSender&);
TransferSender& operator=(const TransferSender&); TransferSender& operator=(const TransferSender&);
RemoteClient* client; MessageHandler<TLSSocket>* client;
const std::string filename; const std::string filename;
fs::ifstream file; std::function<void(const TransferSender*)> end_callback;
boost::filesystem::ifstream file;
int length; int length;
std::string id; std::string id;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment