86 std::vector<std::shared_ptr<THttpWSEngine>> clr;
89 std::lock_guard<std::mutex> grd(
fMutex);
93 for (
auto &eng : clr) {
94 eng->fDisabled =
true;
95 if (eng->fHasSendThrd) {
96 eng->fHasSendThrd =
false;
98 eng->fCond.notify_all();
99 eng->fSendThrd.join();
101 eng->ClearHandle(
kTRUE);
108 std::lock_guard<std::mutex> grd(
fMutex);
118 std::lock_guard<std::mutex> grd(
fMutex);
120 return (*iter)->GetId();
132 std::lock_guard<std::mutex> grd(
fMutex);
135 if (eng->GetId() == wsid) {
143 Error(
"FindEngine",
"Try to book next send operation before previous completed");
146 eng->fMTSend =
kTRUE;
162 std::lock_guard<std::mutex> grd(
fMutex);
165 if (*iter == engine) {
167 Error(
"RemoveEngine",
"Trying to remove WS engine during send operation");
169 engine->fDisabled =
true;
175 engine->ClearHandle(terminate);
177 if (engine->fHasSendThrd) {
178 engine->fHasSendThrd =
false;
179 if (engine->fWaiting)
180 engine->fCond.notify_all();
181 engine->fSendThrd.join();
204 if (arg->IsMethod(
"WS_CONNECT"))
209 if (arg->IsMethod(
"WS_READY")) {
212 Error(
"HandleWS",
"WS engine with similar id exists %u", arg->GetWSId());
216 engine = arg->TakeWSEngine();
218 std::lock_guard<std::mutex> grd(
fMutex);
231 if (arg->IsMethod(
"WS_CLOSE")) {
239 if (engine && engine->PreProcess(arg)) {
247 engine->PostProcess(arg);
270 if (
IsSyncMode() || !engine->SupportSendThrd()) {
272 if (engine->CanSendDirectly())
289 if (loopcnt++ > 1000) {
291 std::this_thread::sleep_for(std::chrono::milliseconds(1));
299 std::thread thrd([
this, engine] {
302 if (IsDisabled() || engine->fDisabled) break;
303 std::unique_lock<std::mutex> lk(engine->fMutex);
304 if (engine->fKind == THttpWSEngine::kNone) {
305 engine->fWaiting = true;
306 engine->fCond.wait(lk);
307 engine->fWaiting = false;
312 engine->fSendThrd.swap(thrd);
314 engine->fHasSendThrd =
true;
326 std::lock_guard<std::mutex> grd(engine->fMutex);
332 if (engine->fSending)
334 engine->fSending =
true;
340 switch (engine->fKind) {
342 engine->Send(engine->fData.data(), engine->fData.length());
345 engine->SendHeader(engine->fHdr.c_str(), engine->fData.data(), engine->fData.length());
348 engine->SendCharStar(engine->fData.c_str());
354 engine->fData.clear();
355 engine->fHdr.clear();
358 std::lock_guard<std::mutex> grd(engine->fMutex);
359 engine->fSending =
false;
373 engine->fMTSend =
false;
389 if (!engine)
return -1;
392 engine->Send(buf,
len);
400 std::lock_guard<std::mutex> grd(engine->fMutex);
403 Error(
"SendWS",
"Data kind is not empty - something screwed up");
407 notify = engine->fWaiting;
411 engine->fData.resize(
len);
412 std::copy((
const char *)buf, (
const char *)buf +
len, engine->fData.begin());
415 if (engine->fHasSendThrd) {
416 if (notify) engine->fCond.notify_all();
434 if (!engine)
return -1;
437 engine->SendHeader(hdr, buf,
len);
445 std::lock_guard<std::mutex> grd(engine->fMutex);
448 Error(
"SendWS",
"Data kind is not empty - something screwed up");
452 notify = engine->fWaiting;
457 engine->fData.resize(
len);
458 std::copy((
const char *)buf, (
const char *)buf +
len, engine->fData.begin());
461 if (engine->fHasSendThrd) {
462 if (notify) engine->fCond.notify_all();
479 if (!engine)
return -1;
482 engine->SendCharStar(str);
490 std::lock_guard<std::mutex> grd(engine->fMutex);
493 Error(
"SendWS",
"Data kind is not empty - something screwed up");
497 notify = engine->fWaiting;
503 if (engine->fHasSendThrd) {
504 if (notify) engine->fCond.notify_all();
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
R__EXTERN TSystem * gSystem
enum THttpWSEngine::@155 kNone
! kind of operation
Class for user-side handling of websocket with THttpServer.
Bool_t HandleWS(std::shared_ptr< THttpCallArg > &arg)
Process request to websocket Different kind of requests coded into THttpCallArg::Method:
Int_t SendHeaderWS(UInt_t wsid, const char *hdr, const void *buf, int len)
Send binary data with text header via given websocket id.
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.
THttpWSHandler(const char *name, const char *title, Bool_t syncmode=kTRUE)
normal constructor
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.
Int_t GetNumWS()
Returns current number of websocket connections.
Int_t RunSendingThrd(std::shared_ptr< THttpWSEngine > engine)
Send data stored in the buffer.
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).