/src/pistache/include/pistache/peer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-FileCopyrightText: 2015 Mathieu Stefani |
3 | | * |
4 | | * SPDX-License-Identifier: Apache-2.0 |
5 | | */ |
6 | | |
7 | | /* peer.h |
8 | | Mathieu Stefani, 12 August 2015 |
9 | | |
10 | | A class representing a TCP Peer |
11 | | */ |
12 | | |
13 | | #pragma once |
14 | | |
15 | | #include <iostream> |
16 | | #include <memory> |
17 | | #include <string> |
18 | | |
19 | | #include <pistache/async.h> |
20 | | #include <pistache/http.h> |
21 | | #include <pistache/net.h> |
22 | | #include <pistache/os.h> |
23 | | #include <pistache/stream.h> |
24 | | |
25 | | #ifdef PISTACHE_USE_SSL |
26 | | |
27 | | #include <openssl/err.h> |
28 | | #include <openssl/ssl.h> |
29 | | |
30 | | #endif /* PISTACHE_USE_SSL */ |
31 | | |
32 | | namespace Pistache::Tcp |
33 | | { |
34 | | |
35 | | class Transport; |
36 | | |
37 | | class Peer |
38 | | { |
39 | | public: |
40 | | friend class Transport; |
41 | | friend class Http::Handler; |
42 | | friend class Http::Timeout; |
43 | | |
44 | | ~Peer(); |
45 | | |
46 | | static std::shared_ptr<Peer> Create(Fd fd, const Address& addr); |
47 | | static std::shared_ptr<Peer> CreateSSL(Fd fd, const Address& addr, void* ssl); |
48 | | |
49 | | // true: there is no http request on the keepalive peer -> only call removePeer |
50 | | // false: there is at least one http request on the peer(keepalive or not) -> send 408 message firsst, then call removePeer |
51 | | void setIdle(bool bIdle); |
52 | | bool isIdle() const; |
53 | | |
54 | | const Address& address() const; |
55 | | const std::string& hostname(); |
56 | | Fd fd() const; // can return PS_FD_EMPTY |
57 | | em_socket_t actualFd() const; // can return -1 |
58 | | |
59 | | void closeFd(); |
60 | | |
61 | | void* ssl() const; |
62 | | |
63 | | void putData(std::string name, std::shared_ptr<void> data); |
64 | | std::shared_ptr<void> getData(std::string name) const; |
65 | | std::shared_ptr<void> tryGetData(std::string name) const; |
66 | | |
67 | | Async::Promise<PST_SSIZE_T> send(const RawBuffer& buffer, |
68 | | int flags = 0); |
69 | | size_t getID() const; |
70 | | |
71 | | protected: |
72 | | // (provide default constructor so child class ConcretePeer can have |
73 | | // default constructor) |
74 | | Peer() |
75 | | : id_(0) |
76 | 0 | { } |
77 | | |
78 | | Peer(Fd fd, const Address& addr, void* ssl); |
79 | | |
80 | | private: |
81 | | void associateTransport(Transport* transport); |
82 | | Transport* transport() const; |
83 | | static size_t getUniqueId(); |
84 | | |
85 | | Transport* transport_ = nullptr; |
86 | | |
87 | | Fd fd_ = PS_FD_EMPTY; |
88 | | |
89 | | Address addr; |
90 | | |
91 | | std::string hostname_; |
92 | | std::unordered_map<std::string, std::shared_ptr<void>> data_; |
93 | | |
94 | | void* ssl_ = nullptr; |
95 | | const size_t id_; |
96 | | bool isIdle_ = false; |
97 | | }; |
98 | | |
99 | | std::ostream& operator<<(std::ostream& os, Peer& peer); |
100 | | |
101 | | } // namespace Pistache::Tcp |