Add user events support
This commit is contained in:
parent
8e9a97d62b
commit
0d797e04b8
2 changed files with 22 additions and 9 deletions
|
@ -118,6 +118,13 @@ struct IrcClient {
|
|||
|
||||
co_await boost::asio::async_write(this->socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable);
|
||||
};
|
||||
template <typename Event>
|
||||
auto SendEvent(Event&& event) -> boost::asio::awaitable<void> {
|
||||
return [&]<typename... Ids, typename... FFs>(utempl::TypeList<std::tuple<Function<Ids, FFs>...>>) {
|
||||
constexpr auto i = utempl::Find<std::decay_t<Event>>(utempl::kTypeList<Ids...>);
|
||||
return std::get<i>(this->callbacks).f(*this, std::forward<Event>(event));
|
||||
}(utempl::kType<Fs>);
|
||||
};
|
||||
auto Pong(std::string_view buff) -> boost::asio::awaitable<void> {
|
||||
const auto pong = std::format("PONG {}", buff);
|
||||
|
||||
|
@ -131,20 +138,18 @@ struct IrcClient {
|
|||
std::string buff;
|
||||
for(;;) {
|
||||
auto messageStr = co_await yail::ReadLine(this->socket, buff);
|
||||
|
||||
std::println("Readed message: {}", messageStr);
|
||||
co_await std::apply( // NOLINTNEXTLINE
|
||||
[&]<typename... Ids, typename... FFs>(Function<Ids, FFs>&... fs) -> boost::asio::awaitable<void> {
|
||||
auto self = this;
|
||||
auto& str = messageStr;
|
||||
// clang-format off
|
||||
( // NOLINTNEXTLINE
|
||||
co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable<void> {
|
||||
(co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable<void> { // NOLINT
|
||||
if constexpr(requires {Ids::TryParse(*self, messageStr);}) {
|
||||
if(auto value = Ids::TryParse(*self, messageStr)) {
|
||||
co_await fs.f(*self, *value);
|
||||
}
|
||||
}(self, str, fs),
|
||||
...);
|
||||
co_await fs.f(*self, std::move(*value));
|
||||
};
|
||||
};
|
||||
}(self, str, fs), ...);
|
||||
// clang-format on
|
||||
},
|
||||
this->callbacks);
|
||||
|
|
8
main.cpp
8
main.cpp
|
@ -2,6 +2,8 @@
|
|||
#include <utempl/tuple.hpp>
|
||||
#include <yail/yail.hpp>
|
||||
|
||||
struct UserEvent {};
|
||||
|
||||
auto Test() -> boost::asio::awaitable<void> {
|
||||
try {
|
||||
auto executor = co_await boost::asio::this_coro::executor;
|
||||
|
@ -14,6 +16,11 @@ auto Test() -> boost::asio::awaitable<void> {
|
|||
.server = "irc.ilita.i2p"}
|
||||
.AddState<std::unordered_set<yail::User, yail::User::Hash>, "ruUsers">()
|
||||
// NOLINTNEXTLINE
|
||||
.With([](auto& self, UserEvent event) -> boost::asio::awaitable<void> {
|
||||
std::println("User event!");
|
||||
co_return;
|
||||
})
|
||||
// NOLINTNEXTLINE
|
||||
.With([](auto& self, yail::event::Ping ping) -> boost::asio::awaitable<void> {
|
||||
co_await self.Pong(ping.data);
|
||||
})
|
||||
|
@ -21,6 +28,7 @@ auto Test() -> boost::asio::awaitable<void> {
|
|||
.With([](auto& self, yail::event::Welcome message) -> boost::asio::awaitable<void> {
|
||||
std::println("Welcome: {}", message.message);
|
||||
co_await self.Join("#ru");
|
||||
co_await self.SendEvent(UserEvent{});
|
||||
co_await self.SendMessage("#ru", "Hello from yet another irc library");
|
||||
})
|
||||
// NOLINTNEXTLINE
|
||||
|
|
Loading…
Reference in a new issue