Skip to content

Commit

Permalink
webhook multicurl, longer timeout, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 15, 2018
1 parent 9f1fbf6 commit 59e1703
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ const int APP_LOADING = 13;

const int GAME_INGAME = 14;
const int GAME_OBS = 15;
const int GAME_REPLAY = 16;
const int GAME_REPLAY = 16;
3 changes: 0 additions & 3 deletions SC2Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class SC2Data : public QObject {
SC2State* state;
QTimer* timer;
vector<Observer*> watchers;
//QNetworkAccessManager networkManager;
//QUrl uiUrl;
//QUrl gameUrl;

public:
explicit SC2Data(QObject* parent = Q_NULLPTR);
Expand Down
58 changes: 45 additions & 13 deletions Webhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,64 @@ void Webhook::notify(SC2State*& previous, SC2State*& current) {

void Webhook::sendRequest(SC2State*& game, std::string event) {
if(!game->fullState.isReplay) {
CURL *curl;
CURLcode res;

Config* cfg = Config::Current();

int still_running = 0;
int repeats = 0;
CURLM *multi_handle;
multi_handle = curl_multi_init();

// todo: make this a multi curl
// vector of handles
vector<CURL*> handles;

for (string &url : cfg->webhookURLList) {
curl = curl_easy_init();
if (curl) {
CURL *handle;
handle = curl_easy_init();
if (handle) {
curl_multi_add_handle(multi_handle, handle);
handles.push_back(handle);
std::string qdelim = "?";
std::size_t found = url.find(qdelim);
if (found!=std::string::npos) {
qdelim = "&";
}

std::string resp = getJSONStringFromSC2State(game, event);
std::string c_url = url + qdelim + "json=" + curl_easy_escape(curl, resp.c_str(), 0);
curl_easy_setopt(curl, CURLOPT_URL, c_url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 150);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
// pass
std::string c_url = url + qdelim + "json=" + curl_easy_escape(handle, resp.c_str(), 0);
curl_easy_setopt(handle, CURLOPT_URL, c_url.c_str());
curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, 500);
}
}

curl_multi_perform(multi_handle, &still_running);
while(still_running) {
CURLMcode mc; /* curl_multi_wait() return code */
int numfds;
mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);

if(mc != CURLM_OK) {
break;
}

if(!numfds) {
repeats++;
if(repeats > 1) {
break;
}
}
else {
repeats = 0;
}

curl_multi_perform(multi_handle, &still_running);
}

// clean up each handle
for (CURL* &handle : handles) {
curl_multi_remove_handle(multi_handle, handle);
curl_easy_cleanup(handle);
}
curl_multi_cleanup(multi_handle);
}
}

Expand Down

0 comments on commit 59e1703

Please sign in to comment.