Line data Source code
1 : #ifndef THINGER_HTTP_CLIENT_CONNECTION_HPP
2 : #define THINGER_HTTP_CLIENT_CONNECTION_HPP
3 :
4 : #include <memory>
5 : #include <mutex>
6 : #include <boost/noncopyable.hpp>
7 :
8 : #include "../common/http_request.hpp"
9 : #include "../common/http_response.hpp"
10 : #include "response_factory.hpp"
11 : #include "stream_types.hpp"
12 : #include "../../asio/sockets/tcp_socket.hpp"
13 : #include "../../asio/sockets/unix_socket.hpp"
14 : #include "../../util/types.hpp"
15 :
16 : namespace thinger::http {
17 :
18 : class client_connection : public std::enable_shared_from_this<client_connection>, public boost::noncopyable {
19 :
20 : static constexpr unsigned MAX_BUFFER_SIZE = 4096;
21 : static constexpr unsigned MAX_RETRIES = 3;
22 : static constexpr auto DEFAULT_TIMEOUT = std::chrono::seconds{60};
23 : static constexpr auto CONNECT_TIMEOUT = std::chrono::seconds{10};
24 :
25 : public:
26 : static std::atomic<unsigned long> connections;
27 :
28 : // Constructors
29 : explicit client_connection(std::shared_ptr<thinger::asio::socket> socket,
30 : std::chrono::seconds timeout = DEFAULT_TIMEOUT);
31 :
32 : client_connection(std::shared_ptr<thinger::asio::unix_socket> socket,
33 : const std::string& path,
34 : std::chrono::seconds timeout = DEFAULT_TIMEOUT);
35 :
36 : virtual ~client_connection();
37 :
38 : // Main operation - send request and get response
39 : awaitable<std::shared_ptr<http_response>> send_request(std::shared_ptr<http_request> request);
40 :
41 : // Streaming operation - send request and stream response through callback
42 : awaitable<stream_result> send_request_streaming(
43 : std::shared_ptr<http_request> request,
44 : stream_callback callback);
45 :
46 : // Connection management
47 : void close();
48 : std::shared_ptr<thinger::asio::socket> release_socket();
49 : std::shared_ptr<thinger::asio::socket> get_socket() const { return socket_; }
50 277 : bool is_open() const { return socket_ && socket_->is_open(); }
51 :
52 : private:
53 : // Internal helpers
54 : awaitable<void> ensure_connected(const http_request& request);
55 : awaitable<std::shared_ptr<http_response>> read_response(bool head_request);
56 :
57 : std::shared_ptr<thinger::asio::socket> socket_;
58 : std::string socket_path_;
59 : std::chrono::seconds timeout_;
60 : uint8_t buffer_[MAX_BUFFER_SIZE];
61 : response_factory response_parser_;
62 : std::mutex connection_mutex_;
63 : };
64 :
65 : }
66 :
67 : #endif
|