summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorllnulldisk <48621230+llnulldisk@users.noreply.github.com>2023-01-29 14:54:20 +0100
committerllnulldisk <48621230+llnulldisk@users.noreply.github.com>2023-01-29 14:54:20 +0100
commita8defacf2ba9bc3029b16af48a539553f873866b (patch)
tree8cea4cad5a8bc15e8213193743111f41a73e1461
parent443b769670046340e266893e00f947c9ce37462f (diff)
Fix timeout issues (#251)
-rw-r--r--include/tgbot/Api.h3
-rw-r--r--include/tgbot/net/HttpClient.h2
-rw-r--r--include/tgbot/net/TgLongPoll.h8
-rw-r--r--src/net/BoostHttpOnlySslClient.cpp2
-rw-r--r--src/net/CurlHttpClient.cpp2
-rw-r--r--src/net/TgLongPoll.cpp17
6 files changed, 19 insertions, 15 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h
index c3ae922..b34a10d 100644
--- a/include/tgbot/Api.h
+++ b/include/tgbot/Api.h
@@ -1982,12 +1982,13 @@ public:
* @return Returns True if bot is blocked by user
*/
bool blockedByUser(std::int64_t chatId) const;
+
+ const HttpClient& _httpClient;
private:
boost::property_tree::ptree sendRequest(const std::string& method, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const;
const std::string _token;
- const HttpClient& _httpClient;
const TgTypeParser _tgTypeParser;
const std::string _url;
};
diff --git a/include/tgbot/net/HttpClient.h b/include/tgbot/net/HttpClient.h
index a35dd7d..024c8ad 100644
--- a/include/tgbot/net/HttpClient.h
+++ b/include/tgbot/net/HttpClient.h
@@ -26,6 +26,8 @@ public:
* If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded.
*/
virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;
+
+ std::int32_t _timeout = 25;
};
}
diff --git a/include/tgbot/net/TgLongPoll.h b/include/tgbot/net/TgLongPoll.h
index 417260a..9c50407 100644
--- a/include/tgbot/net/TgLongPoll.h
+++ b/include/tgbot/net/TgLongPoll.h
@@ -1,6 +1,7 @@
#ifndef TGBOT_TGLONGPOLL_H
#define TGBOT_TGLONGPOLL_H
+#include "tgbot/Api.h"
#include "tgbot/export.h"
#include <cstdint>
@@ -10,7 +11,6 @@
namespace TgBot {
-class Api;
class Bot;
class EventHandler;
@@ -22,8 +22,8 @@ class EventHandler;
class TGBOT_API TgLongPoll {
public:
- TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t, std::int32_t, std::shared_ptr<std::vector<std::string>>);
- TgLongPoll(const Bot& bot, std::int32_t = 100, std::int32_t = 10, const std::shared_ptr<std::vector<std::string>>& = nullptr);
+ TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates);
+ TgLongPoll(const Bot& bot, std::int32_t limit = 100, std::int32_t timeout = 10, const std::shared_ptr<std::vector<std::string>>& allowUpdates = nullptr);
/**
* @brief Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop.
@@ -37,6 +37,8 @@ private:
std::int32_t _limit;
std::int32_t _timeout;
std::shared_ptr<std::vector<std::string>> _allowUpdates;
+
+ std::vector<Update::Ptr> _updates;
};
}
diff --git a/src/net/BoostHttpOnlySslClient.cpp b/src/net/BoostHttpOnlySslClient.cpp
index 6e59649..82bd897 100644
--- a/src/net/BoostHttpOnlySslClient.cpp
+++ b/src/net/BoostHttpOnlySslClient.cpp
@@ -52,7 +52,7 @@ string BoostHttpOnlySslClient::makeRequest(const Url& url, const vector<HttpReqA
struct timeval timeStruct;
// set the timeout to 20 seconds
- timeStruct.tv_sec = 20;
+ timeStruct.tv_sec = _timeout;
timeStruct.tv_usec = 0;
FD_ZERO(&fileDescriptorSet);
diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp
index 2a397f9..c487898 100644
--- a/src/net/CurlHttpClient.cpp
+++ b/src/net/CurlHttpClient.cpp
@@ -11,7 +11,7 @@ CurlHttpClient::CurlHttpClient() : _httpParser() {
curlSettings = curl_easy_init();
curl_easy_setopt(curlSettings, CURLOPT_CONNECTTIMEOUT, 20);
- curl_easy_setopt(curlSettings, CURLOPT_TIMEOUT, 25);
+ curl_easy_setopt(curlSettings, CURLOPT_TIMEOUT, _timeout);
}
CurlHttpClient::~CurlHttpClient() {
diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp
index 6a224cb..70333dd 100644
--- a/src/net/TgLongPoll.cpp
+++ b/src/net/TgLongPoll.cpp
@@ -12,20 +12,19 @@
namespace TgBot {
TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates)
- : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout),
- _allowUpdates(std::move(allowUpdates)) {
+ : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout)
+ , _allowUpdates(std::move(allowUpdates)) {
+
+ const_cast<TgBot::HttpClient&>(_api->_httpClient)._timeout = _timeout + 5;
}
-TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates) :
- TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) {
+TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates)
+ : TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) {
}
void TgLongPoll::start() {
- // get all unconfirmed updates
- std::vector<Update::Ptr> updates = _api->getUpdates(0, _limit, _timeout, _allowUpdates);
-
// handle updates
- for (Update::Ptr& item : updates) {
+ for (Update::Ptr& item : _updates) {
if (item->updateId >= _lastUpdateId) {
_lastUpdateId = item->updateId + 1;
}
@@ -33,7 +32,7 @@ void TgLongPoll::start() {
}
// confirm handled updates
- _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates);
+ _updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates);
}
}