boost-asio

Using ZeroMQ together with Boost::ASIO

天涯浪子 提交于 2019-11-28 17:16:19
I've got a C++ application that is using ZeroMQ for some messaging. But it also has to provide a SGCI connection for an AJAX / Comet based web service. For this I need a normal TCP socket. I could do that by normal Posix sockets, but to stay cross platform portable and make my life easier (I hope...) I was thinking of using Boost::ASIO. But now I have the clash of ZMQ wanting to use it's own zmq_poll() and ASIO it's io_service.run() ... Is there a way to get ASIO to work together with the 0MQ zmq_poll() ? Or is there an other recommended way to achieve such a setup? Note: I could solve that by

How to get IP address of boost::asio::ip::tcp::socket?

主宰稳场 提交于 2019-11-28 17:09:56
I'm writing a server in C++ using Boost ASIO library. I'd like to get the string representation of client IP to be shown in my server's logs. Does anyone know how to do it? The socket has a function that will retrieve the remote endpoint. I'd give this (long-ish) chain of commands a go, they should retrieve the string representation of the remote end IP address: asio::ip::tcp::socket socket(io_service); // Do all your accepting and other stuff here. asio::ip::tcp::endpoint remote_ep = socket.remote_endpoint(); asio::ip::address remote_ad = remote_ep.address(); std::string s = remote_ad.to

boost::asio::socket thread safety

淺唱寂寞╮ 提交于 2019-11-28 17:06:15
( This is a simplified version of my original question ) I have several threads that write to a boost asio socket. This seems to work very well, with no problems. The documentation says a shared socket is not thread safe( here , way down at the bottom ) so I am wondering if I should protect the socket with mutex, or something. This question insists that protection is necessary, but gives no advice on how to do so. All the answers to my original question also insisted that what I was doing dangerous, and most urged me to replace my writes with async_writes or even more complicated things.

using multiple io_service objects

你离开我真会死。 提交于 2019-11-28 16:55:22
I have my application in which listen and process messages from both internet sockets and unix domain sockets. Now I need to add SSL to the internet sockets, I was using a single io_service object for all the sockets in the application. It seems now I need to add separate io_service objects for network sockets and unix domain sockets. I don't have any threads in my application and I use async_send and async_recieve and async_accept to process data and connections. Please point me to any examples using multiple io_service objects with async handlers. The question has a degree of uncertainty as

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

我的未来我决定 提交于 2019-11-28 15:59:12
问题 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:

Should the exception thrown by boost::asio::io_service::run() be caught?

心已入冬 提交于 2019-11-28 14:30:32
boost::asio::io_service::run() throws a boost::system::system_error exception in case of error. Should I handle this exception? If so, how? my main.cpp code is something like this: main() { boost::asio::io_service queue; boost::asio::io_service::work work(queue); { // set some handlers... **queue.run();** } // join some workers... return 0; } Yes. It is documented that exceptions thrown from completion handlers are propagated. So you need to handle them as appropriate for your application. In many cases, this would be looping and repeating the run() until it exits without an error. In our code

boost::asio::async_read ends without fulfilling the completion condition

∥☆過路亽.° 提交于 2019-11-28 14:30:32
On Windows, I am observing that if an async_read operation successfully completes on a serial port, and I immediately initiate another async_read operation to read n bytes, the second async_read operation immediately completes unexpectedly with success and 0 bytes transferred. after the second async_read operation, if a third async_read operation is initiated to read n bytes, then it will complete with success and n bytes transferred // where buffer_size(buffer) and n are both greater than 1 async_read(serial, buffer, transfer_exactly(n), [](error, bytes_transferred) { // completes with error

Gracefully terminate a Boost Asio based Windows console application

删除回忆录丶 提交于 2019-11-28 12:45:39
I am working on a boost.asio based HTTP server. It is supposed to be stopped externally. We use asio signal handling, and it works well for ctrl-c, but does not handle WM_CLOSE, so there is no straightforward way to gracefully close the application externally, e.g. via taskkill. Terminating the process forcibly is not an option. Is there a known approach to this? Update Just use any IPC method you would "normally" use Write a simple process control utility that uses e.g. named_condition to signal your asio process to shutdown. Note that named_codition is somewhat equivalent to a Win32 Named

How to check if socket is closed in Boost.Asio?

一笑奈何 提交于 2019-11-28 12:21:15
What is the easiest way to check if a socket was closed on the remote side of the connection? socket::is_open() returns true even if it is closed on the remote side (I'm using boost::asio::ip::tcp::socket ). I could try to read from the stream and see if it succeeds, but I'd have to change the logic of my program to make it work this way (I do not want data to be extracted from the stream at the point of the check). If the connection has been cleanly closed by the peer you should get an EOF while reading. Otherwise I generally ping in order to figure out if the connection is really alive. Just

How to set error_code to asio::yield_context

若如初见. 提交于 2019-11-28 11:44:47
I'd like to create an asynchronous function which takes as it's last argument boost::asio::yield_context. E.g.: int async_meaning_of_life(asio::yield_context yield); I'd also like to be consistent with how Asio returns error codes. That is, if the user does: int result = async_meaning_of_life(yield); and the function fails, then it throws the system_error exception. But if the user does: boost::error_code ec; int result = async_meaning_of_life(yield[ec]); Then - instead of throwing - the error is returned in ec . The problem is that when implementing the function, I can't seem to find a clean