Add send AnyMessage event

This commit is contained in:
sha512sum 2024-10-24 09:50:00 +00:00
parent e6fe5b3392
commit 9d7610a23c
2 changed files with 21 additions and 6 deletions

View file

@ -34,6 +34,10 @@ struct Ping {
} }
}; };
struct AnyMessage {
std::string_view data;
};
} // namespace event } // namespace event
template <typename F> template <typename F>
@ -124,12 +128,15 @@ struct IrcClient {
co_await boost::asio::async_write(this->socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable); co_await boost::asio::async_write(this->socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable);
this->nick = std::move(nick); this->nick = std::move(nick);
}; };
template <typename Event>
auto SendEvent(Event&& event) -> boost::asio::awaitable<void> { static constexpr auto kHandledEvents = []<typename... Ids, typename... FFs>(utempl::TypeList<std::tuple<Function<Ids, FFs>...>>) {
return [&]<typename... Ids, typename... FFs>(utempl::TypeList<std::tuple<Function<Ids, FFs>...>>) { return utempl::kTypeList<Ids...>;
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>); }(utempl::kType<Fs>);
template <typename Event>
auto SendEvent(Event&& event)
-> decltype(std::get<utempl::Find<std::decay_t<Event>>(kHandledEvents)>(this->callbacks).f(*this, std::forward<Event>(event))) {
return std::get<utempl::Find<std::decay_t<Event>>(kHandledEvents)>(this->callbacks).f(*this, std::forward<Event>(event));
}; };
auto Pong(std::string_view buff) -> boost::asio::awaitable<void> { auto Pong(std::string_view buff) -> boost::asio::awaitable<void> {
const auto pong = std::format("PONG {}", buff); const auto pong = std::format("PONG {}", buff);
@ -148,6 +155,9 @@ struct IrcClient {
[&]<typename... Ids, typename... FFs>(Function<Ids, FFs>&... fs) -> boost::asio::awaitable<void> { [&]<typename... Ids, typename... FFs>(Function<Ids, FFs>&... fs) -> boost::asio::awaitable<void> {
auto self = this; auto self = this;
auto& str = messageStr; auto& str = messageStr;
if constexpr(requires { self->SendEvent(event::AnyMessage{messageStr}); }) {
co_await self->SendEvent(event::AnyMessage{messageStr});
};
// clang-format off // clang-format off
(co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable<void> { // NOLINT (co_await [](auto self, auto& messageStr, auto& fs) -> boost::asio::awaitable<void> { // NOLINT
if constexpr(requires {Ids::TryParse(*self, messageStr);}) { if constexpr(requires {Ids::TryParse(*self, messageStr);}) {

View file

@ -16,6 +16,11 @@ auto Test() -> boost::asio::awaitable<void> {
.server = "irc.ilita.i2p"} .server = "irc.ilita.i2p"}
.AddState<std::unordered_set<yail::User, yail::User::Hash>, "ruUsers">() .AddState<std::unordered_set<yail::User, yail::User::Hash>, "ruUsers">()
// NOLINTNEXTLINE // NOLINTNEXTLINE
.With([](auto& self, yail::event::AnyMessage message) -> boost::asio::awaitable<void> {
std::println("Readed message: {}", message.data);
co_return;
})
// NOLINTNEXTLINE
.With([](auto& self, UserEvent event) -> boost::asio::awaitable<void> { .With([](auto& self, UserEvent event) -> boost::asio::awaitable<void> {
std::println("User event!"); std::println("User event!");
co_return; co_return;