boost-asio

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

匆匆过客 提交于 2019-11-27 14:36:44
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? 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 async. operation is in progress), the connection instance is alive as well. 来源: https://stackoverflow.com

How to send ostream via boost sockets in C++?

拟墨画扇 提交于 2019-11-27 14:13:36
问题 I am facing some issues with my inter-process communication using protobuf. Protobuf allows a set of serialization formats: SerializeToArray(void * data, int size) : bool SerializeToCodedStream(google::protobuf::io::CodeOutputStream * output) : bool SerializeToFileDescriptor(int file_descriptor) : bool SerializeToOstream(ostream * output) My problem is, I have no clue how to use it with the boost asio sockets I am using, as I implemented them to send strings: boost::asio::write(socket, boost:

How to detect when a boost tcp socket disconnects

我怕爱的太早我们不能终老 提交于 2019-11-27 13:52:45
问题 Suppose I have a socket: std::shared_ptr<tcp::socket> socket( new tcp::socket(acceptor.get_io_service()) ); acceptor.async_accept( *socket, std::bind( handleAccept, this, std::placeholders::_1, socket, std::ref(acceptor)) ); And I store a weak_ptr to the said socket in a container. I need this because I want to allow clients to request for a list of other clients, so they can send messages to each other. clients_.insert(socket); // pseudocode Then I run some async operations socket->async

SO_RCVTIME and SO_RCVTIMEO not affecting Boost.Asio operations

一世执手 提交于 2019-11-27 13:22:00
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 timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; //setsockopt(ps.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)

Cancelling boost asio deadline timer safely

你离开我真会死。 提交于 2019-11-27 13:21:31
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); std::atomic<bool> started; void handle_timeout(const boost::system::error_code& ec) { if (!ec) { started

Is ip::tcp::socket.close() thread safe?

自古美人都是妖i 提交于 2019-11-27 13:20:18
问题 If there is a async_read on the socket ongoing, there should be a internal thread of io_service checking the status of the socket. Is it safe to call socket.close() from another thread (maybe when it is running a separate handler of the io_service )? I mean even I can guarantee that my handlers will not use the asio socket concurrently, is it good enough (when taking the internal threads of io_service into consideration)? Update: I am using async_read in a stackful coroutine. Quite similar to

Chaining asynchronous Lambdas with Boost.Asio?

若如初见. 提交于 2019-11-27 12:23:05
问题 I find myself writing code that basically looks like this: using boost::system::error_code; socket.async_connect(endpoint, [&](error_code Error) { if (Error) { print_error(Error); return; } // Read header socket.async_read(socket, somebuffer, [&](error_code Error, std::size_t N) { if (Error) { print_error(Error); return; } // Read actual data socket.async_read(socket, somebuffer, [&](error_code Error, std::size_t N) { // Same here... }); }); }; So basically I'm nesting callbacks in callbacks

When to use `asio_handler_invoke`?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 12:13:39
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 asio_handler_invoke ) and achieve an identical result: template <typename Arg1> void operator()(Arg1 arg1

Async wait on file descriptor using Boost Asio

吃可爱长大的小学妹 提交于 2019-11-27 11:23:00
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_descriptor::bytes_readable command(true); descriptor->io_control(command); std::size_t bytes_readable =

Serialize and send a data structure using Boost?

▼魔方 西西 提交于 2019-11-27 11:08:56
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 specifically for writing/reading to a file, not to a socket using boost::asio. I'm pretty sure I've got the