From b1d9bff1116809a8d21d8e775213c7d82de151ab Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 26 Nov 2023 15:32:52 -0800 Subject: [PATCH] use structured sending functions --- main.cpp | 2 +- ping_thread.cpp | 2 +- registration_thread.cpp | 14 ++++---- watchdog_thread.cpp | 18 +++++----- watchdog_thread.hpp | 2 +- write_irc.cpp | 76 ++++++++++++++++++++++++++++++++++++++--- write_irc.hpp | 28 +++++---------- 7 files changed, 98 insertions(+), 44 deletions(-) diff --git a/main.cpp b/main.cpp index 6ffff18..1aca1a3 100644 --- a/main.cpp +++ b/main.cpp @@ -53,7 +53,7 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void { auto connection = std::make_shared(io); - watchdog_thread(connection.get()); + watchdog_thread(*connection); irc_parse_thread(connection.get()); ping_thread(connection.get()); registration_thread(connection.get(), settings.password, settings.username, settings.realname, settings.nickname); diff --git a/ping_thread.cpp b/ping_thread.cpp index b0ab2b6..9d3921d 100644 --- a/ping_thread.cpp +++ b/ping_thread.cpp @@ -10,7 +10,7 @@ auto ping_thread(Connection * connection) -> void auto& irc = event.irc; if (IrcCommand::PING == event.command) { - write_irc(*connection, "PONG", irc.args[0]); + send_pong(*connection, irc.args[0]); event.handled_ = true; } }); diff --git a/registration_thread.cpp b/registration_thread.cpp index f5a8e02..cbffd4f 100644 --- a/registration_thread.cpp +++ b/registration_thread.cpp @@ -63,10 +63,10 @@ RegistrationThread::RegistrationThread( auto RegistrationThread::on_connect() -> void { - write_irc(*connection_, "CAP", "LS", "302"); - write_irc(*connection_, "PASS", password_); - write_irc(*connection_, "USER", username_, "*", "*", realname_); - write_irc(*connection_, "NICK", nickname_); + send_cap_ls(*connection_); + send_pass(*connection_, password_); + send_user(*connection_, username_, realname_); + send_nick(*connection_, nickname_); connection_->remove_listener(connect_handle_); } @@ -103,12 +103,12 @@ auto RegistrationThread::send_req() -> void if (not outstanding.empty()) { request.pop_back(); - write_irc(*connection_, "CAP", "REQ", request); + send_cap_req(*connection_, request); stage_ = Stage::AckReply; } else { - write_irc(*connection_, "CAP", "END"); + send_cap_end(*connection_); connection_->remove_listener(message_handle_); } } @@ -128,7 +128,7 @@ auto RegistrationThread::capack(IrcMsg const& msg) -> void ); if (outstanding.empty()) { - write_irc(*connection_, "CAP", "END"); + send_cap_end(*connection_); connection_->remove_listener(message_handle_); } } diff --git a/watchdog_thread.cpp b/watchdog_thread.cpp index cefccc6..e2039c4 100644 --- a/watchdog_thread.cpp +++ b/watchdog_thread.cpp @@ -15,14 +15,14 @@ namespace { struct WatchdogThread : std::enable_shared_from_this { - WatchdogThread(Connection * connection) + WatchdogThread(Connection& connection) : connection_{connection} - , timer_{connection->get_executor()} + , timer_{connection.get_executor()} , tried_ping{false} { } - Connection * connection_; + Connection& connection_; boost::asio::steady_timer timer_; bool tried_ping; @@ -50,11 +50,11 @@ struct WatchdogThread : std::enable_shared_from_this { if (tried_ping) { - connection_->close(); + connection_.close(); } else { - write_irc(*connection_, "PING", "watchdog"); + send_ping(connection_, "watchdog"); tried_ping = true; timer_.expires_from_now(30s); timer_.async_wait(timeout_token()); @@ -75,18 +75,18 @@ struct WatchdogThread : std::enable_shared_from_this } // namespace -auto watchdog_thread(Connection * connection) -> void +auto watchdog_thread(Connection& connection) -> void { auto const thread = std::make_shared(connection); - connection->add_listener([thread](auto&) + connection.add_listener([thread](auto&) { thread->on_connect(); }); - connection->add_listener([thread](auto&) + connection.add_listener([thread](auto&) { thread->on_disconnect(); }); - connection->add_listener([thread](auto&) + connection.add_listener([thread](auto&) { thread->on_activity(); }); diff --git a/watchdog_thread.hpp b/watchdog_thread.hpp index 4433f7a..131fe8e 100644 --- a/watchdog_thread.hpp +++ b/watchdog_thread.hpp @@ -1,4 +1,4 @@ #pragma once class Connection; -auto watchdog_thread(Connection * connection) -> void; +auto watchdog_thread(Connection& connection) -> void; diff --git a/write_irc.cpp b/write_irc.cpp index 3ad5c8f..4387b29 100644 --- a/write_irc.cpp +++ b/write_irc.cpp @@ -1,18 +1,26 @@ #include "write_irc.hpp" +namespace { + auto write_irc(Connection& connection, std::string message) -> void { message += "\r\n"; connection.write_raw(std::move(message)); } +auto is_invalid_last(char x) -> bool +{ + return x == '\0' || x == '\r' || x == '\n'; +} + +auto is_invalid(char x) -> bool +{ + return x == '\0' || x == '\r' || x == '\n' || x == ' '; +} + auto write_irc(Connection& connection, std::string front, std::string_view last) -> void { - auto const is_invalid = [](char x) -> bool { - return x == '\0' || x == '\r' || x == '\n'; - }; - - if (last.end() != std::find_if(last.begin(), last.end(), is_invalid)) + if (last.end() != std::find_if(last.begin(), last.end(), is_invalid_last)) { throw std::runtime_error{"bad irc argument"}; } @@ -21,3 +29,61 @@ auto write_irc(Connection& connection, std::string front, std::string_view last) front += last; write_irc(connection, std::move(front)); } + +template +auto write_irc(Connection& connection, std::string front, std::string_view next, Args ...rest) -> void +{ + if (next.empty() + || next.front() == ':' + || next.end() != std::find_if(next.begin(), next.end(), is_invalid)) + { + throw std::runtime_error{"bad irc argument"}; + } + + front += " "; + front += next; + write_irc(connection, std::move(front), rest...); +} + +} // namespace + +auto send_ping(Connection& connection, std::string_view txt) -> void +{ + write_irc(connection, "PING", txt); +} + +auto send_pong(Connection& connection, std::string_view txt) -> void +{ + write_irc(connection, "PONG", txt); +} + +auto send_pass(Connection& connection, std::string_view password) -> void +{ + write_irc(connection, "PASS", password); +} + +auto send_user(Connection& connection, std::string_view user, std::string_view real) -> void +{ + write_irc(connection, "USER", user, "*", "*", real); +} + +auto send_nick(Connection& connection, std::string_view nick) -> void +{ + write_irc(connection, "NICK", nick); +} + +auto send_cap_ls(Connection& connection) -> void +{ + write_irc(connection, "CAP", "LS", "302"); +} + +auto send_cap_end(Connection& connection) -> void +{ + write_irc(connection, "CAP", "END"); +} + +auto send_cap_req(Connection& connection, std::string_view caps) -> void +{ + write_irc(connection, "CAP", "REQ", caps); +} + diff --git a/write_irc.hpp b/write_irc.hpp index f47977d..a4efbf5 100644 --- a/write_irc.hpp +++ b/write_irc.hpp @@ -2,23 +2,11 @@ #include "connection.hpp" -auto write_irc(Connection& connection, std::string message) -> void; - -auto write_irc(Connection& connection, std::string front, std::string_view last) -> void; - -template -auto write_irc(Connection& connection, std::string front, std::string_view next, Args ...rest) -> void -{ - auto const is_invalid = [](char x) -> bool { - return x == '\0' || x == '\r' || x == '\n' || x == ' '; - }; - - if (next.empty() || next.end() != std::find_if(next.begin(), next.end(), is_invalid)) - { - throw std::runtime_error{"bad irc argument"}; - } - - front += " "; - front += next; - write_irc(connection, std::move(front), rest...); -} \ No newline at end of file +auto send_ping(Connection&, std::string_view) -> void; +auto send_pong(Connection&, std::string_view) -> void; +auto send_pass(Connection&, std::string_view) -> void; +auto send_user(Connection&, std::string_view, std::string_view) -> void; +auto send_nick(Connection&, std::string_view) -> void; +auto send_cap_ls(Connection&) -> void; +auto send_cap_end(Connection&) -> void; +auto send_cap_req(Connection&, std::string_view) -> void;