-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketAPI.hpp
228 lines (139 loc) · 8.37 KB
/
WebSocketAPI.hpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*++
Program name:
Apostol CRM
Module Name:
WebSocketAPI.hpp
Notices:
Module: Web Socket API
Author:
Copyright (c) Prepodobny Alen
mailto: alienufo@inbox.ru
mailto: ufocomp@gmail.com
--*/
#ifndef APOSTOL_WEBSOCKETAPI_HPP
#define APOSTOL_WEBSOCKETAPI_HPP
//----------------------------------------------------------------------------------------------------------------------
extern "C++" {
namespace Apostol {
namespace BackEnd {
namespace api {
void observer(CStringList &SQL, const CString &Publisher, const CString &Session, const CString &Identity,
const CString &Data, const CString &Agent, const CString &IP);
}
}
namespace Module {
class CObserverHandler;
typedef std::function<void (CObserverHandler *Handler)> COnObserverHandlerEvent;
//--------------------------------------------------------------------------------------------------------------
//-- CObserverHandler ------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
class CWebSocketAPI;
//--------------------------------------------------------------------------------------------------------------
class CObserverHandler: public CPollConnection {
private:
CWebSocketAPI *m_pModule;
CSession *m_pSession;
CString m_Publisher {};
CString m_Data {};
bool m_Allow;
COnObserverHandlerEvent m_Handler;
int AddToQueue();
void RemoveFromQueue();
protected:
void SetAllow(bool Value) { m_Allow = Value; }
public:
CObserverHandler(CWebSocketAPI *AModule, CSession *ASession, const CString &Publisher, const CString &Data, COnObserverHandlerEvent && Handler);
~CObserverHandler() override;
CSession *Session() { return m_pSession; }
const CString &Publisher() const { return m_Publisher; }
const CString &Data() const { return m_Data; }
bool Allow() const { return m_Allow; };
void Allow(bool Value) { SetAllow(Value); };
bool Handler();
void Close() override;
};
//--------------------------------------------------------------------------------------------------------------
//-- CWebSocketAPI ---------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
typedef std::function<bool (CTCPConnection *AConnection, const CJSON &Token)> COnAuthorizationContinueEvent;
//--------------------------------------------------------------------------------------------------------------
typedef CPollManager CQueueManager;
class CWebSocketAPI: public CApostolModule {
private:
CDateTime m_CheckDate;
CQueue m_Queue;
CQueueManager m_QueueManager;
size_t m_Progress;
size_t m_MaxQueue;
void InitListen();
void CheckListen();
void InitMethods() override;
void UnloadQueue();
void DeleteHandler(CObserverHandler *AHandler);
static void DeleteSession(CSession *ASession);
void CheckSession();
CString VerifyToken(const CString &Token);
static void AfterQuery(CHTTPServerConnection *AConnection, const CString &Path, const CJSON &Payload);
static void QueryException(CPQPollQuery *APollQuery, const Delphi::Exception::Exception &E);
static int CheckError(const CJSON &Json, CString &ErrorMessage, bool RaiseIfError = false);
static CHTTPReply::CStatusType ErrorCodeToStatus(int ErrorCode);
protected:
CSessionManager m_SessionManager;
static bool CheckAuthorizationData(const CHTTPRequest &Request, CAuthorization &Authorization);
static void DoError(const Delphi::Exception::Exception &E);
static void DoCall(CHTTPServerConnection *AConnection, const CString &Action, const CString &Payload);
static void DoCallResult(CHTTPServerConnection *AConnection, const CString &Payload);
static void DoCallResult(CHTTPServerConnection *AConnection, const CString &UniqueId, const CString &Action, const CString &Payload);
static void DoError(CHTTPServerConnection *AConnection, const CString &UniqueId, const CString &Action,
CHTTPReply::CStatusType Status, const CString &Message, const CString &Payload = {});
void DoObserver(CObserverHandler *AHandler);
void DoGet(CHTTPServerConnection *AConnection) override;
virtual void DoPost(CHTTPServerConnection *AConnection);
virtual void DoWebSocket(CHTTPServerConnection *AConnection);
void DoWS(CHTTPServerConnection *AConnection, const CString &Action);
void DoSession(CHTTPServerConnection *AConnection, const CString &Session, const CString &Identity);
void DoSessionDisconnected(CObject *Sender);
void DoPostgresNotify(CPQConnection *AConnection, PGnotify *ANotify) override;
void DoPostgresQueryExecuted(CPQPollQuery *APollQuery) override;
void DoPostgresQueryException(CPQPollQuery *APollQuery, const Delphi::Exception::Exception &E) override;
public:
explicit CWebSocketAPI(CModuleProcess *AProcess, const CString& ModuleName, const CString& SectionName = CString());
~CWebSocketAPI() override = default;
static class CWebSocketAPI *CreateModule(CModuleProcess *AProcess) {
return new CWebSocketAPI(AProcess, "websocket api", "module/WebSocketAPI");
}
int CheckSessionAuthorization(CSession *ASession);
bool CheckAuthorization(CHTTPServerConnection *AConnection, CAuthorization &Authorization);
bool CheckTokenAuthorization(CHTTPServerConnection *AConnection, const CString &Session, CAuthorization &Authorization);
void CheckBearerAuthorization(CHTTPServerConnection *AConnection, CAuthorization &Authorization, COnAuthorizationContinueEvent && OnContinue);
void UnauthorizedFetch(CHTTPServerConnection *AConnection, const CString &UniqueId, const CString &Action,
const CString &Payload, const CString &Agent, const CString &Host);
void AuthorizedFetch(CHTTPServerConnection *AConnection, const CAuthorization &Authorization, const CString &UniqueId,
const CString &Action, const CString &Payload, const CString &Agent, const CString &Host);
void PreSignedFetch(CHTTPServerConnection *AConnection, const CString &UniqueId, const CString &Action,
const CString &Payload, CSession *ASession);
void SignedFetch(CHTTPServerConnection *AConnection, const CString &UniqueId, const CString &Action,
const CString &Payload, const CString &Session, const CString &Nonce, const CString &Signature,
const CString &Agent, const CString &Host, long int ReceiveWindow = 60000);
void Initialization(CModuleProcess *AProcess) override;
bool Execute(CHTTPServerConnection *AConnection) override;
void Heartbeat(CDateTime DateTime) override;
bool Enabled() override;
bool CheckLocation(const CLocation &Location) override;
void IncProgress() { m_Progress++; }
void DecProgress() { m_Progress--; }
int AddToQueue(CObserverHandler *AHandler);
void InsertToQueue(int Index, CObserverHandler *AHandler);
void RemoveFromQueue(CObserverHandler *AHandler);
CQueue &Queue() { return m_Queue; }
const CQueue &Queue() const { return m_Queue; }
CPollManager &QueueManager() { return m_QueueManager; }
const CPollManager &QueueManager() const { return m_QueueManager; }
CSessionManager &SessionManager() { return m_SessionManager; }
const CSessionManager &SessionManager() const { return m_SessionManager; }
};
}
}
using namespace Apostol::Module;
}
#endif //APOSTOL_WEBSOCKETAPI_HPP