Boost.ASIO C++ 20 coroutine equivalent of PPL task_completion_event or JavaScript Promise.resolve?
I'm trying to write a simple ASIO send/receiver that uses message queueing. The goal is to have a number of virtual streams over a single TCP connection. This is pretty simple using callbacks, but I can't figure out the coroutines alternative. Here is my simplified sample code. Note that I'm using asio::experimental::promise the same way I would use std::promise which obviously doesn't work, but it illustrates what I'm trying to do. What is the simplest solution to my problem?
#pragma once
#include "asio.hpp"
#include "asio/co_spawn.hpp"
#include "asio/detached.hpp"
#include "asio/experimental/co_composed.hpp"
#include "asio/experimental/promise.hpp"
#include "asio/use_awaitable.hpp"
#include <coroutine>
#include <cstdint>
#include <map>
#include <memory>
#include <queue>
struct Message {
uint64_t id;
uint64_t streamId;
uint64_t priority;
};
class NetworkConnection {
public:
NetworkConnection(asio::io_context &ioContext)
: ioContext_(ioContext), sendTimer_(ioContext_),
receiveTimer_(ioContext_){};
asio::awaitable
I'm trying to write a simple ASIO send/receiver that uses message queueing. The goal is to have a number of virtual streams over a single TCP connection. This is pretty simple using callbacks, but I can't figure out the coroutines alternative. Here is my simplified sample code. Note that I'm using asio::experimental::promise the same way I would use std::promise which obviously doesn't work, but it illustrates what I'm trying to do. What is the simplest solution to my problem?
Have you considered utilizing asio::experimental::basic_channel? https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio/reference/experime... By the way, there is no synchronization mechanism in Asio akin to a future/promise pair, if you're open to external solutions, you might want to explore these options: https://github.com/ashtum/saf https://github.com/ashtum/oneshot Regards, Mohammad
participants (2)
-
Mohammad Nejati [ashtum]
-
Willem Mitchell