| Qore ServerSentEventHandler Module Reference 1.0.2

Loading...
Searching...
No Matches
ServerSentEventHandler.qc.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* ServerSentEventHandler.qc Copyright 2025 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
26 namespace ServerSentEventHandler {
28
44 class ServerSentEventHandler : public HttpServer::AbstractHttpSocketHandler, public Logger::LoggerWrapper {
45
46 public:
48 const DefaultSocketSendTimeout = 30s;
49
51 const DefaultHeartbeatInterval = 20;
52
54 const DefaultHeartbeatMsg = ":hb\n\n";
55
57 const Options = ...;
58
59
61 const SseResponseHeader = ...;
62
63
65 const DataSerializationSupport = ...;
66
67
68 protected:
70 RWLock rwl();
71
73 string serialization;
74
76 code serializer;
77
79 hash<string, ServerSentEventConnection> ch;
80
82 int heartbeat = DefaultHeartbeatInterval;
83
85 string heartbeat_msg = DefaultHeartbeatMsg;
86
87 public:
88
89 private:
91 string tlk = get_random_string(50);
92
93 public:
94
96
107 constructor(*HttpServer::AbstractAuthenticator auth, *hash<auto> opts) ;
108
109
111 constructor(hash<auto> opts) ;
112
113
115
117 int getHeartbeat(ServerSentEventConnection conn);
118
119
121
123 setHeartbeat(softint seconds);
124
125
127
129 setHeartbeat(date seconds);
130
131
133
164 hash<HttpResponseInfo> handleRequest(hash<auto> cx, hash<auto> hdr, *data b);
165
166
168
170 *list<string> getConnectionIds();
171
172
174
190 bool startImpl(softstring lid, hash<auto> cx, hash<auto> hdr, Qore::Socket sock);
191
192
194
196 bool flushIo(ServerSentEventConnection conn, Socket sock);
197
198
200 string encodeMessage(hash<SseMessageInfo> msg);
201
202
204 static string serializeJson(auto v);
205
207 static string serializeDefault(auto v);
208
210 ServerSentEventConnection getConnectionImpl(Socket sock, hash<auto> cx, hash<auto> hdr, string cid);
211
212
214 ServerSentEventConnection doNewConnection(Socket sock, hash<auto> cx, hash<auto> hdr, string cid);
215
216
218 connectionRegisteredImpl(ServerSentEventConnection conn);
219
220
222 deregisterConnectionImpl(ServerSentEventConnection conn);
223
224
226
230 sendAll(hash<SseMessageInfo> msg);
231
232
234
240 sendOne(softstring id, hash<SseMessageInfo> msg);
241
242
244 stopOne(softstring id);
245
246
248 logInfo(string fmt, ...);
249
250
252 logError(string fmt, ...);
253
254
256 logWarn(string fmt, ...);
257
258
260 logDebug(string fmt, ...);
261
262
264 protected:
265 string getHeartbeatMessage(ServerSentEventConnection conn);
266 public:
267
268
270 protected:
271 registerNewConnectionIntern(string cid, ServerSentEventConnection conn);
272 public:
273
274
276 protected:
277 removeConnectionLocked(string cid);
278 public:
279
280
282 protected:
283 sendHeartbeatMsg(ServerSentEventConnection conn, Socket sock);
284 public:
285
286
288 protected:
289 replayEventsSince(ServerSentEventConnection conn, auto id);
290 public:
291
292
294 protected:
295 constructorInit(*hash<auto> opts);
296 public:
297
298
299 private:
300 bool flushIoIntern(ServerSentEventConnection conn, Socket sock, bool update_tld);
301 public:
302
303};
304};
HttpServer::AbstractAuthenticator
HttpServer::AbstractHttpRequestHandler::auth
*AbstractAuthenticator auth
HttpServer::AbstractHttpSocketHandler
Logger::LoggerWrapper
Qore::Socket
ServerSentEventHandler::ServerSentEventConnection
this class represents a connection to an HTTP client
Definition ServerSentEventConnection.qc.dox.h:37
ServerSentEventHandler::ServerSentEventHandler::constructor
constructor(*HttpServer::AbstractAuthenticator auth, *hash< auto > opts)
Create the object optionally with the given AbstractAuthenticator.
ServerSentEventHandler::ServerSentEventHandler::heartbeat
int heartbeat
heartbeat interval in seconds
Definition ServerSentEventHandler.qc.dox.h:82
ServerSentEventHandler::ServerSentEventHandler::removeConnectionLocked
removeConnectionLocked(string cid)
Called when a connection is removed from the connection map.
ServerSentEventHandler::ServerSentEventHandler::setHeartbeat
setHeartbeat(date seconds)
sets the heartbeat interval as a number of seconds
ServerSentEventHandler::ServerSentEventHandler::sendOne
sendOne(softstring id, hash< SseMessageInfo > msg)
sends a message to the given connection ID
ServerSentEventHandler::ServerSentEventHandler::serializeDefault
static string serializeDefault(auto v)
Default value serialization.
ServerSentEventHandler::ServerSentEventHandler::getHeartbeatMessage
string getHeartbeatMessage(ServerSentEventConnection conn)
Returns the heartbeat message to send.
ServerSentEventHandler::ServerSentEventHandler::DefaultHeartbeatMsg
const DefaultHeartbeatMsg
default heartbeat message; this is a short comment to be used as a heartbeat message
Definition ServerSentEventHandler.qc.dox.h:54
ServerSentEventHandler::ServerSentEventHandler::constructorInit
constructorInit(*hash< auto > opts)
Common constructor initialization.
ServerSentEventHandler::ServerSentEventHandler::getConnectionImpl
ServerSentEventConnection getConnectionImpl(Socket sock, hash< auto > cx, hash< auto > hdr, string cid)
Called when a connection is established; the default implementation creates a ServerSentEventConnecti...
ServerSentEventHandler::ServerSentEventHandler::serialization
string serialization
The value of the serialization option.
Definition ServerSentEventHandler.qc.dox.h:73
ServerSentEventHandler::ServerSentEventHandler::sendHeartbeatMsg
sendHeartbeatMsg(ServerSentEventConnection conn, Socket sock)
Send the default heartbeat message.
ServerSentEventHandler::ServerSentEventHandler::sendAll
sendAll(hash< SseMessageInfo > msg)
sends a message to all connected clients
ServerSentEventHandler::ServerSentEventHandler::logError
logError(string fmt,...)
Log to the logger.
ServerSentEventHandler::ServerSentEventHandler::logDebug
logDebug(string fmt,...)
Log to the logger.
ServerSentEventHandler::ServerSentEventHandler::startImpl
bool startImpl(softstring lid, hash< auto > cx, hash< auto > hdr, Qore::Socket sock)
Called from the HTTP server after the handleRequest() method indicates that a dedicated connection sh...
ServerSentEventHandler::ServerSentEventHandler::tlk
string tlk
Thread-local key for I/O check.
Definition ServerSentEventHandler.qc.dox.h:91
ServerSentEventHandler::ServerSentEventHandler::heartbeat_msg
string heartbeat_msg
heartbeat message returned by getHeartbeatMessage()
Definition ServerSentEventHandler.qc.dox.h:85
ServerSentEventHandler::ServerSentEventHandler::rwl
RWLock rwl()
connection read-write lock
ServerSentEventHandler::ServerSentEventHandler::doNewConnection
ServerSentEventConnection doNewConnection(Socket sock, hash< auto > cx, hash< auto > hdr, string cid)
Called when a connection is established; registers the new connection internally.
ServerSentEventHandler::ServerSentEventHandler::serializeJson
static string serializeJson(auto v)
JSON value serialization.
ServerSentEventHandler::ServerSentEventHandler::constructor
constructor(hash< auto > opts)
Create the object optionally with the given optiond.
ServerSentEventHandler::ServerSentEventHandler::getConnectionIds
*list< string > getConnectionIds()
get list of socket connection ids
ServerSentEventHandler::ServerSentEventHandler::Options
const Options
known constructor options
Definition ServerSentEventHandler.qc.dox.h:57
ServerSentEventHandler::ServerSentEventHandler::connectionRegisteredImpl
connectionRegisteredImpl(ServerSentEventConnection conn)
Called when the connection is registered; the default implementation does nothing.
ServerSentEventHandler::ServerSentEventHandler::handleRequest
hash< HttpResponseInfo > handleRequest(hash< auto > cx, hash< auto > hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
ServerSentEventHandler::ServerSentEventHandler::logWarn
logWarn(string fmt,...)
Log to the logger.
ServerSentEventHandler::ServerSentEventHandler::flushIo
bool flushIo(ServerSentEventConnection conn, Socket sock)
Flushes I/O in the connection queue.
ServerSentEventHandler::ServerSentEventHandler::SseResponseHeader
const SseResponseHeader
Response header to SSE requests.
Definition ServerSentEventHandler.qc.dox.h:61
ServerSentEventHandler::ServerSentEventHandler::registerNewConnectionIntern
registerNewConnectionIntern(string cid, ServerSentEventConnection conn)
Called while holding the write lock to register the new connection.
ServerSentEventHandler::ServerSentEventHandler::DefaultSocketSendTimeout
const DefaultSocketSendTimeout
default socket send timeout
Definition ServerSentEventHandler.qc.dox.h:48
ServerSentEventHandler::ServerSentEventHandler::DataSerializationSupport
const DataSerializationSupport
Data serialization support.
Definition ServerSentEventHandler.qc.dox.h:65
ServerSentEventHandler::ServerSentEventHandler::DefaultHeartbeatInterval
const DefaultHeartbeatInterval
default heartbeat interval in seconds
Definition ServerSentEventHandler.qc.dox.h:51
ServerSentEventHandler::ServerSentEventHandler::ch
hash< string, ServerSentEventConnection > ch
connection hash
Definition ServerSentEventHandler.qc.dox.h:79
ServerSentEventHandler::ServerSentEventHandler::serializer
code serializer
Value serialization code.
Definition ServerSentEventHandler.qc.dox.h:76
ServerSentEventHandler::ServerSentEventHandler::setHeartbeat
setHeartbeat(softint seconds)
sets the heartbeat interval as a number of seconds
ServerSentEventHandler::ServerSentEventHandler::deregisterConnectionImpl
deregisterConnectionImpl(ServerSentEventConnection conn)
Called when the connection terminates; the default implementation does nothing.
ServerSentEventHandler::ServerSentEventHandler::replayEventsSince
replayEventsSince(ServerSentEventConnection conn, auto id)
Resend all events since the given event.
ServerSentEventHandler::ServerSentEventHandler::encodeMessage
string encodeMessage(hash< SseMessageInfo > msg)
Encode a message for sending.
ServerSentEventHandler::ServerSentEventHandler::logInfo
logInfo(string fmt,...)
Log to the logger.
ServerSentEventHandler::ServerSentEventHandler::getHeartbeat
int getHeartbeat(ServerSentEventConnection conn)
returns the connection heartbeat interval as a number of seconds
ServerSentEventHandler::ServerSentEventHandler::stopOne
stopOne(softstring id)
stop given connection ID
ServerSentEventHandler
the ServerSentEventHandler namespace contains all the objects in the ServerSentEventHandler module
Definition ServerSentEventConnection.qc.dox.h:26