HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebEndpoint.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_WebEndpoint.h
7  *
8  * COMMENTS:
9  * Holds information about an endpoint. This is typically used as the
10  * information for responding to a request.
11  */
12 
13 #ifndef __NET_WEBENDPOINT_H__
14 #define __NET_WEBENDPOINT_H__
15 
16 #include "NET_API.h"
17 
18 #include <NET/NET_WebStream.h>
19 
20 #include <UT/UT_Assert.h>
21 #include <UT/UT_StopWatch.h>
22 #include <UT/UT_StringHolder.h>
23 #include <UT/UT_UniquePtr.h>
24 #include <UT/UT_SharedPtr.h>
25 
26 #include <SYS/SYS_AtomicInt.h>
27 
28 #include <atomic>
29 
30 class NET_WebTaskHandler;
31 
32 class NET_API NET_WebEndpoint
33 {
34 public:
35  enum State
36  {
37  kNORMAL, // This is the first time runnin
38  kDEATH
39  };
40  using Id = int32;
41 
44  bool is_secure,
46  bool has_read_access,
47  bool has_write_access,
48  unsigned short client_ip[4]);
49  ~NET_WebEndpoint();
50 
51  NET_WebEndpoint(const NET_WebEndpoint &) = delete;
52  NET_WebEndpoint &operator=(const NET_WebEndpoint &) = delete;
53  NET_WebEndpoint(NET_WebEndpoint &&) = delete;
54  NET_WebEndpoint &operator=(NET_WebEndpoint &&) = delete;
55 
56  bool operator==(const NET_WebEndpoint &other) const
57  {
58  return myId == other.myId;
59  }
60  bool operator!=(const NET_WebEndpoint &other) const
61  {
62  return !(*this == other);
63  }
64 
65  bool initialize(UT_UniquePtr<UT_NetSocket> socket, int port);
66 
67  void getClientIp(unsigned short ip[4]) const;
68 
69  UT_StringHolder clientIpStr() const;
70 
71  int port() const
72  {
73  // Dont use the port from the stream. The port is the way to identify
74  // who this endpoint belongs to so we need to make sure we keeps
75  // this value as a constant.
76  return myPort;
77  }
78 
79  bool hasValidStream() const { return myStream && myStream->isValid(); }
80 
81  void debugPrint() const;
82 
83  int getFD() const
84  {
85  if (!hasValidStream())
86  return -1;
87  return myStream->getFD();
88  }
89 
90  Id getId() const { return myId; }
91 
93  {
94  return myIsAlive.load(std::memory_order_seq_cst);
95  }
96  SYS_FORCE_INLINE void setIsAlive(bool is_alive)
97  {
98  myIsAlive.store(is_alive, std::memory_order_seq_cst);
99  }
101  {
102  return myProcessCount.load() > 0;
103  }
105  {
106  UT_VERIFY(myProcessCount.exchangeAdd(1) >= 0);
107  }
109  {
110  UT_VERIFY(myProcessCount.exchangeAdd(-1) >= 0);
111  }
112 
113  /// We are about to placed on the queue for processing
114  void startProcessQueue() { myWatch.start(); }
115 
120 
121  unsigned short myClientIp[4];
123 
124  void setNewHandler(UT_UniquePtr<NET_WebTaskHandler> handler);
125  void process();
126 
127  unsigned taskCount() const { return myTaskCount; }
128 
129  int64 calcElapsedTime(time_t now) const;
130 
131  // This is really only used for debugging
132  const char* handlerClassName();
133 private:
134  unsigned myTaskCount;
135  int myPort;
136 
137  std::atomic_bool myIsAlive;
138  // This indicates if this endpoint is already being processed
139  SYS_AtomicInt32 myProcessCount;
140  Id myId;
141  static SYS_AtomicCounter theIdCounter;
143  UT_StopWatch myWatch;
144 };
145 
147 
148 #endif // __NET_WEBENDPOINT_H__
149 
GLuint GLuint stream
Definition: glew.h:7265
int int32
Definition: SYS_Types.h:39
UT_SharedPtr< NET_WebEndpoint > NET_WebEndpointPtr
int port() const
int getFD() const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
bool operator==(const NET_WebEndpoint &other) const
Id getId() const
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
NET_WebStreamPtr myStream
bool operator!=(const NET_WebEndpoint &other) const
long long int64
Definition: SYS_Types.h:116
UT_UniquePtr< NET_WebStream > NET_WebStreamPtr
SYS_FORCE_INLINE bool isAlive() const
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:291
void startProcessQueue()
We are about to placed on the queue for processing.
SYS_FORCE_INLINE bool isProcessing() const
SYS_FORCE_INLINE void setIsAlive(bool is_alive)
#define UT_VERIFY(expr)
Definition: UT_Assert.h:181
SYS_FORCE_INLINE void markProcessing()
SYS_FORCE_INLINE void markFinishedProcessing()
unsigned taskCount() const
bool hasValidStream() const