added code to handle a single connection
This commit is contained in:
@@ -35,26 +35,18 @@ AbstractIrcServer::AbstractIrcServer()
|
||||
// this->writeConnection->reconnectRequested.connect([this] { this->connect(); });
|
||||
}
|
||||
|
||||
IrcConnection *AbstractIrcServer::getReadConnection() const
|
||||
{
|
||||
return this->readConnection_.get();
|
||||
}
|
||||
|
||||
IrcConnection *AbstractIrcServer::getWriteConnection() const
|
||||
{
|
||||
return this->writeConnection_.get();
|
||||
}
|
||||
|
||||
void AbstractIrcServer::connect()
|
||||
{
|
||||
this->disconnect();
|
||||
|
||||
// if (this->hasSeparateWriteConnection()) {
|
||||
this->initializeConnection(this->writeConnection_.get(), false, true);
|
||||
this->initializeConnection(this->readConnection_.get(), true, false);
|
||||
// } else {
|
||||
// this->initializeConnection(this->readConnection.get(), true, true);
|
||||
// }
|
||||
bool separateWriteConnection = this->hasSeparateWriteConnection();
|
||||
|
||||
if (separateWriteConnection) {
|
||||
this->initializeConnection(this->writeConnection_.get(), false, true);
|
||||
this->initializeConnection(this->readConnection_.get(), true, false);
|
||||
} else {
|
||||
this->initializeConnection(this->readConnection_.get(), true, true);
|
||||
}
|
||||
|
||||
// fourtf: this should be asynchronous
|
||||
{
|
||||
@@ -67,7 +59,6 @@ void AbstractIrcServer::connect()
|
||||
continue;
|
||||
}
|
||||
|
||||
this->writeConnection_->sendRaw("JOIN #" + chan->name);
|
||||
this->readConnection_->sendRaw("JOIN #" + chan->name);
|
||||
}
|
||||
|
||||
@@ -88,13 +79,18 @@ void AbstractIrcServer::disconnect()
|
||||
}
|
||||
|
||||
void AbstractIrcServer::sendMessage(const QString &channelName, const QString &message)
|
||||
{
|
||||
this->sendRawMessage("PRIVMSG #" + channelName + " :" + message);
|
||||
}
|
||||
|
||||
void AbstractIrcServer::sendRawMessage(const QString &rawMessage)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(this->connectionMutex_);
|
||||
|
||||
// fourtf: trim the message if it's sent from twitch chat
|
||||
|
||||
if (this->writeConnection_) {
|
||||
this->writeConnection_->sendRaw("PRIVMSG #" + channelName + " :" + message);
|
||||
if (this->hasSeparateWriteConnection()) {
|
||||
this->writeConnection_->sendRaw(rawMessage);
|
||||
} else {
|
||||
this->readConnection_->sendRaw(rawMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,11 @@ public:
|
||||
virtual ~AbstractIrcServer() = default;
|
||||
|
||||
// connection
|
||||
IrcConnection *getReadConnection() const;
|
||||
IrcConnection *getWriteConnection() const;
|
||||
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
void sendMessage(const QString &channelName, const QString &message);
|
||||
void sendRawMessage(const QString &rawMessage);
|
||||
|
||||
// channels
|
||||
std::shared_ptr<Channel> getOrAddChannel(const QString &dirtyChannelName);
|
||||
@@ -54,6 +52,7 @@ protected:
|
||||
|
||||
virtual std::shared_ptr<Channel> getCustomChannel(const QString &channelName);
|
||||
|
||||
virtual bool hasSeparateWriteConnection() const = 0;
|
||||
virtual QString cleanChannelName(const QString &dirtyChannelName);
|
||||
|
||||
QMap<QString, std::weak_ptr<Channel>> channels;
|
||||
|
||||
@@ -9,18 +9,23 @@ IrcConnection::IrcConnection(QObject *parent)
|
||||
this->pingTimer_.setInterval(5000);
|
||||
this->pingTimer_.start();
|
||||
QObject::connect(&this->pingTimer_, &QTimer::timeout, [this] {
|
||||
if (!this->recentlyReceivedMessage_.load()) {
|
||||
this->sendRaw("PING");
|
||||
this->reconnectTimer_.start();
|
||||
if (this->isConnected()) {
|
||||
if (!this->recentlyReceivedMessage_.load()) {
|
||||
this->sendRaw("PING");
|
||||
this->reconnectTimer_.start();
|
||||
}
|
||||
this->recentlyReceivedMessage_ = false;
|
||||
}
|
||||
this->recentlyReceivedMessage_ = false;
|
||||
});
|
||||
|
||||
// reconnect after x seconds without receiving a message
|
||||
this->reconnectTimer_.setInterval(5000);
|
||||
this->reconnectTimer_.setSingleShot(true);
|
||||
QObject::connect(&this->reconnectTimer_, &QTimer::timeout,
|
||||
[this] { reconnectRequested.invoke(); });
|
||||
QObject::connect(&this->reconnectTimer_, &QTimer::timeout, [this] {
|
||||
if (this->isConnected()) {
|
||||
reconnectRequested.invoke();
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(this, &Communi::IrcConnection::messageReceived, [this](Communi::IrcMessage *) {
|
||||
this->recentlyReceivedMessage_ = true;
|
||||
|
||||
Reference in New Issue
Block a user