boost-asio

Cancel async_read due to timeout

依然范特西╮ 提交于 2019-11-26 23:12:37
问题 I'm trying to write a wrapper synchronous method around async_read to allow non blocking reads on a socket. Following several examples around internet I have developed a solution that seems to be almost right but which is not working. The class declares these relevant attributes and methods: class communications_client { protected: boost::shared_ptr<boost::asio::io_service> _io_service; boost::shared_ptr<boost::asio::ip::tcp::socket> _socket; boost::array<boost::uint8_t, 128> _data; boost:

What's the deal with boost.asio and file i/o?

情到浓时终转凉″ 提交于 2019-11-26 22:36:26
问题 I've noticed that boost.asio has a lot of examples involving sockets, serial ports, and all sorts of non-file examples. Google hasn't really turned up a lot for me that mentions if asio is a good or valid approach for doing asynchronous file i/o. I've got gobs of data i'd like to write to disk asynchronously. This can be done with native overlapped io in Windows (my platform), but I'd prefer to have a platform independent solution. I'm curious if boost.asio has any kind of file support boost

boost asio deadline_timer async_wait(N seconds) twice within N seconds cause operation canceled

无人久伴 提交于 2019-11-26 21:57:01
问题 What I want is when one message queue receives an int N, the handler function will be called after N seconds. below is my code. It runs OK if the duration seconds of two near message queue is larger than the int N, but the handler will print "Operation canceled" in one handler when the duration seconds between two received message queues are smaller than N, which is not what I want. I'd appreciate a lot for any help. #include <boost/asio.hpp> #include <zmq.h> #include <boost/thread.hpp>

Boost.Asio SSL thread safety

只愿长相守 提交于 2019-11-26 21:22:58
问题 Do I create one strand that all of my SSL sockets share, or one strand per SSL context (shared by any associated sockets)? Boost.Asio SSL documentation states this, but it doesn't mention contexts. I assume that this means I must use only one strand for everything, but I think this was written before OpenSSL had multithreading support. SSL and Threads SSL stream objects perform no locking of their own. Therefore, it is essential that all asynchronous SSL operations are performed in an

boost::asio UDP broadcasting

萝らか妹 提交于 2019-11-26 20:54:24
问题 I want to broadcast UDP messages to all computers in a local network using boost::asio . Working through the examples I came up with try { socket.open(boost::asio::ip::udp::v4()); boost::asio::socket_base::broadcast option(true); socket.set_option(option); endpoint = boost::asio::ip::udp::endpoint( boost::asio::ip::address::from_string("192.168.1.255"), port); } catch(std::exception &e) { } and want to broadcast messages from my queue with while(!queue.empty()) { std::string message = queue

C++ Boost ASIO simple periodic timer?

我是研究僧i 提交于 2019-11-26 20:33:46
问题 I want a very simple periodic timer to call my code every 50ms. I could make a thread that sleeps for 50ms all the time (but that's a pain)... I could start looking into Linux API's for making timers (but it's not portable)... I'd like to use boost.. I'm just not sure it's possible. Does boost provide this functionality? 回答1: The second example on Boosts Asio tutorials explains it. You can find it here. After that, check the 3rd example to see how you can call it again with a periodic time

Using boost::asio thread pool for general purpose tasks

拟墨画扇 提交于 2019-11-26 20:15:35
问题 In this blog I found a pretty neat example on how to create a simple thread pool using boost::asio. I basically want to use it like this: #include <thread> #include <functional> #include <boost/asio.hpp> int main ( int argc, char* argv[] ) { asio::io_service io_service; asio::io_service::work work(io_service); std::vector<std::thread> threadPool; for(size_t t = 0; t < std::thread::hardware_concurrency(); t++){ threadPool.push_back(thread(std::bind(&asio::io_service::run, &io_service))); } io

Boost::Asio : io_service.run() vs poll() or how do I integrate boost::asio in mainloop

流过昼夜 提交于 2019-11-26 19:56:23
I am currently trying to use boost::asio for some simple tcp networking for the first time, and I allready came across something I am not really sure how to deal with. As far as I understand io_service.run() method is basically a loop which runs until there is nothing more left to do, which means it will run until I release my little server object. Since I allready got some sort of mainloop set up, I would rather like to update the networking loop manually from there just for the sake of simplicity, and I think io_service.poll() would do what I want, sort of like this: void myApplication:

Why do we need to use boost::asio::io_service::work?

妖精的绣舞 提交于 2019-11-26 19:01:30
There is an example of using boost::asio. Why does this example use the boost::asio::io_service::work ? And why is srv.run (); not called to perform tasks in the threads? int main() { boost::asio::io_service srv; boost::asio::io_service::work work(srv); boost::thread_group thr_grp; thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv)); thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv)); srv.post(boost::bind(f1, 123)); srv.post(boost::bind(f1, 321)); //sync srv.post(boost::bind(f2, 456)); srv.post(boost::bind(f2, 654)); //sync srv.stop(); thr_grp.join(); }

How to set a timeout on blocking sockets in boost asio?

风流意气都作罢 提交于 2019-11-26 18:53:11
Is there a way to cancel a pending operation (without disconnect) or set a timeout for the boost library functions? I.e. I want to set a timeout on blocking socket in boost asio? socket.read_some(boost::asio::buffer(pData, maxSize), error_); Example: I want to read some from the socket, but I want to throw an error if 10 seconds have passed. Under Linux/BSD the timeout on I/O operations on sockets is directly supported by the operating system. The option can be enabled via setsocktopt() . I don't know if boost::asio provides a method for setting it or exposes the socket scriptor to allow you