added code to handle a single connection

This commit is contained in:
fourtf
2018-07-16 17:23:41 +02:00
parent e51c5c692a
commit 3b3c5d8d75
18 changed files with 114 additions and 84 deletions
+17 -21
View File
@@ -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);
}
}
+2 -3
View File
@@ -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;
+11 -6
View File
@@ -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;
+4 -3
View File
@@ -19,7 +19,7 @@
namespace chatterino {
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *readConnection)
TwitchChannel::TwitchChannel(const QString &channelName)
: Channel(channelName, Channel::Type::Twitch)
, bttvEmotes_(new EmoteMap)
, ffzEmotes_(new EmoteMap)
@@ -27,7 +27,6 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
, channelUrl_("https://twitch.tv/" + name)
, popoutPlayerUrl_("https://player.twitch.tv/?channel=" + name)
, mod_(false)
, readConnection_(readConnection)
{
Log("[TwitchChannel:{}] Opened", this->name);
@@ -420,7 +419,9 @@ bool TwitchChannel::parseRecentMessages(const QJsonObject &jsonRoot)
for (const auto jsonMessage : jsonMessages) {
auto content = jsonMessage.toString().toUtf8();
auto message = Communi::IrcMessage::fromData(content, this->readConnection_);
// passing nullptr as the channel makes the message invalid but we don't check for that
// anyways
auto message = Communi::IrcMessage::fromData(content, nullptr);
auto privMsg = dynamic_cast<Communi::IrcPrivateMessage *>(message);
assert(privMsg);
+1 -2
View File
@@ -86,7 +86,7 @@ private:
QString localizedName;
};
explicit TwitchChannel(const QString &channelName, Communi::IrcConnection *readConnection);
explicit TwitchChannel(const QString &channelName);
// Methods
void refreshLiveStatus();
@@ -124,7 +124,6 @@ private:
QObject lifetimeGuard_;
QTimer liveStatusTimer_;
QTimer chattersListTimer_;
Communi::IrcConnection *readConnection_ = nullptr;
friend class TwitchServer;
};
+14 -5
View File
@@ -28,6 +28,9 @@ TwitchServer::TwitchServer()
qDebug() << "init TwitchServer";
this->pubsub = new PubSub;
getSettings()->twitchSeperateWriteConnection.connect([this](auto, auto) { this->connect(); },
this->signalHolder_, false);
}
void TwitchServer::initialize(Application &app)
@@ -42,12 +45,13 @@ void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead,
{
assert(this->app);
this->singleConnection_ = isRead == isWrite;
std::shared_ptr<TwitchAccount> account = getApp()->accounts->twitch.getCurrent();
qDebug() << "logging in as" << account->getUserName();
QString username = account->getUserName();
// QString oauthClient = account->getOAuthClient();
QString oauthToken = account->getOAuthToken();
if (!oauthToken.startsWith("oauth:")) {
@@ -60,9 +64,6 @@ void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead,
if (!account->isAnon()) {
connection->setPassword(oauthToken);
// fourtf: ignored users
// this->refreshIgnoredUsers(username, oauthClient, oauthToken);
}
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership"));
@@ -75,7 +76,7 @@ void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead,
std::shared_ptr<Channel> TwitchServer::createChannel(const QString &channelName)
{
TwitchChannel *channel = new TwitchChannel(channelName, this->getReadConnection());
TwitchChannel *channel = new TwitchChannel(channelName);
channel->sendMessageSignal.connect([this, channel](auto &chan, auto &msg, bool &sent) {
this->onMessageSendRequested(channel, msg, sent);
@@ -91,6 +92,8 @@ void TwitchServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
void TwitchServer::messageReceived(Communi::IrcMessage *message)
{
qDebug() << message->toData();
// this->readConnection
if (message->type() == Communi::IrcMessage::Type::Private) {
// We already have a handler for private messages
@@ -179,6 +182,12 @@ QString TwitchServer::cleanChannelName(const QString &dirtyChannelName)
return dirtyChannelName.toLower();
}
bool TwitchServer::hasSeparateWriteConnection() const
{
return true;
// return getSettings()->twitchSeperateWriteConnection;
}
void TwitchServer::onMessageSendRequested(TwitchChannel *channel, const QString &message,
bool &sent)
{
+13 -8
View File
@@ -22,7 +22,6 @@ public:
virtual void initialize(Application &app) override;
// fourtf: ugh
void forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func);
std::shared_ptr<Channel> getChannelOrEmptyByID(const QString &channelID);
@@ -36,16 +35,18 @@ public:
PubSub *pubsub;
protected:
void initializeConnection(IrcConnection *connection, bool isRead, bool isWrite) override;
std::shared_ptr<Channel> createChannel(const QString &channelName) override;
virtual void initializeConnection(IrcConnection *connection, bool isRead,
bool isWrite) override;
virtual std::shared_ptr<Channel> createChannel(const QString &channelName) override;
void privateMessageReceived(Communi::IrcPrivateMessage *message) override;
void messageReceived(Communi::IrcMessage *message) override;
void writeConnectionMessageReceived(Communi::IrcMessage *message) override;
virtual void privateMessageReceived(Communi::IrcPrivateMessage *message) override;
virtual void messageReceived(Communi::IrcMessage *message) override;
virtual void writeConnectionMessageReceived(Communi::IrcMessage *message) override;
std::shared_ptr<Channel> getCustomChannel(const QString &channelname) override;
virtual std::shared_ptr<Channel> getCustomChannel(const QString &channelname) override;
QString cleanChannelName(const QString &dirtyChannelName) override;
virtual QString cleanChannelName(const QString &dirtyChannelName) override;
virtual bool hasSeparateWriteConnection() const override;
private:
void onMessageSendRequested(TwitchChannel *channel, const QString &message, bool &sent);
@@ -57,6 +58,10 @@ private:
std::queue<std::chrono::steady_clock::time_point> lastMessageMod_;
std::chrono::steady_clock::time_point lastErrorTimeSpeed_;
std::chrono::steady_clock::time_point lastErrorTimeAmount_;
bool singleConnection_ = false;
pajlada::Signals::SignalHolder signalHolder_;
};
} // namespace chatterino