Commit 440cb7bc authored by louiz’'s avatar louiz’

Fix TransferReceiver to work with any MessageHandler<TLSSocket>

parent 420be7fc
#include <logging/logging.hpp>
#include <network/transfer_receiver.hpp>
#include <network/client.hpp>
#include <network/message_handler.hpp>
#include <network/tls_socket.hpp>
using namespace std::string_literals;
static const fs::path FILES_TO_RECEIVE_DIRECTORY("./receive/");
static const boost::filesystem::path FILES_TO_RECEIVE_DIRECTORY("./receive/");
TransferReceiver::TransferReceiver(Client* client,
TransferReceiver::TransferReceiver(MessageHandler<TLSSocket>* client,
const std::string& sid,
const std::string& filename,
int length):
int length,
std::function<void(const TransferReceiver*)>&& end_callback):
client(client),
id(sid),
filename(FILES_TO_RECEIVE_DIRECTORY / filename),
length(length),
received_length(0)
received_length(0),
end_callback(end_callback)
{
this->client->install_callback("TRANSFER_"s + sid, std::bind(&TransferReceiver::get_next_chunk, this, std::placeholders::_1));
this->file.open(this->filename, std::ofstream::binary);
......@@ -43,5 +46,5 @@ void TransferReceiver::get_next_chunk(Message* received_message)
void TransferReceiver::stop()
{
this->client->remove_callback("TRANSFER_"s + this->id);
this->client->on_transfer_ended(this);
this->end_callback(this);
}
......@@ -7,23 +7,27 @@
* @class TransferReceiver
*/
#ifndef __TRANSFER_RECEIVER_HPP__
# define __TRANSFER_RECEIVER_HPP__
#ifndef TRANSFER_RECEIVER_HPP
# define TRANSFER_RECEIVER_HPP
#include <iostream>
#include <string>
#include <functional>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#include <network/message.hpp>
class Client;
class TLSSocket;
template <typename SocketType>
class MessageHandler;
class TransferReceiver
{
public:
TransferReceiver(Client*, const std::string&, const std::string&, int);
TransferReceiver(MessageHandler<TLSSocket>*, const std::string&, const std::string&, int,
std::function<void(const TransferReceiver*)>&&);
~TransferReceiver();
void stop();
......@@ -32,14 +36,16 @@ private:
TransferReceiver& operator=(const TransferReceiver&);
void get_next_chunk(Message*);
Client* client;
MessageHandler<TLSSocket>* client;
std::string id;
fs::path filename;
boost::filesystem::path filename;
int length;
int received_length;
fs::ofstream file;
std::function<void(const TransferReceiver*)> end_callback;
boost::filesystem::ofstream file;
};
#endif // __TRANSFER_RECEIVER_HPP__
#endif // TRANSFER_RECEIVER_HPP
/**@}*/
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