Commit 45b4bb33 authored by AdamSimpson's avatar AdamSimpson
Browse files

Set file chunk size to socket buffer values

parent 76889da1
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -56,9 +56,14 @@ public:

    // TODO set chunk size to the size of the socket receive buffer?
    template <typename Handler>
    void async_receive_file(boost::filesystem::path file_path, const Handler& handler, std::size_t chunk_size=1024) {
    void async_receive_file(boost::filesystem::path file_path, const Handler& handler) {
        std::ofstream file;

        // Get the socket receive buffer size and use that as the chunk size
        boost::asio::socket_base::receive_buffer_size option;
        socket.get_option(option);
        std::size_t chunk_size = option.value();

        // Throw exception if we run into any file issues
        file.exceptions(std::fstream::failbit | std::ifstream::badbit);

@@ -138,16 +143,21 @@ public:
    void send(asio::streambuf &message_body);

    // Send a file in multiple chunk_size peices
    void send_file(boost::filesystem::path file_path, std::size_t chunk_size = 1024);
    void send_file(boost::filesystem::path file_path);
    // Read a file in multiple chunk_size peices
    void receive_file(boost::filesystem::path file_path, std::size_t chunk_size = 1024);
    void receive_file(boost::filesystem::path file_path);

    // TODO set chunk size to the size of the socket receive buffer?
    // Send a file as a message asynchronously
    template <typename Handler>
    void async_send_file(boost::filesystem::path file_path, const Handler& handler, std::size_t chunk_size=1024) {
    void async_send_file(boost::filesystem::path file_path, const Handler& handler) {
        std::ifstream file;

        // Get the socket receive buffer size and use that as the chunk size
        boost::asio::socket_base::send_buffer_size option;
        socket.get_option(option);
        std::size_t chunk_size = option.value();

        // Throw exception if we run into any file issues
        file.exceptions(std::fstream::failbit | std::ifstream::badbit);

+13 −3
Original line number Diff line number Diff line
@@ -50,15 +50,20 @@ void Messenger::send(asio::streambuf &message_body) {
}

// Send a file as a message
void Messenger::send_file(boost::filesystem::path file_path, std::size_t chunk_size) {
void Messenger::send_file(boost::filesystem::path file_path) {
    std::ifstream file;

    // Get the socket receive buffer size and use that as the chunk size
    boost::asio::socket_base::send_buffer_size option;
    socket.get_option(option);
    std::size_t chunk_size = option.value();

    // Throw exception if we run into any file issues
    file.exceptions(std::fstream::failbit | std::ifstream::badbit);

    // Open file and get size
    file.open(file_path.string(), std::fstream::in | std::fstream::binary);
    auto file_size = boost::filesystem::file_size(file_path);
    std::size_t file_size = boost::filesystem::file_size(file_path);

    // Send message header
    send_header(file_size, MessageType::file);
@@ -95,9 +100,14 @@ std::string Messenger::receive(MessageType type) {
    return body;
}

void Messenger::receive_file(boost::filesystem::path file_path, std::size_t chunk_size) {
void Messenger::receive_file(boost::filesystem::path file_path) {
    std::ofstream file;

    // Get the socket receive buffer size and use that as the chunk size
    boost::asio::socket_base::receive_buffer_size option;
    socket.get_option(option);
    std::size_t chunk_size = option.value();

    // Throw exception if we run into any file issues
    file.exceptions(std::fstream::failbit | std::ifstream::badbit);