small refactor of twitchserver

This commit is contained in:
fourtf 2018-06-22 23:24:45 +02:00
parent 115031b7ac
commit 4aec66e3ea
5 changed files with 44 additions and 53 deletions

View file

@ -15,38 +15,38 @@ namespace irc {
AbstractIrcServer::AbstractIrcServer() AbstractIrcServer::AbstractIrcServer()
{ {
// Initialize the connections // Initialize the connections
this->writeConnection.reset(new IrcConnection); this->writeConnection_.reset(new IrcConnection);
this->writeConnection->moveToThread(QCoreApplication::instance()->thread()); this->writeConnection_->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->writeConnection.get(), &Communi::IrcConnection::messageReceived, QObject::connect(this->writeConnection_.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->writeConnectionMessageReceived(msg); }); [this](auto msg) { this->writeConnectionMessageReceived(msg); });
// Listen to read connection message signals // Listen to read connection message signals
this->readConnection.reset(new IrcConnection); this->readConnection_.reset(new IrcConnection);
this->readConnection->moveToThread(QCoreApplication::instance()->thread()); this->readConnection_->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::messageReceived, QObject::connect(this->readConnection_.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->messageReceived(msg); }); [this](auto msg) { this->messageReceived(msg); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::privateMessageReceived, QObject::connect(this->readConnection_.get(), &Communi::IrcConnection::privateMessageReceived,
[this](auto msg) { this->privateMessageReceived(msg); }); [this](auto msg) { this->privateMessageReceived(msg); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::connected, QObject::connect(this->readConnection_.get(), &Communi::IrcConnection::connected,
[this] { this->onConnected(); }); [this] { this->onConnected(); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected, QObject::connect(this->readConnection_.get(), &Communi::IrcConnection::disconnected,
[this] { this->onDisconnected(); }); [this] { this->onDisconnected(); });
// listen to reconnect request // listen to reconnect request
this->readConnection->reconnectRequested.connect([this] { this->connect(); }); this->readConnection_->reconnectRequested.connect([this] { this->connect(); });
// this->writeConnection->reconnectRequested.connect([this] { this->connect(); }); // this->writeConnection->reconnectRequested.connect([this] { this->connect(); });
} }
IrcConnection *AbstractIrcServer::getReadConnection() const IrcConnection *AbstractIrcServer::getReadConnection() const
{ {
return this->readConnection.get(); return this->readConnection_.get();
} }
IrcConnection *AbstractIrcServer::getWriteConnection() const IrcConnection *AbstractIrcServer::getWriteConnection() const
{ {
return this->writeConnection.get(); return this->writeConnection_.get();
} }
void AbstractIrcServer::connect() void AbstractIrcServer::connect()
@ -54,15 +54,15 @@ void AbstractIrcServer::connect()
this->disconnect(); this->disconnect();
// if (this->hasSeperateWriteConnection()) { // if (this->hasSeperateWriteConnection()) {
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);
// } else { // } else {
// this->initializeConnection(this->readConnection.get(), true, true); // this->initializeConnection(this->readConnection.get(), true, true);
// } // }
// fourtf: this should be asynchronous // fourtf: this should be asynchronous
{ {
std::lock_guard<std::mutex> lock1(this->connectionMutex); std::lock_guard<std::mutex> lock1(this->connectionMutex_);
std::lock_guard<std::mutex> lock2(this->channelMutex); std::lock_guard<std::mutex> lock2(this->channelMutex);
for (std::weak_ptr<Channel> &weak : this->channels.values()) { for (std::weak_ptr<Channel> &weak : this->channels.values()) {
@ -71,12 +71,12 @@ void AbstractIrcServer::connect()
continue; continue;
} }
this->writeConnection->sendRaw("JOIN #" + chan->name); this->writeConnection_->sendRaw("JOIN #" + chan->name);
this->readConnection->sendRaw("JOIN #" + chan->name); this->readConnection_->sendRaw("JOIN #" + chan->name);
} }
this->writeConnection->open(); this->writeConnection_->open();
this->readConnection->open(); this->readConnection_->open();
} }
// this->onConnected(); // this->onConnected();
@ -85,20 +85,20 @@ void AbstractIrcServer::connect()
void AbstractIrcServer::disconnect() 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(); this->writeConnection_->close();
} }
void AbstractIrcServer::sendMessage(const QString &channelName, const QString &message) void AbstractIrcServer::sendMessage(const QString &channelName, const QString &message)
{ {
std::lock_guard<std::mutex> locker(this->connectionMutex); std::lock_guard<std::mutex> locker(this->connectionMutex_);
// fourtf: trim the message if it's sent from twitch chat // fourtf: trim the message if it's sent from twitch chat
if (this->writeConnection) { if (this->writeConnection_) {
this->writeConnection->sendRaw("PRIVMSG #" + channelName + " :" + message); this->writeConnection_->sendRaw("PRIVMSG #" + channelName + " :" + message);
} }
} }
@ -133,25 +133,25 @@ std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(const QString &dirty
debug::Log("[AbstractIrcServer::addChannel] {} was destroyed", clojuresInCppAreShit); debug::Log("[AbstractIrcServer::addChannel] {} was destroyed", clojuresInCppAreShit);
this->channels.remove(clojuresInCppAreShit); this->channels.remove(clojuresInCppAreShit);
if (this->readConnection) { if (this->readConnection_) {
this->readConnection->sendRaw("PART #" + clojuresInCppAreShit); this->readConnection_->sendRaw("PART #" + clojuresInCppAreShit);
} }
if (this->writeConnection) { if (this->writeConnection_) {
this->writeConnection->sendRaw("PART #" + clojuresInCppAreShit); this->writeConnection_->sendRaw("PART #" + clojuresInCppAreShit);
} }
}); });
// join irc channel // join irc channel
{ {
std::lock_guard<std::mutex> lock2(this->connectionMutex); std::lock_guard<std::mutex> lock2(this->connectionMutex_);
if (this->readConnection) { if (this->readConnection_) {
this->readConnection->sendRaw("JOIN #" + channelName); this->readConnection_->sendRaw("JOIN #" + channelName);
} }
if (this->writeConnection) { if (this->writeConnection_) {
this->writeConnection->sendRaw("JOIN #" + channelName); this->writeConnection_->sendRaw("JOIN #" + channelName);
} }
} }
@ -240,7 +240,7 @@ QString AbstractIrcServer::cleanChannelName(const QString &dirtyChannelName)
void AbstractIrcServer::addFakeMessage(const QString &data) void AbstractIrcServer::addFakeMessage(const QString &data)
{ {
auto fakeMessage = Communi::IrcMessage::fromData(data.toUtf8(), this->readConnection.get()); auto fakeMessage = Communi::IrcMessage::fromData(data.toUtf8(), this->readConnection_.get());
this->messageReceived(fakeMessage); this->messageReceived(fakeMessage);
} }

View file

@ -64,12 +64,12 @@ protected:
private: private:
void initConnection(); void initConnection();
std::unique_ptr<IrcConnection> writeConnection = nullptr; std::unique_ptr<IrcConnection> writeConnection_ = nullptr;
std::unique_ptr<IrcConnection> readConnection = nullptr; std::unique_ptr<IrcConnection> readConnection_ = nullptr;
std::mutex connectionMutex; std::mutex connectionMutex_;
QTimer pingTimer; // bool autoReconnect_ = false;
}; };
} // namespace irc } // namespace irc

View file

@ -186,16 +186,7 @@ std::shared_ptr<Channel> TwitchServer::getCustomChannel(const QString &channelNa
void TwitchServer::forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func) void TwitchServer::forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func)
{ {
std::lock_guard<std::mutex> lock(this->channelMutex); this->forEachChannel(func);
for (std::weak_ptr<Channel> &weak : this->channels) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
func(chan);
}
func(this->whispersChannel); func(this->whispersChannel);
func(this->mentionsChannel); func(this->mentionsChannel);
@ -203,6 +194,8 @@ void TwitchServer::forEachChannelAndSpecialChannels(std::function<void(ChannelPt
std::shared_ptr<Channel> TwitchServer::getChannelOrEmptyByID(const QString &channelID) std::shared_ptr<Channel> TwitchServer::getChannelOrEmptyByID(const QString &channelID)
{ {
this->forEachChannel();
{ {
std::lock_guard<std::mutex> lock(this->channelMutex); std::lock_guard<std::mutex> lock(this->channelMutex);

View file

@ -26,9 +26,6 @@ public:
util::MutexValue<QString> lastUserThatWhisperedMe; util::MutexValue<QString> lastUserThatWhisperedMe;
// QString getLastWhisperedPerson() const;
// void setLastWhisperedPerson(const QString &person);
const ChannelPtr whispersChannel; const ChannelPtr whispersChannel;
const ChannelPtr mentionsChannel; const ChannelPtr mentionsChannel;
IndirectChannel watchingChannel; IndirectChannel watchingChannel;

View file

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <boost/noncopyable.hpp>
#include <mutex> #include <mutex>
namespace chatterino { namespace chatterino {
namespace util { namespace util {
template <typename T> template <typename T>
class MutexValue class MutexValue : boost::noncopyable
{ {
mutable std::mutex mutex; mutable std::mutex mutex;
T value; T value;