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