Here's my implementation :
- Client A send a message for Client B
- Server process the message by
async_read
the right amount of data and will wait for new data from Client A (in Order not to block Client A) - Afterwards Server will process the information (probably do a mysql
query) and then send the message to Client B with
async_write
.
The problem is, if Client A send message really fast, async_writes
will interleave before the previous async_write handler is called.
Is there a simple way to avoid this problem ?
EDIT 1 : If a Client C sends a message to Client B just after Client A, the same issue should appear...
EDIT 2 : This would work ? because it seems to block, I don't know where...
namespace structure {
class User {
public:
User(boost::asio::io_service& io_service, boost::asio::ssl::context& context) :
m_socket(io_service, context), m_strand(io_service), is_writing(false) {}
ssl_socket& getSocket() {
return m_socket;
}
boost::asio::strand getStrand() {
return m_strand;
}
void push(std::string str) {
m_strand.post(boost::bind(&structure::User::strand_push, this, str));
}
void strand_push(std::string str) {
std::cout << "pushing: " << boost::this_thread::get_id() << std::endl;
m_queue.push(str);
if (!is_writing) {
write();
std::cout << "going to write" << std::endl;
}
std::cout << "Already writing" << std::endl;
}
void write() {
std::cout << "writing" << std::endl;
is_writing = true;
std::string str = m_queue.front();
boost::asio::async_write(m_socket,
boost::asio::buffer(str.c_str(), str.size()),
boost::bind(&structure::User::sent, this)
);
}
void sent() {
std::cout << "sent" << std::endl;
m_queue.pop();
if (!m_queue.empty()) {
write();
return;
}
else
is_writing = false;
std::cout << "done sent" << std::endl;
}
private:
ssl_socket m_socket;
boost::asio::strand m_strand;
std::queue<std::string> m_queue;
bool is_writing;
};
}
#endif
str
is destroyed whenwrite()
returns, which may be beforeboost::asio_async_write()
accesses the buffer. – Ormuz