boost-asio

Boost::Asio synchronous client with timeout

陌路散爱 提交于 2019-11-26 18:37:47
问题 I´m trying to build a synchronous FTP client code with timeout using a thread as the timeout control. The thread will be started on every transaction and will close the socket in case of timeout - that will force the syncronous call to return with error. So here is my code: #include <cstdlib> #include <cstring> #include <iostream> #include <thread> #include <chrono> #include <boost/asio.hpp> #define TIMEOUT_SECONDS 5 #define MAX_MESSAGE_SIZE 4096 using boost::asio::ip::tcp; enum { max_length

Cancelling boost asio deadline timer safely

混江龙づ霸主 提交于 2019-11-26 18:17:30
问题 I'm trying to cancel a boost::asio::basic_waitable_timer<std::chrono::steady_clock> safely. According to this answer, this code should do that work: timer.get_io_service().post([&]{timer.cancel();}) I'm afraid it doesn't work for me. Am I doing something wrong? This is my code: #include <iostream> #include "boost/asio.hpp" #include <chrono> #include <thread> #include <random> boost::asio::io_service io_service; boost::asio::basic_waitable_timer<std::chrono::steady_clock> timer(io_service);

Serialize and send a data structure using Boost?

守給你的承諾、 提交于 2019-11-26 17:59:26
问题 I have a data structure that looks like this: typedef struct { unsigned short m_short1; unsigned short m_short2; unsigned char m_character; } MyDataType; I want to use boost::serialization to serialize this data structure, then use boost::asio to transmit it via TCP/IP, then have another application receive the data and de-serialize it using the same boost libraries. I'm trying to following boost::serialization tutorial, (as some other SO questions have suggested) but the example is

Boost.Asio as header-only

 ̄綄美尐妖づ 提交于 2019-11-26 16:52:49
问题 I want to use ASIO library from Boost in my project. Its doc say it can be header-only if regex is not used and SSL not used. However, running bcp for asio pulls a very many libraies some of which are with sources so need compiling, bjam etc. Can I somehow use ASIO in project as only headers, without libs/source? I only need ASIO, not other part of Boost. EDIT: ASIO want Boost.System which has a lib to link - can this dependency not be so that I can use header only ASIO? 回答1: AFAIK you can

What's the reason of using auto self(shared_from_this()) variable in lambda function?

余生颓废 提交于 2019-11-26 16:51:45
问题 I read the boost asio http server example code (see http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/http/server/connection.cpp) and find the auto self(shared_from_this()); variable is been used in the capture scope ( [this, self] ). But the self variable is not been used in the lambda function. Then what's the benefit of doing so? 回答1: This is done in order to make sure that connection object outlives the asynchronous operation: as long as the lambda is alive (i.e. the

SO_RCVTIME and SO_RCVTIMEO not affecting Boost.Asio operations

可紊 提交于 2019-11-26 16:19:11
问题 Below is my code boost::asio::io_service io; boost::asio::ip::tcp::acceptor::reuse_address option(true); boost::asio::ip::tcp::acceptor accept(io); boost::asio::ip::tcp::resolver resolver(io); boost::asio::ip::tcp::resolver::query query("0.0.0.0", "8080"); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); accept.open(endpoint.protocol()); accept.set_option(option); accept.bind(endpoint); accept.listen(30); boost::asio::ip::tcp::socket ps(io); accept.accept(ps); struct

When to use `asio_handler_invoke`?

谁说胖子不能爱 提交于 2019-11-26 15:58:47
问题 Question When is it necessary to use asio_handler_invoke to achieve something that cannot be done by simply wrapping the handler? A canonical example that demonstrates a case where asio_handler_invoke is required would be ideal. Background The boost asio docs contain an example of how to use asio_handler_invoke here, but I don't think it is a compelling example of why you would use the invocation handler. In that example it appears you could make changes like the following (and remove the

Async wait on file descriptor using Boost Asio

时光毁灭记忆、已成空白 提交于 2019-11-26 15:28:20
问题 I'm trying to integrate D-Bus with my boost::asio application. D-Bus has an API that enumerates a set of Unix file descriptors (mainly sockets but could also be FIFOs) to be watched. When those descriptors have something to be read I should inform the D-Bus API so it can read them and do it's thing. Currently I'm doing this: using boost::asio::posix::stream_descriptor; void read_handle(stream_descriptor* desc, const boost::system::error_code& ec, std::size_t bytes_read) { if (!ec) { stream

c++ work queues with blocking

天涯浪子 提交于 2019-11-26 14:50:02
问题 This question should be a little simpler than my last few. I've implemented the following work queue in my program: Pool.h: // tpool class // It's always closed. :glasses: #ifndef __POOL_H #define __POOL_H class tpool { public: tpool( std::size_t tpool_size ); ~tpool(); template< typename Task > void run_task( Task task ){ boost::unique_lock< boost::mutex > lock( mutex_ ); if( 0 < available_ ) { --available_; io_service_.post( boost::bind( &tpool::wrap_task, this, boost::function< void() > (

Some clarification needed about synchronous versus asynchronous asio operations

丶灬走出姿态 提交于 2019-11-26 12:02:29
问题 As far as I know, the main difference between synchronous and asynchronous operations. I.e. write() or read() vs async_write() and async_read() is that the former, don\'t return until the operation finish -or error-, and the last ones, returns immediately. Due the fact that the asynchronous operations are controlled by an io_service.run() that does not finish until the controlled operations has finalized. It seems to me that in sequential operations as those involved in TCP/IP connections