asynchronous - Problems using asio async_read() -


good day!

i'm trying write asynchronous server using boost:asio. working fine until server tries read data 2 clients simultaneously. data 1 client reads supposed, data other client mixed up.

structure of packet: 4 bytes - lenght of message, rest - message

some code:

#define mem_fn(x)       boost::bind(&tcp_connection::x, shared_from_this())  #define mem_fn1(x,y)    boost::bind(&tcp_connection::x, shared_from_this(), y) #define mem_fn2(x,y,z)  boost::bind(&tcp_connection::x, shared_from_this(), y, z)  void tcp_connection::do_read() {   msg_size = 0;   async_read(socket_, boost::asio::buffer(read_buffer_, 4),            mem_fn2(read_len, _1, _2), mem_fn2(on_len, _1, _2)); } void tcp_connection::do_read(size_t size) {   async_read(socket_, boost::asio::buffer(read_buffer_, size),            mem_fn2(read_msg, _1, _2), mem_fn2(on_read, _1, _2)); }  size_t tcp_connection::read_len(const error_code& err, size_t bytes) {   if(err) { qdebug() << "error(read_len): " << err.message().data(); return 0; }   if(bytes == 4 && msg_size == 0)   {     qbytearray a;     a.push_back(read_buffer_[0]); a.push_back(read_buffer_[1]);     a.push_back(read_buffer_[2]); a.push_back(read_buffer_[3]);     qdatastream in(&a, qiodevice::readonly);     int size;     in >> size;     msg_size = size;     return 0;   }   return 1; } size_t tcp_connection::read_msg(const error_code& err, size_t bytes) {   if(err) { qdebug() << "error(read_msg): " << err.message().data(); return 0; }   if(bytes < msg_size)   {     return 1;   } else return 0; } void tcp_connection::on_len(const error_code & err, size_t) {     if(err) { qdebug() << "need drop connection(on_len)"; stop(); }     if(!started_) return;     if(msg_size > 0 && msg_size < 20010) do_read(msg_size);     else do_read(); } 

any advice fix problem?

p.s. forgot add 1 important method: on_len called when read_len finished


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -