Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/THttpWSHandler.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // $Id$
0002 // Author: Sergey Linev   20/10/2017
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_THttpWSHandler
0013 #define ROOT_THttpWSHandler
0014 
0015 #include "TNamed.h"
0016 #include "THttpCallArg.h"
0017 
0018 #include <vector>
0019 #include <memory>
0020 #include <mutex>
0021 
0022 class THttpWSEngine;
0023 class THttpServer;
0024 
0025 class THttpWSHandler : public TNamed {
0026 
0027 friend class THttpServer;
0028 
0029 private:
0030    Bool_t fSyncMode{kTRUE};  ///<! is handler runs in synchronous mode (default, no multi-threading)
0031    Bool_t fDisabled{kFALSE}; ///<!  when true, all further operations will be ignored
0032    Int_t fSendCnt{0};        ///<! counter for completed send operations
0033    std::mutex fMutex;        ///<!  protect list of engines
0034    std::vector<std::shared_ptr<THttpWSEngine>> fEngines; ///<!  list of active WS engines (connections)
0035 
0036    std::shared_ptr<THttpWSEngine> FindEngine(UInt_t id, Bool_t book_send = kFALSE);
0037 
0038    Bool_t HandleWS(std::shared_ptr<THttpCallArg> &arg);
0039 
0040    Int_t RunSendingThrd(std::shared_ptr<THttpWSEngine> engine);
0041 
0042    Int_t PerformSend(std::shared_ptr<THttpWSEngine> engine);
0043 
0044    void RemoveEngine(std::shared_ptr<THttpWSEngine> &engine, Bool_t terminate = kFALSE);
0045 
0046    Int_t CompleteSend(std::shared_ptr<THttpWSEngine> &engine);
0047 
0048 protected:
0049 
0050    THttpWSHandler(const char *name, const char *title, Bool_t syncmode = kTRUE);
0051 
0052    /// Method called when multi-threaded send operation is completed
0053    virtual void CompleteWSSend(UInt_t) {}
0054 
0055    /// Method used to accept or reject root_batch_holder.js request
0056    virtual Bool_t ProcessBatchHolder(std::shared_ptr<THttpCallArg> &) { return kFALSE; }
0057 
0058    /// Method called when default page content is prepared for use
0059    /// By default no-cache header is provided
0060    virtual void VerifyDefaultPageContent(std::shared_ptr<THttpCallArg> &arg) { arg->AddNoCacheHeader(); }
0061 
0062    /// Method generate extra suffix for all kinds of loaded code
0063    virtual std::string GetCodeVersion() { return ""; }
0064 
0065 public:
0066    virtual ~THttpWSHandler();
0067 
0068    /// Returns processing mode of WS handler
0069    /// If sync mode is TRUE (default), all event processing and data sending performed in main thread
0070    /// All send functions are blocking and must be performed from main thread
0071    /// If sync mode is false, WS handler can be used from different threads and starts its own sending threads
0072    Bool_t IsSyncMode() const { return fSyncMode; }
0073 
0074    /// Provides content of default web page for registered web-socket handler
0075    /// Can be content of HTML page or file name, where content should be taken
0076    /// For instance, file:/home/user/test.htm or file:$jsrootsys/files/canvas.htm
0077    /// If not specified, default index.htm page will be shown
0078    /// Used by the webcanvas
0079    virtual TString GetDefaultPageContent() { return ""; }
0080 
0081    /// If returns kTRUE, allows to serve files from subdirectories where page content is situated
0082    virtual Bool_t CanServeFiles() const { return kFALSE; }
0083 
0084    /// Allow processing of WS requests in arbitrary thread
0085    virtual Bool_t AllowMTProcess() const { return kFALSE; }
0086 
0087    /// Allow send operations in separate threads (when supported by websocket engine)
0088    virtual Bool_t AllowMTSend() const { return kFALSE; }
0089 
0090    /// Returns true when processing of websockets is disabled, set shortly before handler need to be destroyed
0091    Bool_t IsDisabled() const { return fDisabled; }
0092 
0093    /// Disable all processing of websockets, normally called shortly before destructor
0094    void SetDisabled() { fDisabled = kTRUE; }
0095 
0096    /// Return kTRUE if websocket with given ID exists
0097    Bool_t HasWS(UInt_t wsid) { return !!FindEngine(wsid); }
0098 
0099    /// Returns current number of websocket connections
0100    Int_t GetNumWS();
0101 
0102    UInt_t GetWS(Int_t num = 0);
0103 
0104    void CloseWS(UInt_t wsid);
0105 
0106    Int_t SendWS(UInt_t wsid, const void *buf, int len);
0107 
0108    Int_t SendHeaderWS(UInt_t wsid, const char *hdr, const void *buf, int len);
0109 
0110    Int_t SendCharStarWS(UInt_t wsid, const char *str);
0111 
0112    virtual Bool_t ProcessWS(THttpCallArg *arg) = 0;
0113 
0114    ClassDefOverride(THttpWSHandler, 0) // abstract class for handling websocket requests
0115 };
0116 
0117 #endif