boost-asio

Linking Boost Library in Linux

ε祈祈猫儿з 提交于 2019-11-30 02:54:43
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!" << std::endl; return 0; } It can be found here on Boost's website. So, initially I just had: -I /usr

standard way to perform a clean shutdown with Boost.Asio

∥☆過路亽.° 提交于 2019-11-30 02:36:25
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 server object has been destroyed. I'm trying to avoid undefined behavior as specified by the C++

How to use std::string with asio::buffer()

家住魔仙堡 提交于 2019-11-30 01:37:36
问题 I get the following error message when I'm trying to use std::string with boost::asio::buffer: boost/asio/detail/consuming_buffers.hpp: In constructor 'boost::asio::detail::consuming_buffers< boost::asio::mutable_buffer, boost::asio::const_buffers_1 ::consuming_buffers(const boost::asio::const_buffers_1 &)': boost/asio/impl/read.hpp:140:25: instantiated from 'boost::asio::detail::read_op< boost::asio::basic_stream_socket, boost::asio::const_buffers_1 , boost::asio::detail::transfer_all_t ,

boost asio io_service.run()

我的梦境 提交于 2019-11-30 01:19:12
问题 I was just going over the asio chat server example. My question is about their usage of the io_service.run() function. The documentation for the io_service.run() function says: The run() function blocks until all work has finished and there are no more handlers to be dispatched, or until the io_service has been stopped. Multiple threads may call the run() function to set up a pool of threads from which the io_service may execute handlers. All threads that are waiting in the pool are

Boost asio io_service dispatch vs post

南笙酒味 提交于 2019-11-29 22:48:43
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. 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 it otherwise. So, it depends on the function calling post/dispatch was called, and if the given handler can

Using SSL sockets and non-SSL sockets simultaneously in Boost.Asio?

﹥>﹥吖頭↗ 提交于 2019-11-29 22:21:19
I'm in the process of converting a library to Boost.Asio (which has worked very well so far), but I've hit something of a stumbling block with regards to a design decision. Boost.Asio provides support for SSL, but a boost::asio::ssl::stream<boost::asio::ip::tcp::socket> type must be used for the socket. My library has the option of connecting to SSL servers or connecting normally, so I've made a class with two sockets like this: class client : public boost::enable_shared_from_this<client> { public: client(boost::asio::io_service & io_service, boost::asio::ssl::context & context) : socket_(io

boost::asio::ip::tcp::socket is connected?

爱⌒轻易说出口 提交于 2019-11-29 21:48:27
I want to verify the connection status before performing read/write operations. Is there a way to make an isConnect() method? I saw this , but it seems "ugly". I have tested is_open() function as well, but it doesn't have the expected behavior. TCP is meant to be robust in the face of a harsh network; even though TCP provides what looks like a persistent end-to-end connection, it's all just a lie, each packet is really just a unique, unreliable datagram. The connections are really just virtual conduits created with a little state tracked at each end of the connection (Source and destination

Pros & cons of a callback (std::function/std::bind) vs an interface (abstract class)

青春壹個敷衍的年華 提交于 2019-11-29 20:09:59
I'm creating a server application in C++11 using Boost.Asio. I've created a class, Server , which takes care of accepting new connections. It's basically just: void Server::Accept() { socket_.reset(new boost::asio::ip::tcp::socket(*io_service_)); acceptor_.async_accept(*socket_, boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error)); } void Server::HandleAccept(const boost::system::error_code& error) { if (!error) { // TODO } else { TRACE_ERROR("Server::HandleAccept: Error!"); } Accept(); } I've found two ways (I'm sure there are more) to "fix" the TODO comment, i.e. to

Asynchronously waiting until a socket is available for reading/writing in Asio

只谈情不闲聊 提交于 2019-11-29 19:10:43
问题 I want to do the following with Boost Asio. I have a socket and I want to register a callback to be called when data is available for reading/writing on the socket, but I don't want it to actually do the reading/writing. Basically, what I need is similar to async_read_some / async_write_some , except that the actual reading and writing is not done. I need this because I'm using an external library with its own read and write function that require a socket descriptor as an input parameter and

HOWTO: post messages between threads with Boost::asio?

元气小坏坏 提交于 2019-11-29 18:09:42
Sorry for my english I've searched around, but did not get an answer to this question: I have a windows application project, using boost thread libraries. I want to post messages(or, invoke callbacks) from a worker's thread to the main UI thread. I studied the samples in boost::asio, all of them used in a blocked main thread, but my UI thread is working asynchronous. Would you please help me? thanks a lot! Igor R. Since the UI thread has its own message loop, you can't call in its context the blocking io_service::run() function. What you can do is to interleave a polling UI-related method with