Kurlyk
Loading...
Searching...
No Matches
threaded_request_processing_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <kurlyk.hpp>
3#include <thread>
4#include <atomic>
5
6int main() {
7 // Initialize the library in synchronous mode (own-thread handling)
8 kurlyk::init(false);
9
10 std::atomic<bool> running{true}; // Atomic flag to control the processing loop
11
12 // Start a separate thread to process requests
13 std::thread processing_thread([&running](){
14 while (running) {
15 kurlyk::process(); // Processes pending requests in synchronous mode
16 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Throttle to avoid busy-waiting
17 }
18 // Deinitialize the library
20 });
21
22 // Set up an HTTP client and configure requests
23 kurlyk::HttpClient client("https://httpbin.org");
24 client.set_user_agent("KurlykClient/1.0");
25 client.set_timeout(10); // Timeout for requests
26 client.set_retry_attempts(3, 1000); // Retry up to 3 times with 1s delay
27
28 // Send a GET request
29 KURLYK_PRINT << "Sending GET request..." << std::endl;
30 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
31 [&running](const kurlyk::HttpResponsePtr response) {
33 << "GET Response Content: " << response->content << std::endl
34 << "Status Code: " << response->status_code << std::endl;
35 if (response->ready) running = false;
36 });
37
38 // Signal the processing thread to stop
39 processing_thread.join(); // Wait for the processing thread to finish
40
41 KURLYK_PRINT << "Request processing completed. Exiting program." << std::endl;
42 return 0;
43}
A client class for making HTTP requests to a specific host.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
void shutdown()
Shuts down all network operations, resetting the state of the network worker and clearing pending req...
Definition runtime.hpp:38
std::unique_ptr< HttpResponse > HttpResponsePtr
A unique pointer to an HttpResponse object for memory management.
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
void process()
Processes pending requests (used in synchronous mode).
Definition runtime.hpp:32
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT