0

This is the code I am using to send a string message to the client. The problem, I think, has to do with the buffer.

void Client::send_message(std::string message) {
message = message + "\n";
std::ostream sending(&_out_buffer);
sending << message;
boost::asio::async_write(this->_socket, this->_out_buffer,
        boost::bind(&Client::send_callback, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));

debugger( "--> Message send to " + this->_client_name + " : " + message );

}

The problem, is that when I send a sequence of messages fairly quickly like so:

//Send the data to client
    client->send_message("connected " + boost::lexical_cast<std::string>(number_of_cells));


    //Send the cells
    std::set<std::string> cells = it->second->get_names_of_all_non_empty_cells();
    std::set<std::string>::iterator cell = cells.begin();
    std::set<std::string>::iterator end = cells.end();
    for(; cell != end; cell++) {
        client->send_message("cell " + *cell + " " + it->second->get_cell_contents(*cell));
    }

I will get this on the client side:

connected 2

connected 2

cell A1 testcontent

connected 2

cell A1 testcontent

cell B4 anothertest

When I drop the for loop in the sending code, it only sends the "connected 2" message one time. So I am thinking that this has to do with the buffer not being cleared properly or loaded up too quickly. I am unsure though. Is there a way to handle this situation? It only occurs when I send a lot of messages quickly in a block of code.

Thanks!

edit:

I found a workaround, though this doesn't fix the problem. Instead of calling the method "send_message" repeatedly, I just pulled together one large string and sent it. The new code:

//Send the data to client
    std::string message = "connected " + boost::lexical_cast<std::string>(number_of_cells) + "\n";


    //Send the cells
    std::set<std::string> cells = it->second->get_names_of_all_non_empty_cells();
    std::set<std::string>::iterator cell = cells.begin();
    std::set<std::string>::iterator end = cells.end();
    for(; cell != end; cell++) {
        message += "cell " + *cell + " " + it->second->get_cell_contents(*cell) +"\n";
    }

    client->send_message(message);
Community
  • 1
  • 1
user2438390
  • 3
  • 1
  • 6

0 Answers0