Loading...
Searching...
No Matches
ServerSentEventClient.qc.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
/* ServerSentEventClient.qc Copyright 2025 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
namespace ServerSentEventClient {
const SseDefaultPort = 80;
const EVENT_SSE_DATA = "sse-data-event";
const EVENT_SSE_CLOSED = "sse-closed-event";
const EVENT_SSE_CONNECTED = "sse-connected-event";
class ServerSentEventClient : public Logger::LoggerWrapper, public ConnectionProvider::OptionHelper, public DataProvider::DelayedObservable {
public:
const DataSerializationSupport = ...;
const DefaultTimeout = 15s;
const Version = "1.0";
const DefaultUserAgent = sprintf("Qore-SSE-Client/%s", ServerSentEventClient::Version);
const MaxReconnectRetries = -1;
const ReconnectDelay = 2s;
protected:
Mutex m();
Condition sleep_cond();
HTTPClient hc;
bool stop;
string last_event_id;
bool chunked;
string content_encoding;
StringOutputStream ostream;
TransformOutputStream dstream;
timeout timeout_ms = DefaultTimeout;
bool reconnect = False;
int tid;
int tid_waiting;
Condition tid_cond();
int cid = -1;
*code yield;
*code start_thread;
int num_connects;
string url;
bool do_reconnect;
string serialization;
static Sequence seq();
const SSEEDC_CONTINUE = 1;
const SSEEDC_BREAK = 2;
public:
constructor(hash<auto> opts);
destructor();
observersReady();
stopEvents();
string getSerialization();
string getUrl();
string getSafeUrl();
int getConnectionId();
hash<auto> getSchemes();
bool getReconnect();
hash<auto> connect(*hash<auto> opts, *reference<hash<auto>> info);
bool isOpen();
bool reconnect();
disconnect();
bool isReconnecting();
*hash<auto> getConnectionInfo();
*string getUsername();
*string getPassword();
protected:
start();
public:
protected:
hash<auto> connectIntern(*hash<auto> opts, *reference<hash<auto>> info, *bool reconnect);
public:
protected:
disconnectIntern(*bool destroy);
public:
clearWarningQueue();
setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms = 1s);
hash<auto> getUsageInfo();
clearStats();
setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data);
setEventQueue();
protected:
constructorInit(hash<auto> opts);
public:
protected:
disconnectUnlocked();
public:
protected:
int handleEvent(hash<SseMessageInfo> event);
public:
protected:
auto deserializeData(string buf);
public:
protected:
unsupportedSerialization();
public:
protected:
closingConnection();
public:
protected:
updateTid(int tid);
public:
protected:
clearTid();
public:
protected:
waitTidCleared(int old_tid);
public:
protected:
setupDecompression();
public:
protected:
eventLoop();
public:
protected:
startReconnect();
public:
protected:
doReconnect();
public:
protected:
reconnectPreamble();
public:
protected:
reestablishConnection(reference<hash<auto>> info);
public:
protected:
reconnectPostamble();
public:
protected:
logArgs(LoggerLevel level, string message, *softlist<auto> args);
public:
protected:
logDebug(string fmt, ...);
public:
protected:
logInfo(string fmt, ...);
public:
protected:
logWarn(string fmt, ...);
public:
protected:
logError(string fmt, ...);
public:
protected:
hash<auto> connectUnlocked(*hash<auto> hdr0, *reference<hash<auto>> info, *bool reconnect);
public:
protected:
raiseError(string fmt, ...);
public:
};
};
namespace Priv {
class QueuedObserver : public DataProvider::Observer {
public:
protected:
Queue q();
public:
Queue getQueue();
update(string event_id, hash<auto> data_);
};
};
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::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::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::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::dstream
TransformOutputStream dstream
The decompression transform stream.
Definition ServerSentEventClient.qc.dox.h:102
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::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
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