1
0
Fork 0
mirror of https://gitlab.com/niansa/llama_nds.git synced 2025-03-06 20:53:28 +01:00
llama_nds/AsyncManager.hpp
2023-04-06 09:07:53 +02:00

59 lines
1.4 KiB
C++

#ifndef _ASYNCMANAGER_HPP
#define _ASYNCMANAGER_HPP
#include "Runtime.hpp"
#include <unordered_map>
#include <memory>
#include "basic-coro/AwaitableTask.hpp"
#include "basic-coro/SingleEvent.hpp"
class Runtime;
class AsyncManager {
public:
using SockError = bool;
using SockFuture = basiccoro::SingleEvent<SockError>;
using SockFutureUnique = std::unique_ptr<SockFuture>;
using SockFutureMap = std::unordered_multimap<int, SockFutureUnique>;
private:
Runtime& runtime;
SockFutureMap sockReads;
SockFutureMap sockWrites;
bool stopping = false;
static
void cleanFutureMap(SockFutureMap&);
public:
AsyncManager(Runtime& runtime) : runtime(runtime) {}
AsyncManager(AsyncManager&) = delete;
AsyncManager(const AsyncManager&) = delete;
AsyncManager(AsyncManager&&) = delete;
void run();
void stop() {
stopping = true;
}
basiccoro::AwaitableTask<SockError> waitRead(int fd) {
auto event = std::make_unique<SockFuture>();
auto eventPtr = event.get();
sockReads.emplace(fd, std::move(event));
co_return co_await *eventPtr;
}
basiccoro::AwaitableTask<SockError> waitWrite(int fd) {
auto event = std::make_unique<SockFuture>();
auto eventPtr = event.get();
sockWrites.emplace(fd, std::move(event));
co_return co_await *eventPtr;
}
auto& getRuntime() const {
return runtime;
}
};
#endif