Start of thread-safty
This commit is contained in:
parent
c2b5edb2aa
commit
308b77f2c4
2 changed files with 37 additions and 13 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <atomic>
|
||||||
#include "olm/account.hh"
|
#include "olm/account.hh"
|
||||||
|
|
||||||
namespace Matrix {
|
namespace Matrix {
|
||||||
|
@ -47,7 +48,7 @@ private:
|
||||||
Store* store;
|
Store* store;
|
||||||
std::string userIdCache;
|
std::string userIdCache;
|
||||||
std::string deviceIdCache;
|
std::string deviceIdCache;
|
||||||
int requestId = 0;
|
std::atomic_uint32_t requestId = 0;
|
||||||
bool stopSyncing = false;
|
bool stopSyncing = false;
|
||||||
bool isSyncing = false;
|
bool isSyncing = false;
|
||||||
Thread syncThread;
|
Thread syncThread;
|
||||||
|
@ -62,7 +63,7 @@ private:
|
||||||
void processSync(json_t* sync);
|
void processSync(json_t* sync);
|
||||||
void registerFilter();
|
void registerFilter();
|
||||||
json_t* doSync(std::string token, std::string filter, u32 timeout, CURLcode* res);
|
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);
|
json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes);
|
||||||
|
|
||||||
static void print_json(json_t *json);
|
static void print_json(json_t *json);
|
||||||
|
@ -89,7 +90,24 @@ public:
|
||||||
std::string sendText(std::string roomId, std::string text);
|
std::string sendText(std::string roomId, std::string text);
|
||||||
std::string sendMessage(std::string roomId, json_t* content);
|
std::string sendMessage(std::string roomId, json_t* content);
|
||||||
std::string sendEvent(std::string roomId, std::string eventType, 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);
|
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 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 = "");
|
std::string redactEvent(std::string roomId, std::string eventId, std::string reason = "");
|
||||||
|
|
|
@ -374,18 +374,22 @@ std::string Client::sendMessage(std::string roomId, json_t* content) {
|
||||||
return sendEvent(roomId, "m.room.message", 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);
|
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);
|
if (ret) json_decref(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) {
|
std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) {
|
||||||
roomId = resolveRoom(roomId);
|
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);
|
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");
|
const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
|
||||||
if (!eventIdCStr) {
|
if (!eventIdCStr) {
|
||||||
if (ret) json_decref(ret);
|
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) {
|
std::string Client::redactEvent(std::string roomId, std::string eventId, std::string reason) {
|
||||||
roomId = resolveRoom(roomId);
|
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();
|
json_t* content = json_object();
|
||||||
if (reason != "") {
|
if (!reason.empty()) {
|
||||||
json_object_set_new(content, "reason", json_string(reason.c_str()));
|
json_object_set_new(content, "reason", json_string(reason.c_str()));
|
||||||
}
|
}
|
||||||
std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid;
|
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);
|
json_decref(content);
|
||||||
const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
|
const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
|
||||||
if (!eventIdCStr) {
|
if (!eventIdCStr) {
|
||||||
|
@ -772,9 +776,11 @@ size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *u
|
||||||
bool doingCurlRequest = false;
|
bool doingCurlRequest = false;
|
||||||
bool doingHttpcRequest = 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;
|
std::string url = hsUrl + path;
|
||||||
requestId++;
|
if(needsRequest) {
|
||||||
|
requestId++;
|
||||||
|
}
|
||||||
return doRequestCurl(method, url, body, timeout, retRes);
|
return doRequestCurl(method, url, body, timeout, retRes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue