An event loop

This commit is contained in:
Blue 2025-04-12 13:12:38 +03:00
parent ce29081a5f
commit 69e8098cce
Signed by: blue
GPG key ID: 9B203B252A63EE38
10 changed files with 295 additions and 11 deletions

View file

@ -3,6 +3,8 @@
#include "connection.h"
#include "gloox/connectiontcpclient.h"
Connection::Connection(const std::shared_ptr<Core>& core):
Shared::Loggable(core->logger, {"Connection"}),
state(initial),
@ -53,14 +55,26 @@ void Connection::deinitialize() {
state = initial;
}
void Connection::connect() {
if (state != disconnected)
return;
int Connection::connect() {
if (state != disconnected) {
std::string message("an attempt to call connect in the wrong state");
fatal(message);
throw std::runtime_error(message);
}
debug("connecting");
state = connected;
gloox->connect(true);
state = disconnected;
state = connecting;
gloox->connect(false);
return static_cast<gloox::ConnectionTCPClient*>(gloox->connectionImpl())->socket();
}
void Connection::disconnect() {
if (state != connecting || state != connected)
return;
gloox->disconnect();
state = disconnecting;
}
void Connection::send(const std::string& jid, const std::string& body) {
@ -87,6 +101,10 @@ void Connection::publish(const std::string& service, const std::string& node, co
pubsub->publishItem(service, node, list, nullptr, this);
}
void Connection::processMessages() {
gloox->recv();
}
std::string Connection::errorTypeToString(gloox::StanzaErrorType err) {
switch (err) {
case gloox::StanzaErrorTypeAuth:
@ -131,10 +149,12 @@ void Connection::handleItemPublication(const std::string& id, const gloox::JID&
}
void Connection::onConnect() {
state = connected;
info("connection established");
}
void Connection::onDisconnect(gloox::ConnectionError e) {
state = disconnected;
std::string error;
switch (e) {

View file

@ -28,7 +28,9 @@ public:
enum State {
initial,
disconnected,
connected
connecting,
connected,
disconnecting
};
public:
@ -37,9 +39,11 @@ public:
void initialize();
void deinitialize();
void connect();
int connect();
void disconnect();
void send(const std::string& jid, const std::string& body);
void publish(const std::string& service, const std::string& node, const std::string& title, const std::string& body);
void processMessages();
static std::string errorTypeToString(gloox::StanzaErrorType err);