* @brief Pushes the queue of events to the endpoint server * @details if failure occurs, will re-try 5 times until success * @return true/false, indicating successful network request * @usage Should not be directly called. A worker thread is created in the class constructor to handle data pushes */
| 88 | * @usage Should not be directly called. A worker thread is created in the class constructor to handle data pushes |
| 89 | */ |
| 90 | bool EventLogger::SendDataToServer() |
| 91 | { |
| 92 | if (this->ServerEndpoint.empty()) |
| 93 | { |
| 94 | Logger::log(Err, "Server endpoint is not set. Cannot send event data."); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | int sendFailureCount = 0; |
| 99 | |
| 100 | while (true) |
| 101 | { |
| 102 | GameEvent event; |
| 103 | |
| 104 | { |
| 105 | std::lock_guard<std::mutex> lock(mtx); |
| 106 | if (eventQueue.empty()) |
| 107 | break; |
| 108 | |
| 109 | event = eventQueue.front(); |
| 110 | eventQueue.pop(); |
| 111 | } |
| 112 | |
| 113 | if (event.details.empty() || event.playerID.empty() || event.timestamp == 0) |
| 114 | continue; |
| 115 | |
| 116 | json j = |
| 117 | { |
| 118 | {"type", static_cast<int>(event.type)}, |
| 119 | {"playerID", event.playerID}, |
| 120 | {"timestamp", event.timestamp}, |
| 121 | {"details", event.details} |
| 122 | }; |
| 123 | |
| 124 | HttpRequest request; |
| 125 | request.url = ServerEndpoint; |
| 126 | request.cookie = ""; |
| 127 | request.body = j.dump(); |
| 128 | request.requestHeaders = |
| 129 | { |
| 130 | "Content-Type: application/json", |
| 131 | "Accept: application/json", |
| 132 | "User-Agent: GameEventLogger/1.0" |
| 133 | }; |
| 134 | |
| 135 | if (!HttpClient::PostRequest(request) || (request.responseHeaders.empty() && sendFailureCount < 5) ) |
| 136 | { |
| 137 | Logger::log(Err, "Failed to send event data to server."); |
| 138 | sendFailureCount++; |
| 139 | |
| 140 | std::lock_guard<std::mutex> lock(mtx); |
| 141 | eventQueue.push(event); |
| 142 | continue; |
| 143 | } |
| 144 | else if (sendFailureCount >= 5) |
| 145 | { |
| 146 | Logger::log(Err, "Failed to send event data after multiple attempts."); |
| 147 | std::lock_guard<std::mutex> lock(mtx); |