fixed single connection code and deleting abstractircserver

This commit is contained in:
fourtf 2019-09-09 22:22:47 +02:00
parent 99b92bf820
commit 9f1a5b900e
3 changed files with 59 additions and 33 deletions

View file

@ -37,6 +37,7 @@ public:
TwitchWatching, TwitchWatching,
TwitchMentions, TwitchMentions,
TwitchEnd, TwitchEnd,
Irc,
Misc Misc
}; };

View file

@ -18,13 +18,17 @@ const int MAX_FALLOFF_COUNTER = 60;
AbstractIrcServer::AbstractIrcServer() AbstractIrcServer::AbstractIrcServer()
{ {
// Initialize the connections // Initialize the connections
// XXX: don't create write connection if there is not separate write connection.
this->writeConnection_.reset(new IrcConnection); this->writeConnection_.reset(new IrcConnection);
this->writeConnection_->moveToThread( this->writeConnection_->moveToThread(
QCoreApplication::instance()->thread()); QCoreApplication::instance()->thread());
QObject::connect( QObject::connect(
this->writeConnection_.get(), &Communi::IrcConnection::messageReceived, this->writeConnection_.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->writeConnectionMessageReceived(msg); }); this, [this](auto msg) { this->writeConnectionMessageReceived(msg); });
QObject::connect(
this->writeConnection_.get(), &Communi::IrcConnection::connected, this,
[this] { this->onWriteConnected(this->writeConnection_.get()); });
// Listen to read connection message signals // Listen to read connection message signals
this->readConnection_.reset(new IrcConnection); this->readConnection_.reset(new IrcConnection);
@ -32,21 +36,18 @@ AbstractIrcServer::AbstractIrcServer()
QObject::connect( QObject::connect(
this->readConnection_.get(), &Communi::IrcConnection::messageReceived, this->readConnection_.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->readConnectionMessageReceived(msg); }); this, [this](auto msg) { this->readConnectionMessageReceived(msg); });
QObject::connect(this->readConnection_.get(), QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::privateMessageReceived, &Communi::IrcConnection::privateMessageReceived, this,
[this](auto msg) { this->privateMessageReceived(msg); }); [this](auto msg) { this->privateMessageReceived(msg); });
QObject::connect( QObject::connect(
this->readConnection_.get(), &Communi::IrcConnection::connected, this->readConnection_.get(), &Communi::IrcConnection::connected, this,
[this] { this->onReadConnected(this->readConnection_.get()); }); [this] { this->onReadConnected(this->readConnection_.get()); });
QObject::connect(
this->writeConnection_.get(), &Communi::IrcConnection::connected,
[this] { this->onWriteConnected(this->writeConnection_.get()); });
QObject::connect(this->readConnection_.get(), QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::disconnected, &Communi::IrcConnection::disconnected, this,
[this] { this->onDisconnected(); }); [this] { this->onDisconnected(); });
QObject::connect(this->readConnection_.get(), QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::socketError, &Communi::IrcConnection::socketError, this,
[this] { this->onSocketError(); }); [this] { this->onSocketError(); });
// listen to reconnect request // listen to reconnect request
@ -75,9 +76,7 @@ void AbstractIrcServer::connect()
{ {
this->disconnect(); this->disconnect();
bool separateWriteConnection = this->hasSeparateWriteConnection(); if (this->hasSeparateWriteConnection())
if (separateWriteConnection)
{ {
this->initializeConnection(this->writeConnection_.get(), false, true); this->initializeConnection(this->writeConnection_.get(), false, true);
this->initializeConnection(this->readConnection_.get(), true, false); this->initializeConnection(this->readConnection_.get(), true, false);
@ -94,18 +93,18 @@ void AbstractIrcServer::connect()
for (std::weak_ptr<Channel> &weak : this->channels.values()) for (std::weak_ptr<Channel> &weak : this->channels.values())
{ {
if (auto channel = std::shared_ptr<Channel>(weak.lock())) if (auto channel = weak.lock())
{ {
this->readConnection_->sendRaw("JOIN #" + channel->getName()); this->readConnection_->sendRaw("JOIN #" + channel->getName());
} }
} }
this->writeConnection_->open(); if (this->hasSeparateWriteConnection())
{
this->writeConnection_->open();
}
this->readConnection_->open(); this->readConnection_->open();
} }
// this->onConnected();
// possbile event: started to connect
} }
void AbstractIrcServer::disconnect() void AbstractIrcServer::disconnect()
@ -113,7 +112,10 @@ void AbstractIrcServer::disconnect()
std::lock_guard<std::mutex> locker(this->connectionMutex_); std::lock_guard<std::mutex> locker(this->connectionMutex_);
this->readConnection_->close(); this->readConnection_->close();
this->writeConnection_->close(); if (this->hasSeparateWriteConnection())
{
this->writeConnection_->close();
}
} }
void AbstractIrcServer::sendMessage(const QString &channelName, void AbstractIrcServer::sendMessage(const QString &channelName,
@ -139,10 +141,10 @@ void AbstractIrcServer::sendRawMessage(const QString &rawMessage)
void AbstractIrcServer::writeConnectionMessageReceived( void AbstractIrcServer::writeConnectionMessageReceived(
Communi::IrcMessage *message) Communi::IrcMessage *message)
{ {
(void)message;
} }
std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel( ChannelPtr AbstractIrcServer::getOrAddChannel(const QString &dirtyChannelName)
const QString &dirtyChannelName)
{ {
auto channelName = this->cleanChannelName(dirtyChannelName); auto channelName = this->cleanChannelName(dirtyChannelName);
@ -177,7 +179,7 @@ std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(
this->readConnection_->sendRaw("PART #" + clojuresInCppAreShit); this->readConnection_->sendRaw("PART #" + clojuresInCppAreShit);
} }
if (this->writeConnection_) if (this->writeConnection_ && this->hasSeparateWriteConnection())
{ {
this->writeConnection_->sendRaw("PART #" + clojuresInCppAreShit); this->writeConnection_->sendRaw("PART #" + clojuresInCppAreShit);
} }
@ -192,7 +194,7 @@ std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(
this->readConnection_->sendRaw("JOIN #" + channelName); this->readConnection_->sendRaw("JOIN #" + channelName);
} }
if (this->writeConnection_) if (this->writeConnection_ && this->hasSeparateWriteConnection())
{ {
this->writeConnection_->sendRaw("JOIN #" + channelName); this->writeConnection_->sendRaw("JOIN #" + channelName);
} }
@ -201,8 +203,7 @@ std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(
return chan; return chan;
} }
std::shared_ptr<Channel> AbstractIrcServer::getChannelOrEmpty( ChannelPtr AbstractIrcServer::getChannelOrEmpty(const QString &dirtyChannelName)
const QString &dirtyChannelName)
{ {
auto channelName = this->cleanChannelName(dirtyChannelName); auto channelName = this->cleanChannelName(dirtyChannelName);
@ -230,9 +231,24 @@ std::shared_ptr<Channel> AbstractIrcServer::getChannelOrEmpty(
return Channel::getEmpty(); return Channel::getEmpty();
} }
std::vector<std::weak_ptr<Channel>> AbstractIrcServer::getChannels()
{
std::lock_guard lock(this->channelMutex);
std::vector<std::weak_ptr<Channel>> channels;
for (auto &&weak : this->channels.values())
{
channels.push_back(weak);
}
return channels;
}
void AbstractIrcServer::onReadConnected(IrcConnection *connection) void AbstractIrcServer::onReadConnected(IrcConnection *connection)
{ {
std::lock_guard<std::mutex> lock(this->channelMutex); (void)connection;
std::lock_guard lock(this->channelMutex);
auto connectedMsg = makeSystemMessage("connected"); auto connectedMsg = makeSystemMessage("connected");
connectedMsg->flags.set(MessageFlag::ConnectedMessage); connectedMsg->flags.set(MessageFlag::ConnectedMessage);
@ -267,6 +283,7 @@ void AbstractIrcServer::onReadConnected(IrcConnection *connection)
void AbstractIrcServer::onWriteConnected(IrcConnection *connection) void AbstractIrcServer::onWriteConnected(IrcConnection *connection)
{ {
(void)connection;
} }
void AbstractIrcServer::onDisconnected() void AbstractIrcServer::onDisconnected()
@ -297,6 +314,7 @@ void AbstractIrcServer::onSocketError()
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel( std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
const QString &channelName) const QString &channelName)
{ {
(void)channelName;
return nullptr; return nullptr;
} }
@ -324,11 +342,13 @@ void AbstractIrcServer::addFakeMessage(const QString &data)
void AbstractIrcServer::privateMessageReceived( void AbstractIrcServer::privateMessageReceived(
Communi::IrcPrivateMessage *message) Communi::IrcPrivateMessage *message)
{ {
(void)message;
} }
void AbstractIrcServer::readConnectionMessageReceived( void AbstractIrcServer::readConnectionMessageReceived(
Communi::IrcMessage *message) Communi::IrcMessage *message)
{ {
(void)message;
} }
void AbstractIrcServer::forEachChannel(std::function<void(ChannelPtr)> func) void AbstractIrcServer::forEachChannel(std::function<void(ChannelPtr)> func)
@ -337,7 +357,7 @@ void AbstractIrcServer::forEachChannel(std::function<void(ChannelPtr)> func)
for (std::weak_ptr<Channel> &weak : this->channels.values()) for (std::weak_ptr<Channel> &weak : this->channels.values())
{ {
std::shared_ptr<Channel> chan = weak.lock(); ChannelPtr chan = weak.lock();
if (!chan) if (!chan)
{ {
continue; continue;

View file

@ -13,7 +13,7 @@ namespace chatterino {
class Channel; class Channel;
using ChannelPtr = std::shared_ptr<Channel>; using ChannelPtr = std::shared_ptr<Channel>;
class AbstractIrcServer class AbstractIrcServer : public QObject
{ {
public: public:
virtual ~AbstractIrcServer() = default; virtual ~AbstractIrcServer() = default;
@ -26,14 +26,12 @@ public:
void sendRawMessage(const QString &rawMessage); void sendRawMessage(const QString &rawMessage);
// channels // channels
std::shared_ptr<Channel> getOrAddChannel(const QString &dirtyChannelName); ChannelPtr getOrAddChannel(const QString &dirtyChannelName);
std::shared_ptr<Channel> getChannelOrEmpty(const QString &dirtyChannelName); ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName);
// signals // signals
pajlada::Signals::NoArgSignal connected; pajlada::Signals::NoArgSignal connected;
pajlada::Signals::NoArgSignal disconnected; pajlada::Signals::NoArgSignal disconnected;
// pajlada::Signals::Signal<Communi::IrcPrivateMessage *>
// onPrivateMessage;
void addFakeMessage(const QString &data); void addFakeMessage(const QString &data);
@ -69,8 +67,15 @@ protected:
private: private:
void initConnection(); void initConnection();
std::unique_ptr<IrcConnection> writeConnection_ = nullptr; struct Deleter {
std::unique_ptr<IrcConnection> readConnection_ = nullptr; void operator()(IrcConnection *conn)
{
conn->deleteLater();
}
};
std::unique_ptr<IrcConnection, Deleter> writeConnection_ = nullptr;
std::unique_ptr<IrcConnection, Deleter> readConnection_ = nullptr;
QTimer reconnectTimer_; QTimer reconnectTimer_;
int falloffCounter_ = 1; int falloffCounter_ = 1;