Pass connection as a reference

This commit is contained in:
Eric Mertens 2023-11-26 15:40:40 -08:00
parent b1d9bff111
commit f18a440b20
9 changed files with 38 additions and 38 deletions

View File

@ -10,9 +10,9 @@ namespace {
} // namespace } // namespace
auto irc_parse_thread(Connection * connection) -> void auto irc_parse_thread(Connection& connection) -> void
{ {
connection->add_listener<LineEvent>([connection](LineEvent const& event) connection.add_listener<LineEvent>([&connection](LineEvent const& event)
{ {
auto const msg = parse_irc_message(event.line); auto const msg = parse_irc_message(event.line);
auto const recognized = IrcCommandHash::in_word_set(msg.command.data(), msg.command.size()); auto const recognized = IrcCommandHash::in_word_set(msg.command.data(), msg.command.size());
@ -21,6 +21,6 @@ auto irc_parse_thread(Connection * connection) -> void
&& recognized->min_args <= msg.args.size() && recognized->min_args <= msg.args.size()
&& recognized->max_args >= msg.args.size() && recognized->max_args >= msg.args.size()
? recognized->command : IrcCommand::UNKNOWN; ? recognized->command : IrcCommand::UNKNOWN;
connection->make_event<IrcMsgEvent>(command, msg); connection.make_event<IrcMsgEvent>(command, msg);
}); });
} }

View File

@ -293,4 +293,4 @@ struct IrcMsgEvent : Event
IrcMsg const& irc; IrcMsg const& irc;
}; };
auto irc_parse_thread(Connection * connection) -> void; auto irc_parse_thread(Connection& connection) -> void;

View File

@ -32,9 +32,9 @@
using namespace std::chrono_literals; using namespace std::chrono_literals;
auto unhandled_message_thread(Connection * connection) -> void auto unhandled_message_thread(Connection& connection) -> void
{ {
connection->add_listener<IrcMsgEvent>([](IrcMsgEvent const& event) connection.add_listener<IrcMsgEvent>([](IrcMsgEvent const& event)
{ {
if (IrcCommand::UNKNOWN == event.command) if (IrcCommand::UNKNOWN == event.command)
{ {
@ -54,11 +54,11 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void
auto connection = std::make_shared<Connection>(io); auto connection = std::make_shared<Connection>(io);
watchdog_thread(*connection); watchdog_thread(*connection);
irc_parse_thread(connection.get()); irc_parse_thread(*connection);
ping_thread(connection.get()); ping_thread(*connection);
registration_thread(connection.get(), settings.password, settings.username, settings.realname, settings.nickname); registration_thread(*connection, settings.password, settings.username, settings.realname, settings.nickname);
snote_thread(connection.get()); snote_thread(*connection);
unhandled_message_thread(connection.get()); unhandled_message_thread(*connection);
boost::asio::co_spawn( boost::asio::co_spawn(
io, io,

View File

@ -3,14 +3,14 @@
#include "irc_parse_thread.hpp" #include "irc_parse_thread.hpp"
#include "write_irc.hpp" #include "write_irc.hpp"
auto ping_thread(Connection * connection) -> void auto ping_thread(Connection& connection) -> void
{ {
connection->add_listener<IrcMsgEvent>([connection](IrcMsgEvent& event) connection.add_listener<IrcMsgEvent>([&connection](IrcMsgEvent& event)
{ {
auto& irc = event.irc; auto& irc = event.irc;
if (IrcCommand::PING == event.command) if (IrcCommand::PING == event.command)
{ {
send_pong(*connection, irc.args[0]); send_pong(connection, irc.args[0]);
event.handled_ = true; event.handled_ = true;
} }
}); });

View File

@ -3,4 +3,4 @@
#include "connection.hpp" #include "connection.hpp"
#include "thread.hpp" #include "thread.hpp"
auto ping_thread(Connection * connection) -> void; auto ping_thread(Connection& connection) -> void;

View File

@ -7,7 +7,7 @@
namespace { namespace {
struct RegistrationThread : std::enable_shared_from_this<RegistrationThread> struct RegistrationThread : std::enable_shared_from_this<RegistrationThread>
{ {
Connection * connection_; Connection& connection_;
std::string password_; std::string password_;
std::string username_; std::string username_;
std::string realname_; std::string realname_;
@ -28,7 +28,7 @@ struct RegistrationThread : std::enable_shared_from_this<RegistrationThread>
Stage stage_; Stage stage_;
RegistrationThread( RegistrationThread(
Connection * connection_, Connection& connection_,
std::string password, std::string password,
std::string username, std::string username,
std::string realname, std::string realname,
@ -47,7 +47,7 @@ struct RegistrationThread : std::enable_shared_from_this<RegistrationThread>
}; };
RegistrationThread::RegistrationThread( RegistrationThread::RegistrationThread(
Connection * connection, Connection& connection,
std::string password, std::string password,
std::string username, std::string username,
std::string realname, std::string realname,
@ -63,12 +63,12 @@ RegistrationThread::RegistrationThread(
auto RegistrationThread::on_connect() -> void auto RegistrationThread::on_connect() -> void
{ {
send_cap_ls(*connection_); send_cap_ls(connection_);
send_pass(*connection_, password_); send_pass(connection_, password_);
send_user(*connection_, username_, realname_); send_user(connection_, username_, realname_);
send_nick(*connection_, nickname_); send_nick(connection_, nickname_);
connection_->remove_listener(connect_handle_); connection_.remove_listener(connect_handle_);
} }
auto RegistrationThread::send_req() -> void auto RegistrationThread::send_req() -> void
@ -103,13 +103,13 @@ auto RegistrationThread::send_req() -> void
if (not outstanding.empty()) if (not outstanding.empty())
{ {
request.pop_back(); request.pop_back();
send_cap_req(*connection_, request); send_cap_req(connection_, request);
stage_ = Stage::AckReply; stage_ = Stage::AckReply;
} }
else else
{ {
send_cap_end(*connection_); send_cap_end(connection_);
connection_->remove_listener(message_handle_); connection_.remove_listener(message_handle_);
} }
} }
@ -128,8 +128,8 @@ auto RegistrationThread::capack(IrcMsg const& msg) -> void
); );
if (outstanding.empty()) if (outstanding.empty())
{ {
send_cap_end(*connection_); send_cap_end(connection_);
connection_->remove_listener(message_handle_); connection_.remove_listener(message_handle_);
} }
} }
} }
@ -194,7 +194,7 @@ auto RegistrationThread::on_msg(IrcMsg const& msg) -> void
} // namespace } // namespace
auto registration_thread( auto registration_thread(
Connection * connection, Connection& connection,
std::string password, std::string password,
std::string username, std::string username,
std::string realname, std::string realname,
@ -202,14 +202,14 @@ auto registration_thread(
) -> void ) -> void
{ {
auto const thread = std::make_shared<RegistrationThread>(connection, password, username, realname, nickname); auto const thread = std::make_shared<RegistrationThread>(connection, password, username, realname, nickname);
thread->message_handle_ = connection->add_listener<IrcMsgEvent>([thread](IrcMsgEvent const& event) thread->message_handle_ = connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent const& event)
{ {
if (IrcCommand::CAP == event.command) if (IrcCommand::CAP == event.command)
{ {
thread->on_msg(event.irc); thread->on_msg(event.irc);
} }
}); });
thread->connect_handle_ = connection->add_listener<ConnectEvent>([thread](ConnectEvent const&) thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
{ {
thread->on_connect(); thread->on_connect();
}); });

View File

@ -10,7 +10,7 @@
#include <string> #include <string>
auto registration_thread( auto registration_thread(
Connection * connection, Connection& connection,
std::string password, std::string password,
std::string username, std::string username,
std::string realname, std::string realname,

View File

@ -6,10 +6,10 @@
#include <cstring> #include <cstring>
auto snote_thread(Connection * connection) -> void auto snote_thread(Connection& connection) -> void
{ {
static char const* const prefix = "*** Notice -- "; static char const* const prefix = "*** Notice -- ";
connection->add_listener<IrcMsgEvent>([connection](IrcMsgEvent& event) connection.add_listener<IrcMsgEvent>([&connection](IrcMsgEvent& event)
{ {
auto& args = event.irc.args; auto& args = event.irc.args;
if (IrcCommand::NOTICE == event.command if (IrcCommand::NOTICE == event.command
@ -17,7 +17,7 @@ auto snote_thread(Connection * connection) -> void
&& args[1].starts_with(prefix)) && args[1].starts_with(prefix))
{ {
event.handled_ = true; event.handled_ = true;
connection->make_event<SnoteEvent>(args[1].substr(strlen(prefix))); connection.make_event<SnoteEvent>(args[1].substr(strlen(prefix)));
} }
}); });
} }

View File

@ -11,4 +11,4 @@ struct SnoteEvent : Event
std::string_view raw; std::string_view raw;
}; };
auto snote_thread(Connection * connection) -> void; auto snote_thread(Connection& connection) -> void;