HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebTaskHandler.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_WebTaskHandler.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBTASKHANDLER_H__
13 #define __NET_WEBTASKHANDLER_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_HTTPRequest.h"
18 #include "NET_HttpIO.h"
19 #include "NET_Error.h"
20 #include "NET_Time.h"
21 
22 #include <UT/UT_Assert.h>
23 #include <UT/UT_NetSocket.h>
24 #include <UT/UT_SharedPtr.h>
25 #include <UT/UT_StopWatch.h>
26 #include <UT/UT_UniquePtr.h>
27 #include <UT/UT_Functor.h>
28 
29 #include <SYS/SYS_Compiler.h>
30 
31 class NET_WebEndpoint;
32 
33 class NET_API NET_ProfileDebugger
34 {
35 public:
36  NET_ProfileDebugger(const char *name) : myName(name) { myWatch.start(); }
38  {
39  fpreal64 t = myWatch.stop();
40  UTformat("Stopwatch End (\"{}\"): {}\n", myName, t);
41  }
42 
45 };
46 
47 #define __NET_COMBINE(lh, rh) lh##rh
48 
49 #define NET_SCOPED_PROFILER(_name_) \
50  NET_ProfileDebugger __NET_COMBINE(fs_pd, __LINE__)(_name_);
51 
52 class NET_API NET_WebTaskHandler
53 {
54 public:
56 
58  virtual ~NET_WebTaskHandler() = default;
59 
60  const NET_Time &processStartTime() const { return myProcessTime; }
61  virtual int64 calcElapsedTime(time_t now) const
62  {
63  return now - myProcessTime.toTimeT();
64  }
65 
66  virtual const char* className() const = 0;
67 
68  // If we dont have any data left then mark us for death so that the
69  // we can properly clean ourselves up
70  void setHasWork(bool has_work) { myHasMoreWork = has_work; }
71 
72  SYS_FORCE_INLINE bool isFinished() const { return !myHasMoreWork; }
73 
75 
76  // Callback for when the endpoint first opens
78  // Callback for when the endpoint is about to close
80  virtual void onProcess() {}
81 
83  // The time the task started processing
85 };
86 
88 
89 /// This is used to tell the web server what kind of task to create.
90 class NET_API NET_WebTaskFactory
91 {
92 public:
93  using ProtocolId = uint32;
94  static constexpr ProtocolId InvalidProtocolId = 0;
95  static constexpr ProtocolId HttpProtocolId = UT_StringLit("http").hash();
96  static constexpr ProtocolId WSProtocolId = UT_StringLit("websocket").hash();
97 
98  virtual ~NET_WebTaskFactory() = default;
100  {
101  return createByProtocol(ep, determineProtocol(ep), nullptr);
102  }
103 
105  NET_WebEndpoint &ep, ProtocolId Id, const char *url = nullptr) const
106  {
107  UT_ASSERT_P(!"The task factory most have a create function");
108  return nullptr;
109  }
110 
112  {
113  bool is_secure = false;
114  if (NET_HttpIO::isHTTPRequest(ep, is_secure))
115  return HttpProtocolId;
116  return InvalidProtocolId;
117  }
118 
119  virtual bool supportsProtocol(ProtocolId id, const char* path = nullptr) const
120  {
121  return false;
122  }
123 
125  {
126  return supportsProtocol(determineProtocol(ep));
127  }
128 protected:
129  // Enforce the fact that you need to subclass to use this class.
130  NET_WebTaskFactory() = default;
131 };
132 
133 /// This is used as default generic way to generate web tasks. There is no
134 /// packet inspection.
135 template <typename T>
137 {
138 public:
141  const char *url = nullptr) const override
142  {
143  // Check that the protocol we want to create is the protocol type we
144  // actually are.
145  if (T::protocolId() != id)
146  return nullptr;
147  return UTmakeUnique<T>(ep);
148  }
149 
150  bool supportsProtocol(ProtocolId id, const char* path = nullptr) const override
151  {
152  return T::protocolId() == id;
153  }
154 };
155 
156 #endif // __NET_WEBTASKHANDLER_H__
157 
NET_WebTaskHandlerPtr createByProtocol(NET_WebEndpoint &ep, ProtocolId id=InvalidProtocolId, const char *url=nullptr) const override
GLuint id
Definition: glew.h:1679
GLuint const GLchar * name
Definition: glew.h:1814
bool supportsProtocol(NET_WebEndpoint &ep) const
UT_StringHolder myName
SYS_FORCE_INLINE bool isFinished() const
UT_UniquePtr< NET_WebTaskHandler > NET_WebTaskHandlerPtr
NET_ProfileDebugger(const char *name)
SYS_FORCE_INLINE constexpr uint32 hash() const
static constexpr ProtocolId InvalidProtocolId
virtual ProtocolId determineProtocol(NET_WebEndpoint &ep) const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
double fpreal64
Definition: SYS_Types.h:201
virtual int64 calcElapsedTime(time_t now) const
virtual NET_WebTaskHandlerPtr create(NET_WebEndpoint &ep) const
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
virtual bool supportsProtocol(ProtocolId id, const char *path=nullptr) const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
const NET_Time & processStartTime() const
GLsizei const GLchar *const * path
Definition: glew.h:6461
This is used to tell the web server what kind of task to create.
NET_WebEndpoint & myEP
size_t UTformat(FILE *file, const char *format, const Args &...args)
unsigned int uint32
Definition: SYS_Types.h:40
void setHasWork(bool has_work)
virtual void onProcess()
static bool isHTTPRequest(NET_WebEndpoint &ep, bool &is_secure)
bool supportsProtocol(ProtocolId id, const char *path=nullptr) const override
GLdouble GLdouble t
Definition: glew.h:1398
virtual NET_WebTaskHandlerPtr createByProtocol(NET_WebEndpoint &ep, ProtocolId Id, const char *url=nullptr) const