boost-asio

boost shared_from_this<>()

天大地大妈咪最大 提交于 2019-12-03 11:37:54
问题 could someone summarize in a few succinct words how the boost shared_from_this<>() smart pointer should be used, particularly from the perspective of registering handlers in the io_service using the bind function. EDIT: Some of the responses have asked for more context. Basically, I'm looking for "gotchas", counter-intuitive behaviour people have observed using this mechanism. 回答1: The biggest "gotcha" I've run into is that it's illegal to call shared_from_this from the constructor. This

enumerating ipv4 and ipv6 address of my cards using boost asio

被刻印的时光 ゝ 提交于 2019-12-03 11:21:46
I am trying to enumerate ipv4 and ipv6 addresses of all the network cards(I have 2 cards) my pc. I am using the following code to do that. using boost::asio::ip::tcp; boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(boost::asio::ip::host_name(),""); tcp::resolver::iterator it=resolver.resolve(query); while(it!=tcp::resolver::iterator()) { boost::asio::ip::address addr=(it++)->endpoint().address(); if(addr.is_v6()) { std::cout<<"ipv6 address: "; } else std::cout<<"ipv4 address: "; std::cout<<addr.to_string()<<std::endl; } The code displays

boost asio async_connect success after close

不打扰是莪最后的温柔 提交于 2019-12-03 10:15:54
Single-threaded application. It happens not every time, only after 1.5 hours of high load. tcp::socket::async_connect tcp::socket::close (by deadline_timer) async_connect_handler gives success error_code (one of a million times), but socket is closed by(2). 99.999% of time it gives errno=125 (ECANCELED). Is it possible that socket implementation or boost asio somehow do this: async_connect async success posted to io_service close by timer async success handled by me, not affected by close Right now solved by saving state in my variables, ignoring accept success. Linux 2.6 (fedora). Boost 1.46

Boost Asio pattern with GUI and worker thread

落爺英雄遲暮 提交于 2019-12-03 10:14:27
问题 I would like to implement a Boost Asio pattern using a thread for GUI and a worker thread for some socket IO. The worker thread will use boost::asio::io_service to manage a socket client. All operations on sockets will be performed by the worker thread only. The GUI thread needs to send and receive messages from the worker thread. I can't exactly figure how to implement this pattern using Boost Asio. I've already implemented the socket communication in the standard Asio way (I call io_service

boost asio for sync server keeping TCP session open (with google proto buffers)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 10:01:04
问题 I currently have a very simple boost::asio server that sends a status update upon connecting (using google proto buffers): try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service,tcp::endpoint(tcp::v4(), 13)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); ... std::stringstream message; protoMsg.SerializeToOstream(&message); boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message.str()), ignored_error); } } catch

Having a hard time understanding a few concepts with Boost ASIO TCP with async_read and async_write

放肆的年华 提交于 2019-12-03 09:23:53
I'm having a hard time understand the correct way I should structure a tcp client when using async_read and async_write. The examples seem to do a async_read after connecting and then have async_write in the handler. In the case of my client and sever, when the client connects it needs to check a queue of messages to write and check to see if anything needs to be read. One of the things I'm having a hard time with is understanding how this would work asynchronously. What I envision is in the async_connect handler, the thread would call async_write if anything is in the sendQueue and call async

How to use Asio (standalone from Boost) in Android NDK?

╄→гoц情女王★ 提交于 2019-12-03 09:16:33
Asio (without Boost) is supposed to be usable with just the headers only right? By default, Asio is a header-only library. ( http://think-async.com ) I understand that internally Asio still depends on Boost. This is my setup. Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := gatelib LOCAL_SRC_FILES := gatelib.cpp LOCAL_C_INCLUDES += /cygdrive/l/asio-1.5.3/include LOCAL_C_INCLUDES += /cygdrive/l/boost/boost_1_49_0 include $(BUILD_SHARED_LIBRARY) Application.mk APP_STL := stlport_static APP_CFLAGS += -DBOOST_DATE_TIME_NO_LIB -DBOOST_REGEX_NO_LIB gatelib.cpp #include

Variable-size buffer for receiving UDP packets

落爺英雄遲暮 提交于 2019-12-03 08:57:12
I have an UDP socket that will receive some packets, of potentially different sizes, and I handle this asynchronously: socket.async_receive_from(boost::asio::buffer(buffer, 65536), senderEndpoint, handler); The problem here is that to handle the different sizes I have a big buffer, something that could be addressed with variable size buffers. To my understanding, when using async_receive_from , the handler is called with only one packet at a time, because the packet boundaries are preserved in UDP. So, is there a way to give an empty buffer to async_receive_from that Asio will grow to fit the

When do handlers for cancelled boost::asio handlers get to run?

混江龙づ霸主 提交于 2019-12-03 08:57:11
The boost docs say that cancelled async connect, send and receive finish immediately, and the handlers for cancelled operations will be passed the boost::asio::error::operation_aborted error. I would like to find out if the cancelled handler gets to run (and see the operation_aborted error) before other (non-cancelled, and newly scheduled) completion handlers run. Here is the timeline that concerns me: acceptHandler and readHandler are running on the same event loop and the same thread. time t0 - readHandler is running on oldConnectionSocket time t1 - acceptHandler runs time t2 - acceptHandler

Long-running / blocking operations in boost asio handlers

回眸只為那壹抹淺笑 提交于 2019-12-03 08:51:32
问题 Current Situation I implemented a TCP server using boost.asio which currently uses a single io_service object on which I call the run method from a single thread. So far the server was able to answer the requests of the clients immediately, since it had all necessary information in the memory (no long-running operations in the receive handler were necessary). Problem Now requirements have changed and I need to get some information out of a database (with ODBC) - which is basically a long