reestablishing connection after it is lost

This commit is contained in:
tastytea 2018-05-12 10:29:57 +02:00
parent dbebbd934f
commit 18b92496e5
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 18 additions and 1 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon project (expandurl-mastodon
VERSION 0.1.3 VERSION 0.2.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -78,6 +78,8 @@ public:
const bool send_reply(const Easy::Status &to_status, const string &message); const bool send_reply(const Easy::Status &to_status, const string &message);
const std::uint_fast64_t get_parent_id(Easy::Notification &notif); const std::uint_fast64_t get_parent_id(Easy::Notification &notif);
const bool stillrunning() const;
private: private:
string _instance; string _instance;
string _access_token; string _access_token;
@ -85,6 +87,7 @@ private:
string _stream; string _stream;
std::unique_ptr<API::http> _ptr; std::unique_ptr<API::http> _ptr;
std::thread _thread; std::thread _thread;
bool _running;
}; };
#endif // EXPANDURL_MASTODON_HPP #endif // EXPANDURL_MASTODON_HPP

View File

@ -74,6 +74,11 @@ int main(int argc, char *argv[])
while (running) while (running)
{ {
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
if (!listener.stillrunning())
{
cerr << "DEBUG: Reestablishing connection...\n";
listener.start();
}
for (Easy::Notification &notif : listener.get_new_messages()) for (Easy::Notification &notif : listener.get_new_messages())
{ {

View File

@ -28,6 +28,7 @@ Listener::Listener()
, _access_token("") , _access_token("")
, _stream("") , _stream("")
, _ptr(nullptr) , _ptr(nullptr)
, _running(false)
{ {
} }
@ -56,10 +57,13 @@ const bool Listener::start()
_thread = std::thread([=] _thread = std::thread([=]
{ {
_running = true;
Easy masto(_instance, _access_token); Easy masto(_instance, _access_token);
masto.set_useragent(static_cast<const string>("expandurl-mastodon/") + masto.set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version); global::version);
masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr); masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr);
cerr << "DEBUG: Connection lost.\n";
_running = false;
}); });
return true; return true;
@ -182,3 +186,8 @@ const std::uint_fast64_t Listener::get_parent_id(Easy::Notification &notif)
} }
return 0; return 0;
} }
const bool Listener::stillrunning() const
{
return _running;
}