boost-asio

Which Boost error codes/conditions are returned by which Boost.Asio calls?

 ̄綄美尐妖づ 提交于 2019-12-01 09:44:07
I am currently writing a TCP I/O facility that uses Boost.Asio as the underlying socket API, and I've noticed that Boost.Asio seems to lack documentation on which specific Boost error codes/conditions can result from each individual operation (e.g. function/method call or asynchronous operation). All that I've been able to find is the error code API and some informal error code lists, none of which correlate specific codes to specific operations . This apparent lack of documentation is frustrating, because it's difficult to write robust code when you don't know the possible failure modes. It's

How to read a fix-sized packet using boost asio?

我的未来我决定 提交于 2019-12-01 09:36:13
I am doing a synchronous read/write using boost-asio. The data is coming in binary format, without boundary, the length information is encoded in the packet format. So it is important to read in with specified size. Can ip::tcp::iostream do that? Can someone provide an example? Thanks. I work on a program wich send different data with different size. I use a fixed header of 8 byte to encode the size, then, I add the data : enum { header_length = 8 }; //const header length I get the size (m_outbound_data is a std::string == a serialized object) //give header length std::ostringstream header

How to read a fix-sized packet using boost asio?

你。 提交于 2019-12-01 09:12:44
问题 I am doing a synchronous read/write using boost-asio. The data is coming in binary format, without boundary, the length information is encoded in the packet format. So it is important to read in with specified size. Can ip::tcp::iostream do that? Can someone provide an example? Thanks. 回答1: I work on a program wich send different data with different size. I use a fixed header of 8 byte to encode the size, then, I add the data : enum { header_length = 8 }; //const header length I get the size

asio implicit strand and data synchronization

六月ゝ 毕业季﹏ 提交于 2019-12-01 09:03:03
问题 When I read asio source code, I am curious about how asio making data synchronized between threads even a implicit strand was made. These are code in asio: io_service::run mutex::scoped_lock lock(mutex_); std::size_t n = 0; for (; do_run_one(lock, this_thread, ec); lock.lock()) if (n != (std::numeric_limits<std::size_t>::max)()) ++n; return n; io_service::do_run_one while (!stopped_) { if (!op_queue_.empty()) { // Prepare to execute first handler from queue. operation* o = op_queue_.front();

boost::asio - peeking into a socket buffer

时光毁灭记忆、已成空白 提交于 2019-12-01 08:35:45
问题 I use boost::asio::read (or may be the equivalent async_read) to read some data from a socket. Is it possible that I leave the bytes read in the underlying socket so that next time I call read on the socket I receive again that data ? 回答1: Like Simon said, you can't do it with boost::asio::read() (or boost::asio::async_read() ). However, for read() you could call native_handle() on the socket to get the socket descriptor and then use ::recvmsg() with the MSG_PEEK flag. Similarly, you could

Boost.Asio thread safety

左心房为你撑大大i 提交于 2019-12-01 08:24:03
Is it safe to call async_write & async_read from different threads in situation when io_service::run() was called from only one thread? Thank you! Sam Miller Is it safe to call async_write & async_read from different threads Yes, but with a caveat. You can safely do this for distinct socket objects Thread Safety Distinct objects: Safe . Shared objects: Unsafe . The documentation is conservative on that and says "no". But I'm doing this anyway in one of my linux programs and it seems to work fine. Digging into boost/asio/detail/epoll_reactor.ipp shows that the function start_op() is indeed

Can I share boost::asio::tcp::socket object between 2 threads that perform read and write

蓝咒 提交于 2019-12-01 08:14:25
I have two threads, one sending and another receiving data via TCP socket. I use boost::asio::read() and boost::asio::write() for reading and writing. My question is do I have to guard the access to socket object during read and write operation? The other case would be what if I have two threads both writing using the same socket object? The socket is not thread safe when shared between two or more threads. For more information look at the Boost.Asio documentation . 来源: https://stackoverflow.com/questions/6941626/can-i-share-boostasiotcpsocket-object-between-2-threads-that-perform-read

How to run boost asio resolver service on more threads?

天涯浪子 提交于 2019-12-01 08:07:56
问题 I am using boost::asio::ip::udp::resolver in an SNMPV2 implementation to determine wheather a host is reachable or not. using Resolver = boost::asio::ip::udp::resolver; Resolver resolver(ioService); Resolver::query query(connectOptions.getHost(), connectOptions.getPort()); Resolver::iterator endpointIterator; BOOST_LOG_SEV(logger, Severity::debug) << "Waiting for async resolve"; endpointIterator = resolver.async_resolve(query, yield); BOOST_LOG_SEV(logger, Severity::debug) << "Async resolve

asio::io_service and thread_group lifecycle issue

徘徊边缘 提交于 2019-12-01 07:28:31
问题 Looking at answers like this one, we can do stuff like: boost::asio::io_service ioService; boost::thread_group threadpool; { boost::asio::io_service::work work(ioService); threadpool.create_thread(boost::bind(&boost::asio::io_service::run, ioService)); threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService)); ioService.post(boost::bind(...)); ioService.post(boost::bind(...)); ioService.post(boost::bind(...)); } threadpool.join_all(); However, in my case I want to do

How to reduce compilation times with Boost Asio

廉价感情. 提交于 2019-12-01 06:50:11
问题 Boost.Asio is great library but it has one huge drawback -- extreamly slow compilation times. A simple implementation (really simple) of HTTP protocol (about 1k lines of code) requires about 13.5s to compile under GCC 4.4! I tryed to use PCH but it does not improve compilation times too much (about 1s. only). So are there any tutorials on how to make Boost.Asio compilation times faster? For example what headers should I exactly include for what class. I use for example: io_service , tcp::ip: