c++ ASIO - equivalent of C#'s TaskCompletionSource

Viewed 62

I'm attempting to use asio's coroutines in C++ to have a TCP client that listens for responses to requests from the server (like RPC, but in TCP). I am using the standalone version, rather than with Boost. I have working code on the server side (written in C#) using a TaskCompletionSource. Basic C# pseudocode:

TaskCompletionSource<Packet> packetAwaiter = new();
SocketHandler.RegisterAwaiter(packetAwaiter);
m_Socket.Send(requestPacket);
Packet result = await packetAwaiter.Task;

Where the SocketHandler is doing something like this (C# pseudocode):

var length = await Socket.ReceiveAsync(buffer, SocketFlags.None, linkedTokenSource.Token);
Packet packet = GetPacketFromBuffer(buffer);
TaskCompletionSource<Packet> awaiter = GetPacketAwaiter(...);
awaiter.SetResult(packet);

On the C++ side, I have ASIO working on connecting to the server, but I have no idea how to translate the idea of the resumable awaiter. I'd like to do something like this:

asio::ip::tcp::socket serverSocket {...};
std::future<packet> packetAwaiter = ...;
serverSocket.send(asio::buffer{...});
packet result = co_await packetAwaiter;

Are there any source code listings or similar that could show me what is supported? I'm using VS2022 on Windows with vcpkg, if that helps.

Thank you!

0 Answers
Related