boost-asio

Matching boost::deadline_timer callbacks to corresponding wait_async

梦想与她 提交于 2019-12-02 11:54:33
问题 Consider this short code snippet where one boost::deadline_timer interrupts another: #include <iostream> #include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/asio.hpp> static boost::asio::io_service io; boost::asio::deadline_timer timer1(io); boost::asio::deadline_timer timer2(io); static void timer1_handler1(const boost::system::error_code& error) { std::cout << __PRETTY_FUNCTION__ << " time:" << time(0) << " error:" << error.message() << " expect:Operation canceled." <<

Implementing an event timer using boost::asio

拜拜、爱过 提交于 2019-12-02 11:40:28
问题 The sample code looks long, but actually it's not so complicated :-) What I'm trying to do is, when a user calls EventTimer.Start(), it will execute the callback handler (which is passed into the ctor) every interval milliseconds for repeatCount times. You just need to look at the function EventTimer::Stop() #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/thread.hpp> #include <boost/function.hpp> #include <boost/date_time/posix_time

ASIO getting a tcp endpoint directly from an asynchronous resolve

喜夏-厌秋 提交于 2019-12-02 11:32:33
问题 I'm looking to use the ASIO standalone library (not Boost ASIO), I am trying to set up a client to connect to a server on a specific port. I saw in the porthopper example that it is possible to get the endpoint without having to deal with an iterator. asio::io_service io_service; // Determine the location of the server. tcp::resolver resolver(io_service); tcp::resolver::query query(host_name, port); tcp::endpoint remote_endpoint = *resolver.resolve(query); I am trying to do the resolve of the

boost:asio::read or boost:asio::async_read with timeout

做~自己de王妃 提交于 2019-12-02 09:38:00
Yes. I know there have been a few questions around this time_out in boost::asio . My problem might to too simple for the asio guys to solve here. I am using boost::asio on TCP protocol to read data over a network continuously in a loop as fast as I can. Following function ReadData() gets called continuously from a worker std::thread in a while loop. std::size_t ReadData(std::vector<unsigned char> & buffer, unsigned int size_to_read) { boost::system::error_code error_code; buffer.resize(size_to_read); // Receive body std::size_t bytes_read = boost::asio::read(*m_socket, boost::asio::buffer

Does boost asio io_service guarantee execution of two parallel call chains?

若如初见. 提交于 2019-12-02 08:30:31
In my program, using boost asio io_service, I want to have two parallel call chains. Two endless loops writing and reading to two usb ports. But does boost asio io_service guarantee execution of two parallel call chains? Look at this minimal example: #include <boost/asio/io_service.hpp> #include <functional> class Chain { public: Chain(boost::asio::io_service &io_service, const std::string &message) : io_service(io_service) , message(message) {} void call() { std::cout << message << std::endl; io_service.post(std::bind(&Chain::call, this)); } private: boost::asio::io_service &io_service; std:

program crash with boost::asio::spawn and socket::async_receive_from

家住魔仙堡 提交于 2019-12-02 08:20:43
int main() { boost::asio::io_service io_service; Worker ob1(&io_service); ob1.AsyncRead(); io_service.run(); } void Worker::AsyncRead() { socket.async_receive_from(buffer,endpoint, handler); } void handler (const boost::system::error_code& error, size_t bytes_received) { if(!error) handleData(); AsyncRead(); } Now this works perfectly fine. But if I use boost::Spawn() to do asyncRead() it crashes. void work(boost::asio::io_service* io_service) { auto ob1 = std::make_shared<Worker>(io_service); boost::asio::spawn(*io_service, [ob1]( boost::asio::yield_context yield) { ob1->AsyncRead(); }); }

Correct usage of asio::io_service::strand?

我怕爱的太早我们不能终老 提交于 2019-12-02 07:54:33
Here is my code: void client_connection::serve() { asio::async_read(this->socket_, asio::buffer(&buffer_, buffer_.size()), // predicate/condition (do I wrap this?) std::bind(&client_connection::handle_read_predicate, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2), // handler this->strand_.wrap(std::bind(&client_connection::handle_read, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2))); } std::size_t client_connection::handle_read_predicate(const asio::error_code& error, std::size_t bytes_) { // useless flawed function, for now // std::cout <<

Which io_context does std::boost::asio::post / dispatch use?

 ̄綄美尐妖づ 提交于 2019-12-02 07:48:16
While using boost::asio 1.66 I read in the documentation that boost::asio::io_context::post is deprecated for boost::asio::post , same for boost::asio::io_context::dispatch . Because before they where member functions of the io_context before, and of course the handler needs to be executed in the context of some io_context i.e. executor my question is: How does boost::asio::io_context::post simplest overload know which io_context i.e. executor to use? The documentation of template< typename CompletionToken> DEDUCED post(CompletionToken && token); states that Obtains the handler's associated

Read child process stdout in a separate thread with BOOST process

限于喜欢 提交于 2019-12-02 07:38:49
问题 I have a main program that uses boost process library to spawn a child process that prints Hello World ! on its stdout every 5 seconds. I would like to read/monitor the stdout of the child process in the main process when it becomes available along with performing other operations within the main program. I have tried out the examples for boost asynchronous IO (http://www.boost.org/doc/libs/1_66_0/doc/html/boost_process/tutorial.html) but all these seem to block the main program until the

Alternative to missing method in last version of Boost asio library

本秂侑毒 提交于 2019-12-02 07:34:41
问题 Some years ago, I wrote a email client using Boost asio library. There are a abstract class ICON with four subclasses. POP3conN to flat POP3 communications POP3conS to secure POP3 communications SMTPconN to flat SMTP communications SMTPconS to secure SMTP communications ICON has a member boost::asio::ip::tcp::socket socket_ and two virtual procedures, defined in echa subclass: void SMTPconN::run() { socket_.get_io_service().run(); } void SMTPconN::reset() { socket_.get_io_service().reset(); }