| Qore ServerSentEventClient Module Reference 1.0

Loading...
Searching...
No Matches
ServerSentEventClient.qc.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* ServerSentEventClient.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 ServerSentEventClient {
28 const SseDefaultPort = 80;
29
31
34 const EVENT_SSE_DATA = "sse-data-event";
35
37
39 const EVENT_SSE_CLOSED = "sse-closed-event";
40
42
44 const EVENT_SSE_CONNECTED = "sse-connected-event";
45
47
61 class ServerSentEventClient : public Logger::LoggerWrapper, public ConnectionProvider::OptionHelper, public DataProvider::DelayedObservable {
62
63 public:
65 const DataSerializationSupport = ...;
66
67
69 const DefaultTimeout = 15s;
70
72 const Version = "1.0";
73
75 const DefaultUserAgent = sprintf("Qore-SSE-Client/%s", ServerSentEventClient::Version);
76
78 const MaxReconnectRetries = -1;
79
81 const ReconnectDelay = 2s;
82
83 protected:
84 Mutex m();
85 Condition sleep_cond();
86 HTTPClient hc;
87 bool stop;
88
90 string last_event_id;
91
93 bool chunked;
94
96 string content_encoding;
97
99 StringOutputStream ostream;
100
102 TransformOutputStream dstream;
103
104 timeout timeout_ms = DefaultTimeout;
105
107 bool reconnect = False;
108
110
112 int tid;
113
115 int tid_waiting;
116
118 Condition tid_cond();
119
121 int cid = -1;
122
124 *code yield;
126 *code start_thread;
127
129 int num_connects;
130
132 string url;
133
135 bool do_reconnect;
136
138 string serialization;
139
141 static Sequence seq();
142
144 const SSEEDC_CONTINUE = 1;
145
147 const SSEEDC_BREAK = 2;
148
149 public:
150
152
177 constructor(hash<auto> opts);
178
179
181 destructor();
182
183
185
187 observersReady();
188
189
191
193 stopEvents();
194
195
197 string getSerialization();
198
199
201 string getUrl();
202
203
205 string getSafeUrl();
206
207
209 int getConnectionId();
210
211
213 hash<auto> getSchemes();
214
215
217 bool getReconnect();
218
219
221
256 hash<auto> connect(*hash<auto> opts, *reference<hash<auto>> info);
257
258
260 bool isOpen();
261
262
264
266 bool reconnect();
267
268
270 disconnect();
271
272
274 bool isReconnecting();
275
276
278
286 *hash<auto> getConnectionInfo();
287
288
290
292 *string getUsername();
293
294
296
298 *string getPassword();
299
300
302 protected:
303 start();
304 public:
305
306
308 protected:
309 hash<auto> connectIntern(*hash<auto> opts, *reference<hash<auto>> info, *bool reconnect);
310 public:
311
312
314 protected:
315 disconnectIntern(*bool destroy);
316 public:
317
318
320
329 clearWarningQueue();
330
331
333
371 setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms = 1s);
372
373
375
396 hash<auto> getUsageInfo();
397
398
400
409 clearStats();
410
411
413
429 setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data);
430
431
433
442 setEventQueue();
443
444
446 protected:
447 constructorInit(hash<auto> opts);
448 public:
449
450
451 protected:
452 disconnectUnlocked();
453 public:
454
455
457 protected:
458 int handleEvent(hash<SseMessageInfo> event);
459 public:
460
461
463 protected:
464 auto deserializeData(string buf);
465 public:
466
467
469 protected:
470 unsupportedSerialization();
471 public:
472
473
475 protected:
476 closingConnection();
477 public:
478
479
480 protected:
481 updateTid(int tid);
482 public:
483
484
486 protected:
487 clearTid();
488 public:
489
490
491 protected:
492 waitTidCleared(int old_tid);
493 public:
494
495
496 protected:
497 setupDecompression();
498 public:
499
500
501 protected:
502 eventLoop();
503 public:
504
505
507 protected:
508 startReconnect();
509 public:
510
511
513 protected:
514 doReconnect();
515 public:
516
517
519 protected:
520 reconnectPreamble();
521 public:
522
523
525 protected:
526 reestablishConnection(reference<hash<auto>> info);
527 public:
528
529
531 protected:
532 reconnectPostamble();
533 public:
534
535
536 protected:
537 logArgs(LoggerLevel level, string message, *softlist<auto> args);
538 public:
539
540
541 protected:
542 logDebug(string fmt, ...);
543 public:
544
545
546 protected:
547 logInfo(string fmt, ...);
548 public:
549
550
551 protected:
552 logWarn(string fmt, ...);
553 public:
554
555
556 protected:
557 logError(string fmt, ...);
558 public:
559
560
562 protected:
563 hash<auto> connectUnlocked(*hash<auto> hdr0, *reference<hash<auto>> info, *bool reconnect);
564 public:
565
566
568 protected:
569 raiseError(string fmt, ...);
570 public:
571
572};
573};
574
575 namespace Priv {
576 class QueuedObserver : public DataProvider::Observer {
577
578 public:
579 protected:
580 Queue q();
581
582 public:
583
584 Queue getQueue();
585
586
588 update(string event_id, hash<auto> data_);
589
590};
591};
Logger::LoggerWrapper
Logger::LoggerWrapper::info
info(string message,...)
Qore::Thread::Queue
ServerSentEventClient::ServerSentEventClient::MaxReconnectRetries
const MaxReconnectRetries
Max reconnect retries; negative numbers = infinite.
Definition ServerSentEventClient.qc.dox.h:78
ServerSentEventClient::ServerSentEventClient::getSafeUrl
string getSafeUrl()
Returns a "safe" URL, without any password info.
ServerSentEventClient::ServerSentEventClient::clearWarningQueue
clearWarningQueue()
Removes any warning Queue object from the Socket.
ServerSentEventClient::ServerSentEventClient::connectIntern
hash< auto > connectIntern(*hash< auto > opts, *reference< hash< auto > > info, *bool reconnect)
Connects to the sse server.
ServerSentEventClient::ServerSentEventClient::last_event_id
string last_event_id
Request Last-Event-Id when connecting.
Definition ServerSentEventClient.qc.dox.h:90
ServerSentEventClient::ServerSentEventClient::tid_waiting
int tid_waiting
Event thread waiting count.
Definition ServerSentEventClient.qc.dox.h:115
ServerSentEventClient::ServerSentEventClient::ReconnectDelay
const ReconnectDelay
Reconnect retry delay.
Definition ServerSentEventClient.qc.dox.h:81
ServerSentEventClient::ServerSentEventClient::connectUnlocked
hash< auto > connectUnlocked(*hash< auto > hdr0, *reference< hash< auto > > info, *bool reconnect)
Performs the HTTP socket connection to the ServerSentEvent server.
ServerSentEventClient::ServerSentEventClient::reconnectPostamble
reconnectPostamble()
Executes after a reconnect.
ServerSentEventClient::ServerSentEventClient::isOpen
bool isOpen()
returns True if the connection is currently open and active, False if not
ServerSentEventClient::ServerSentEventClient::clearStats
clearStats()
Clears performance statistics.
ServerSentEventClient::ServerSentEventClient::tid
int tid
TID of the event thread; only changed in the log.
Definition ServerSentEventClient.qc.dox.h:112
ServerSentEventClient::ServerSentEventClient::closingConnection
closingConnection()
Called when the connection is closed.
ServerSentEventClient::ServerSentEventClient::raiseError
raiseError(string fmt,...)
throsse an exception due to an error
ServerSentEventClient::ServerSentEventClient::ostream
StringOutputStream ostream
The decompression output stream.
Definition ServerSentEventClient.qc.dox.h:99
ServerSentEventClient::ServerSentEventClient::reestablishConnection
reestablishConnection(reference< hash< auto > > info)
Connects to the server when reconnecting.
ServerSentEventClient::ServerSentEventClient::startReconnect
startReconnect()
Starts a reconnect.
ServerSentEventClient::ServerSentEventClient::disconnectIntern
disconnectIntern(*bool destroy)
Disconnect with the given close code.
ServerSentEventClient::ServerSentEventClient::setWarningQueue
setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
ServerSentEventClient::ServerSentEventClient::disconnect
disconnect()
Disconnect with the given close code.
ServerSentEventClient::ServerSentEventClient::deserializeData
auto deserializeData(string buf)
Deserializes data received.
ServerSentEventClient::ServerSentEventClient::DefaultTimeout
const DefaultTimeout
Default socket I/O operation timeout: 15 seconds.
Definition ServerSentEventClient.qc.dox.h:69
ServerSentEventClient::ServerSentEventClient::observersReady
observersReady()
Called when all observers have been added to the object.
ServerSentEventClient::ServerSentEventClient::constructorInit
constructorInit(hash< auto > opts)
Common constructor code.
ServerSentEventClient::ServerSentEventClient::reconnectPreamble
reconnectPreamble()
Executes before a reconnect.
ServerSentEventClient::ServerSentEventClient::cid
int cid
Unique sse connection ID; -1 when not connected.
Definition ServerSentEventClient.qc.dox.h:121
ServerSentEventClient::ServerSentEventClient::do_reconnect
bool do_reconnect
Flag for a user-initiated reconnect.
Definition ServerSentEventClient.qc.dox.h:135
ServerSentEventClient::ServerSentEventClient::seq
static Sequence seq()
Connection sequence.
ServerSentEventClient::ServerSentEventClient::start
start()
Connects and starts receiving messages.
ServerSentEventClient::ServerSentEventClient::setEventQueue
setEventQueue()
Removes any Queue object so that socket events are no longer added to the Queue.
ServerSentEventClient::ServerSentEventClient::url
string url
URL string.
Definition ServerSentEventClient.qc.dox.h:132
ServerSentEventClient::ServerSentEventClient::destructor
destructor()
disconnects the connection and stops the event thread if connected
ServerSentEventClient::ServerSentEventClient::clearTid
clearTid()
Clears the tid and stop variables and wakes up any waiting threads.
ServerSentEventClient::ServerSentEventClient::constructor
constructor(hash< auto > opts)
Creates the ServerSentEvent client object from the arguments.
ServerSentEventClient::ServerSentEventClient::getSchemes
hash< auto > getSchemes()
returns a hash of URL scheme information for URL schemes supported by this object
ServerSentEventClient::ServerSentEventClient::getReconnect
bool getReconnect()
Returns the value of the reconnect flag.
ServerSentEventClient::ServerSentEventClient::start_thread
*code start_thread
callable value to start a new thread; must take a callable object as a parameter and return an intege...
Definition ServerSentEventClient.qc.dox.h:126
ServerSentEventClient::ServerSentEventClient::tid_cond
Condition tid_cond()
Event thread condition variable.
ServerSentEventClient::ServerSentEventClient::getUrl
string getUrl()
returns the URL given in the constructor
ServerSentEventClient::ServerSentEventClient::content_encoding
string content_encoding
The content encoding used by the server.
Definition ServerSentEventClient.qc.dox.h:96
ServerSentEventClient::ServerSentEventClient::doReconnect
doReconnect()
Handles reconnections.
ServerSentEventClient::ServerSentEventClient::num_connects
int num_connects
Total number of connections made.
Definition ServerSentEventClient.qc.dox.h:129
ServerSentEventClient::ServerSentEventClient::chunked
bool chunked
Flag indicating chunked transfer encoding.
Definition ServerSentEventClient.qc.dox.h:93
ServerSentEventClient::ServerSentEventClient::DefaultUserAgent
const DefaultUserAgent
default user agent string for HTTP requests
Definition ServerSentEventClient.qc.dox.h:75
ServerSentEventClient::ServerSentEventClient::reconnect
bool reconnect
Reconnect flag.
Definition ServerSentEventClient.qc.dox.h:107
ServerSentEventClient::ServerSentEventClient::dstream
TransformOutputStream dstream
The decompression transform stream.
Definition ServerSentEventClient.qc.dox.h:102
ServerSentEventClient::ServerSentEventClient::SSEEDC_BREAK
const SSEEDC_BREAK
Event disposition code: break event loop.
Definition ServerSentEventClient.qc.dox.h:147
ServerSentEventClient::ServerSentEventClient::SSEEDC_CONTINUE
const SSEEDC_CONTINUE
Event disposition code: continue processing event loop.
Definition ServerSentEventClient.qc.dox.h:144
ServerSentEventClient::ServerSentEventClient::connect
hash< auto > connect(*hash< auto > opts, *reference< hash< auto > > info)
Connects to the sse server.
ServerSentEventClient::ServerSentEventClient::stopEvents
stopEvents()
Called to stop generating events.
ServerSentEventClient::ServerSentEventClient::isReconnecting
bool isReconnecting()
Returns True if the client is reconnecting.
ServerSentEventClient::ServerSentEventClient::getConnectionId
int getConnectionId()
returns the unique connection ID
ServerSentEventClient::ServerSentEventClient::reconnect
bool reconnect()
Initiate a server reconnect immediately.
ServerSentEventClient::ServerSentEventClient::getSerialization
string getSerialization()
Returns the data serialization value ("none", "json")
ServerSentEventClient::ServerSentEventClient::getPassword
*string getPassword()
Returns any password set for the connection.
ServerSentEventClient::ServerSentEventClient::yield
*code yield
callable object to yield the current thread's execution
Definition ServerSentEventClient.qc.dox.h:124
ServerSentEventClient::ServerSentEventClient::handleEvent
int handleEvent(hash< SseMessageInfo > event)
Handles messages.
ServerSentEventClient::ServerSentEventClient::setEventQueue
setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data)
Sets a Queue object to receive socket events.
ServerSentEventClient::ServerSentEventClient::getConnectionInfo
*hash< auto > getConnectionInfo()
Returns information about the current connection.
ServerSentEventClient::ServerSentEventClient::serialization
string serialization
data serialization option: json or none
Definition ServerSentEventClient.qc.dox.h:138
ServerSentEventClient::ServerSentEventClient::getUsername
*string getUsername()
Returns any username set for the connection.
ServerSentEventClient::ServerSentEventClient::DataSerializationSupport
const DataSerializationSupport
Data serialization support.
Definition ServerSentEventClient.qc.dox.h:65
ServerSentEventClient::ServerSentEventClient::getUsageInfo
hash< auto > getUsageInfo()
Returns performance statistics for the socket.
ServerSentEventClient::ServerSentEventClient::unsupportedSerialization
unsupportedSerialization()
Throws an UNSUPPORTED-SERIALIZATION error.
ServerSentEventClient::ServerSentEventClient::Version
const Version
Client version.
Definition ServerSentEventClient.qc.dox.h:72
Priv
ServerSentEventClient
the ServerSentEventClient namespace contains all the definitions in the ServerSentEventClient module
Definition ServerSentEventClient.qc.dox.h:26
ServerSentEventClient::EVENT_SSE_CLOSED
const EVENT_SSE_CLOSED
ServerSentEvent connection closed event constant.
Definition ServerSentEventClient.qc.dox.h:39
ServerSentEventClient::SseDefaultPort
const SseDefaultPort
default port for connections
Definition ServerSentEventClient.qc.dox.h:28
ServerSentEventClient::EVENT_SSE_CONNECTED
const EVENT_SSE_CONNECTED
ServerSentEvent connected event type.
Definition ServerSentEventClient.qc.dox.h:44
ServerSentEventClient::EVENT_SSE_DATA
const EVENT_SSE_DATA
ServerSentEvent data event constant.
Definition ServerSentEventClient.qc.dox.h:34