-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathclient.hpp
More file actions
126 lines (100 loc) · 4.23 KB
/
client.hpp
File metadata and controls
126 lines (100 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <tiny_websockets/internals/ws_common.hpp>
#include <tiny_websockets/network/tcp_client.hpp>
#include <tiny_websockets/internals/data_frame.hpp>
#include <tiny_websockets/internals/websockets_endpoint.hpp>
#include <tiny_websockets/message.hpp>
#include <memory>
#include <functional>
#include <vector>
namespace websockets {
enum class WebsocketsEvent {
ConnectionOpened,
ConnectionClosed,
GotPing, GotPong
};
class WebsocketsClient;
typedef std::function<void(WebsocketsClient&, WebsocketsMessage)> MessageCallback;
typedef std::function<void(WebsocketsMessage)> PartialMessageCallback;
typedef std::function<void(WebsocketsClient&, WebsocketsEvent, WSInterfaceString)> EventCallback;
typedef std::function<void(WebsocketsEvent, WSInterfaceString)> PartialEventCallback;
class WebsocketsClient {
public:
WebsocketsClient();
WebsocketsClient(std::shared_ptr<network::TcpClient> client);
WebsocketsClient(const WebsocketsClient& other) = delete;
WebsocketsClient(const WebsocketsClient&& other) = delete;
WebsocketsClient& operator=(const WebsocketsClient& other) = delete;
WebsocketsClient& operator=(const WebsocketsClient&& other) = delete;
void addHeader(const WSInterfaceString key, const WSInterfaceString value);
bool connect(const WSInterfaceString url);
bool connect(const WSInterfaceString host, const int port, const WSInterfaceString path);
void onMessage(const MessageCallback callback);
void onMessage(const PartialMessageCallback callback);
void onEvent(const EventCallback callback);
void onEvent(const PartialEventCallback callback);
bool poll();
bool available(const bool activeTest = false);
bool send(const WSInterfaceString&& data);
bool send(const WSInterfaceString& data);
bool send(const char* data);
bool send(const char* data, const size_t len);
bool sendBinary(const WSInterfaceString data);
bool sendBinary(const char* data, const size_t len);
// stream messages
bool stream(const WSInterfaceString data = "");
bool streamBinary(const WSInterfaceString data = "");
bool end(const WSInterfaceString data = "");
void setFragmentsPolicy(const FragmentsPolicy newPolicy);
FragmentsPolicy getFragmentsPolicy() const;
WebsocketsMessage readBlocking();
bool ping(const WSInterfaceString data = "");
bool pong(const WSInterfaceString data = "");
void close(const CloseReason reason = CloseReason_NormalClosure);
CloseReason getCloseReason() const;
void setUseMasking(bool useMasking) {
_endpoint.setUseMasking(useMasking);
}
void setInsecure();
#ifdef ESP8266
void setFingerprint(const char* fingerprint);
void setClientRSACert(const X509List *cert, const PrivateKey *sk);
void setClientECCert(const X509List *cert, const PrivateKey *sk);
void setTrustAnchors(const X509List *ta);
void setKnownKey(const PublicKey *pk);
#elif defined(ESP32)
void setCACert(const char* ca_cert);
void setCertificate(const char* client_ca);
void setPrivateKey(const char* private_key);
#endif
virtual ~WebsocketsClient();
private:
std::shared_ptr<network::TcpClient> _client;
std::vector<std::pair<WSString, WSString>> _customHeaders;
internals::WebsocketsEndpoint _endpoint;
bool _connectionOpen;
MessageCallback _messagesCallback;
EventCallback _eventsCallback;
enum SendMode {
SendMode_Normal,
SendMode_Streaming
} _sendMode;
#ifdef ESP8266
const char* _optional_ssl_fingerprint = nullptr;
const X509List* _optional_ssl_trust_anchors = nullptr;
const PublicKey* _optional_ssl_known_key = nullptr;
const X509List* _optional_ssl_rsa_cert = nullptr;
const PrivateKey* _optional_ssl_rsa_private_key = nullptr;
const X509List* _optional_ssl_ec_cert = nullptr;
const PrivateKey* _optional_ssl_ec_private_key = nullptr;
#elif defined(ESP32)
const char* _optional_ssl_ca_cert = nullptr;
const char* _optional_ssl_client_ca = nullptr;
const char* _optional_ssl_private_key = nullptr;
#endif
void _handlePing(WebsocketsMessage);
void _handlePong(WebsocketsMessage);
void _handleClose(WebsocketsMessage);
void upgradeToSecuredConnection();
};
}