boost-asio

Linking Boost Library in Linux

人走茶凉 提交于 2019-11-28 23:48:08
问题 I am trying to build a project using Boost's Asio and I am having some trouble. Initially, I tried to build the project without any additional libraries since everything is supposedly in the header files. The program I am trying to build looks like this: #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.wait(); std::cout << "Hello, world

standard way to perform a clean shutdown with Boost.Asio

喜夏-厌秋 提交于 2019-11-28 23:37:41
问题 I'm writing a cross-platform server program in C++ using Boost.Asio. Following the HTTP Server example on this page, I'd like to handle a user termination request without using implementation-specific APIs. I've initially attempted to use the standard C signal library, but have been unable to find a design pattern suitable for Asio. The Windows example's design seems to resemble the signal library closest, but there's a race condition where the console ctrl handler could be called after the

boost::asio io_service thread pool

泄露秘密 提交于 2019-11-28 23:24:33
What's the proper usage of settings up a thread pool for io_service? These 2 statements from the documentation are throwing me off: io_service::run A normal exit from the run() function implies that the io_service object is stopped (the stopped() function returns true). Subsequent calls to run(), run_one(), poll() or poll_one() will return immediately unless there is a prior call to reset(). io_service::reset This function must be called prior to any second or later set of invocations of the run(), run_one(), poll() or poll_one() functions when a previous invocation of these functions returned

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

守給你的承諾、 提交于 2019-11-28 22:07:51
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::asio::buffer(message), boost::asio::transfer_all(), ignored_error); But I would like to send the

Should std::bind be compatible with boost::asio?

不想你离开。 提交于 2019-11-28 22:02:38
问题 I am trying to adapt one of the boost::asio examples to use c++11 / TR1 libraries where possible. The original code looks like this: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept(new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } If I replace boost::bind with std::bind as follows: void start_accept() { tcp_connection::pointer

How to detect when a boost tcp socket disconnects

半腔热情 提交于 2019-11-28 21:29:40
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_receive( boost::asio::buffer(&(*header), sizeof(Header)) , 0 , std::bind(handleReceiveHeader, this, std:

Boost asio io_service dispatch vs post

眉间皱痕 提交于 2019-11-28 19:43:11
问题 Can anyone tell me the difference between io_service dispatch and post? It was not clear to me what is more suitable for my problem. I need to invoke a handler inside another handler and I don't know what invoker to use. 回答1: Well, it depends on the context of the call, i.e. is it run from within the io_service or without: post will not call the function directly, ever, but postpone the call. dispatch will call it rightaway if the dispatch-caller was called from io_service itself, but queue

Chaining asynchronous Lambdas with Boost.Asio?

懵懂的女人 提交于 2019-11-28 19:32:45
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 in callbacks, while the logic is simple and "linear". Is there a more elegant way of writing this, so

Boost::asio - how to interrupt a blocked tcp server thread?

霸气de小男生 提交于 2019-11-28 18:16:56
I'm working on a multithreaded application in which one thread acts as a tcp server which receives commands from a client. The thread uses a Boost socket and acceptor to wait for a client to connect, receives a command from the client, passes the command to the rest of the application, then waits again. Here's the code: void ServerThreadFunc() { using boost::asio::ip::tcp; boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port_no)); for (;;) { // listen for command connection tcp::socket socket(io_service); acceptor.accept(socket); // connected;

boost::asio async server design

馋奶兔 提交于 2019-11-28 17:59:40
Currently I'm using design when server reads first 4 bytes of stream then read N bytes after header decoding. But I found that time between first async_read and second read is 3-4 ms. I just printed in console timestamp from callbacks for measuring. I sent 10 bytes of data in total. Why it takes so much time to read? I running it in debug mode but I think that 1 connection for debug is not so much to have a 3 ms delay between reads from socket. Maybe I need another approach to cut TCP stream on "packets"? UPDATE: I post some code here void parseHeader(const boost::system::error_code& error) {