Start of thread-safty

This commit is contained in:
timoreo 2023-12-11 16:06:09 +01:00
parent c2b5edb2aa
commit 308b77f2c4
Signed by: timoreo
GPG key ID: 121A72C3512BA288
2 changed files with 37 additions and 13 deletions

View file

@ -8,6 +8,7 @@
#include <memory>
#include <string>
#include <vector>
#include <atomic>
#include "olm/account.hh"
namespace Matrix {
@ -47,7 +48,7 @@ private:
Store* store;
std::string userIdCache;
std::string deviceIdCache;
int requestId = 0;
std::atomic_uint32_t requestId = 0;
bool stopSyncing = false;
bool isSyncing = false;
Thread syncThread;
@ -62,7 +63,7 @@ private:
void processSync(json_t* sync);
void registerFilter();
json_t* doSync(std::string token, std::string filter, u32 timeout, CURLcode* res);
json_t* doRequest(const char* method, std::string path, json_t* body = NULL, u32 timeout = 5, CURLcode* retRes = NULL);
json_t* doRequest(const char* method, std::string path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool doRequest = true);
json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes);
static void print_json(json_t *json);
@ -89,7 +90,24 @@ public:
std::string sendText(std::string roomId, std::string text);
std::string sendMessage(std::string roomId, json_t* content);
std::string sendEvent(std::string roomId, std::string eventType, json_t* content);
void sendEventToDevice(const std::string& eventType, json_t* devices, json_t* content);
// Format Json in shape
// {
// "@alice:example.com": {
// "TLLBEANAAG": {
// "example_content_key": "value"
// }
// }
// }
// OR, for all devices
// {
// "@alice:example.com": {
// "*": {
// "example_content_key": "value"
// }
// }
// }
void sendEventToDevice(const std::string& eventType, json_t* content);
json_t* getStateEvent(std::string roomId, std::string type, std::string stateKey);
std::string sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content);
std::string redactEvent(std::string roomId, std::string eventId, std::string reason = "");

View file

@ -374,18 +374,22 @@ std::string Client::sendMessage(std::string roomId, json_t* content) {
return sendEvent(roomId, "m.room.message", content);
}
void Client::sendEventToDevice(const std::string& eventType, json_t* devices, json_t* content){
std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId);
void Client::sendEventToDevice(const std::string& eventType, json_t* content){
std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++);
std::string path = "/_matrix/client/v3/sendToDevice/" + urlencode(eventType) + "/" + urlencode(txid);
json_t* ret = doRequest("PUT", path, content);
json_t* messages = json_object();
json_object_set(messages, "messages", content);
json_t* ret = doRequest("PUT", path, messages, 5, nullptr, false);
json_decref(messages);
if (ret) json_decref(ret);
}
std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) {
roomId = resolveRoom(roomId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++);
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid);
json_t* ret = doRequest("PUT", path, content);
json_t* ret = doRequest("PUT", path, content, 5, nullptr, false);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) {
if (ret) json_decref(ret);
@ -418,13 +422,13 @@ std::string Client::sendStateEvent(std::string roomId, std::string type, std::st
std::string Client::redactEvent(std::string roomId, std::string eventId, std::string reason) {
roomId = resolveRoom(roomId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++);
json_t* content = json_object();
if (reason != "") {
if (!reason.empty()) {
json_object_set_new(content, "reason", json_string(reason.c_str()));
}
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid;
json_t* ret = doRequest("PUT", path, content);
json_t* ret = doRequest("PUT", path, content, 5, nullptr, false);
json_decref(content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) {
@ -772,9 +776,11 @@ size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *u
bool doingCurlRequest = false;
bool doingHttpcRequest = false;
json_t* Client::doRequest(const char* method, std::string path, json_t* body, u32 timeout, CURLcode* retRes) {
json_t* Client::doRequest(const char* method, std::string path, json_t* body, u32 timeout, CURLcode* retRes, bool needsRequest) {
std::string url = hsUrl + path;
requestId++;
if(needsRequest) {
requestId++;
}
return doRequestCurl(method, url, body, timeout, retRes);
}