Line data Source code
1 : #ifndef HTTP_STREAM_HPP
2 : #define HTTP_STREAM_HPP
3 :
4 : #include <queue>
5 : #include <memory>
6 : #include <functional>
7 : #include "../common/http_frame.hpp"
8 :
9 : namespace thinger::http {
10 :
11 : typedef uint32_t stream_id;
12 :
13 : /**
14 : * A HTTP stream represents a communication channel inside a single HTTP connection. As a
15 : * single connection can be used for multiple HTTP request, each request will generate a new
16 : * HTTP stream. In HTTP 1.1, all requests in a connection must be answered in order, even if
17 : * the responses are generated in a different order. In HTTP 2.0, it is possible to answer
18 : * asynchronously to each single stream within a connection using a stream identifier.
19 : * TODO check HTTP 2.0 support.
20 : */
21 : class http_stream {
22 :
23 : private:
24 : /**
25 : * Unique identifier within a http connection, generate for each request
26 : */
27 : stream_id stream_id_;
28 :
29 : /**
30 : * Queue for each HTTP frame composing a response. A response can be composed on several frames
31 : * i.e., while sending large files
32 : */
33 : std::queue<std::shared_ptr<http_frame>> queue_;
34 :
35 : /**
36 : * Callback to be able to register a function when the stream was completed, i.e., completed a
37 : * response to a given query.
38 : */
39 : std::function<void()> stream_callback_;
40 :
41 : bool keep_alive_;
42 :
43 : public:
44 906 : http_stream(stream_id stream_id, bool keep_alive) : stream_id_(stream_id), keep_alive_(keep_alive) {}
45 :
46 906 : virtual ~http_stream() {}
47 :
48 : public:
49 :
50 : size_t get_queue_size() const;
51 :
52 : bool empty_queue() const;
53 :
54 : std::shared_ptr<http_frame> current_frame() const;
55 :
56 : void pop_frame();
57 :
58 : void add_frame(std::shared_ptr<http_frame> frame);
59 :
60 : size_t get_queued_frames() const;
61 :
62 : void on_completed(std::function<void()> callback);
63 :
64 : void completed();
65 :
66 : stream_id id() const;
67 :
68 1774 : bool keep_alive() const {
69 1774 : return keep_alive_;
70 : }
71 : };
72 :
73 : }
74 :
75 : #endif
|