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()
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)
{
this->senders.push_back(sender);
......
......@@ -9,11 +9,13 @@ static constexpr std::size_t CHUNK_SIZE = 262144u;
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),
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->id = std::to_string(TransferSender::current_id++);
......@@ -69,5 +71,5 @@ void TransferSender::send_next_chunk()
this->client->send(message, std::bind(&TransferSender::send_next_chunk, this));
}
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 @@
#include <boost/asio.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
class TLSSocket;
template <typename SocketType>
class MessageHandler;
#ifndef __TRANSFER_SENDER_HPP__
# define __TRANSFER_SENDER_HPP__
static const fs::path FILES_TO_SEND_DIRECTORY("./send/");
class RemoteClient;
static const boost::filesystem::path FILES_TO_SEND_DIRECTORY("./send/");
class TransferSender
{
public:
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();
/**
* Try to start the file transfer, returns true if there was
......@@ -46,9 +49,11 @@ private:
TransferSender(const TransferSender&);
TransferSender& operator=(const TransferSender&);
RemoteClient* client;
MessageHandler<TLSSocket>* client;
const std::string filename;
fs::ifstream file;
std::function<void(const TransferSender*)> end_callback;
boost::filesystem::ifstream file;
int length;
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