83 std::vector<std::shared_ptr<THttpWSEngine>> clr;
86 std::lock_guard<std::mutex> grd(
fMutex);
90 for (
auto &eng : clr) {
91 eng->fDisabled =
true;
92 if (eng->fHasSendThrd) {
93 eng->fHasSendThrd =
false;
94 eng->fCond.notify_all();
95 eng->fSendThrd.join();
97 eng->ClearHandle(
kTRUE);
104 std::lock_guard<std::mutex> grd(
fMutex);
114 std::lock_guard<std::mutex> grd(
fMutex);
116 return (*iter)->GetId();
128 std::lock_guard<std::mutex> grd(
fMutex);
131 if (eng->GetId() == wsid) {
139 Error(
"FindEngine",
"Try to book next send operation before previous completed");
142 eng->fMTSend =
kTRUE;
158 std::lock_guard<std::mutex> grd(
fMutex);
161 if (*iter == engine) {
163 Error(
"RemoveEngine",
"Trying to remove WS engine during send operation");
165 engine->fDisabled =
true;
171 engine->ClearHandle(terminate);
173 if (engine->fHasSendThrd) {
174 engine->fHasSendThrd =
false;
175 engine->fCond.notify_all();
176 engine->fSendThrd.join();
197 if (arg->IsMethod(
"WS_CONNECT"))
202 if (arg->IsMethod(
"WS_READY")) {
205 Error(
"HandleWS",
"WS engine with similar id exists %u", arg->GetWSId());
209 engine = arg->TakeWSEngine();
211 std::lock_guard<std::mutex> grd(
fMutex);
224 if (arg->IsMethod(
"WS_CLOSE")) {
232 if (engine && engine->PreProcess(arg)) {
240 engine->PostProcess(arg);
262 if (engine->fHasSendThrd) {
264 engine->fCond.notify_all();
268 if (
IsSyncMode() || !engine->SupportSendThrd()) {
270 if (engine->CanSendDirectly())
287 if (loopcnt++ > 1000) {
289 std::this_thread::sleep_for(std::chrono::milliseconds(1));
297 std::thread thrd([
this, engine] {
300 if (IsDisabled() || engine->fDisabled) break;
301 std::unique_lock<std::mutex> lk(engine->fDataMutex);
302 if (engine->fKind == THttpWSEngine::kNone)
303 engine->fCond.wait(lk);
307 engine->fSendThrd.swap(thrd);
309 engine->fHasSendThrd =
true;
321 std::lock_guard<std::mutex> grd(engine->fDataMutex);
327 if (engine->fDoingSend)
329 engine->fDoingSend =
true;
335 switch (engine->fKind) {
337 engine->Send(engine->fData.data(), engine->fData.length());
340 engine->SendHeader(engine->fHdr.c_str(), engine->fData.data(), engine->fData.length());
343 engine->SendCharStar(engine->fData.c_str());
349 engine->fData.clear();
350 engine->fHdr.clear();
353 std::lock_guard<std::mutex> grd(engine->fDataMutex);
354 engine->fDoingSend =
false;
368 engine->fMTSend =
false;
383 if (!engine)
return -1;
386 engine->Send(buf, len);
392 std::lock_guard<std::mutex> grd(engine->fDataMutex);
395 Error(
"SendWS",
"Data kind is not empty - something screwed up");
399 engine->fData.resize(len);
400 std::copy((
const char *)buf, (
const char *)buf + len, engine->fData.begin());
402 engine->fDoingSend =
false;
419 if (!engine)
return -1;
422 engine->SendHeader(hdr, buf, len);
428 std::lock_guard<std::mutex> grd(engine->fDataMutex);
431 Error(
"SendWS",
"Data kind is not empty - something screwed up");
436 engine->fData.resize(len);
437 std::copy((
const char *)buf, (
const char *)buf + len, engine->fData.begin());
439 engine->fDoingSend =
false;
455 if (!engine)
return -1;
458 engine->SendCharStar(str);
464 std::lock_guard<std::mutex> grd(engine->fDataMutex);
467 Error(
"SendWS",
"Data kind is not empty - something screwed up");
473 engine->fDoingSend =
false;
R__EXTERN TSystem * gSystem
enum THttpWSEngine::@151 kNone
! kind of operation
Bool_t HandleWS(std::shared_ptr< THttpCallArg > &arg)
Process request to websocket Different kind of requests coded into THttpCallArg::Method "WS_CONNECT" ...
Int_t SendHeaderWS(UInt_t wsid, const char *hdr, const void *buf, int len)
Send binary data with text header via given websocket id Returns -1 - in case of error,...
Bool_t IsSyncMode() const
Returns processing mode of WS handler If sync mode is TRUE (default), all event processing and data s...
Bool_t IsDisabled() const
Returns true when processing of websockets is disabled, set shortly before handler need to be destroy...
std::vector< std::shared_ptr< THttpWSEngine > > fEngines
! list of active WS engines (connections)
void SetDisabled()
Disable all processing of websockets, normally called shortly before destructor.
void CloseWS(UInt_t wsid)
Close connection with given websocket id.
UInt_t GetWS(Int_t num=0)
Return websocket id with given sequential number Number of websockets returned with GetNumWS() method...
virtual ~THttpWSHandler()
destructor Make sure that all sending threads are stopped correctly
virtual Bool_t ProcessWS(THttpCallArg *arg)=0
Int_t SendCharStarWS(UInt_t wsid, const char *str)
Send string via given websocket id Returns -1 - in case of error, 0 - when operation was executed imm...
THttpWSHandler(const char *name, const char *title, Bool_t syncmode=kTRUE)
THttpWSHandler.
Int_t PerformSend(std::shared_ptr< THttpWSEngine > engine)
Perform send operation, stored in buffer.
Int_t CompleteSend(std::shared_ptr< THttpWSEngine > &engine)
Complete current send operation.
Int_t SendWS(UInt_t wsid, const void *buf, int len)
Send binary data via given websocket id Returns -1 - in case of error 0 - when operation was executed...
Int_t GetNumWS()
Returns current number of websocket connections.
Int_t RunSendingThrd(std::shared_ptr< THttpWSEngine > engine)
Send data stored in the buffer Returns 0 - when operation was executed immediately 1 - when send oper...
virtual Bool_t AllowMTSend() const
Allow send operations in separate threads (when supported by websocket engine)
Int_t fSendCnt
! counter for completed send operations
virtual void CompleteWSSend(UInt_t)
Method called when multi-threaded send operation is completed.
void RemoveEngine(std::shared_ptr< THttpWSEngine > &engine, Bool_t terminate=kFALSE)
Remove and destroy WS connection.
std::mutex fMutex
! protect list of engines
std::shared_ptr< THttpWSEngine > FindEngine(UInt_t id, Bool_t book_send=kFALSE)
Find websocket connection handle with given id If book_send parameter specified, have to book send op...
The TNamed class is the base class for all named ROOT classes.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept