HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_ConnectionHandler.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_ConnectionHandler.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_CONNECTIONHANDLER_H__
13 #define __NET_CONNECTIONHANDLER_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_Error.h"
18 #include "NET_InfoCallback.h"
19 
20 #include <UT/UT_Debug.h>
21 #include <UT/UT_BoostAsio.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_NonCopyable.h>
24 #include <UT/UT_Set.h>
25 #include <UT/UT_Lock.h>
26 #include <UT/UT_SharedPtr.h>
27 
28 #include <SYS/SYS_AtomicInt.h>
29 
31 
33 {
34 public:
37 
39  {
40  myInfoCallback = std::move(info_clb);
41  }
42 protected:
44  myConnectionManager(conn_mgr)
45  {
46  }
47 
48  friend class NET_ConnectionManager;
49 
50  virtual void start() = 0;
51  virtual void stop(bool force) = 0;
52 
54 
56 
57 private:
58  /// Only the connection manager should be touching this information.
61 };
62 
64 
66 {
67 public:
68  void start(const NET_ConnectionHandlerSPtr& handler)
69  {
70  UT_ASSERT(handler != nullptr);
71  {
72  UT_AutoLock lock(myLock);
73  handler->myNext = myConnections;
74  handler->myPrev.reset();
75  if (myConnections)
76  myConnections->myPrev = handler;
77  myConnections = handler;
78  }
79  handler->start();
80  }
81  void stop(const NET_ConnectionHandlerSPtr& handler)
82  {
83  UT_ASSERT(handler != nullptr);
84  {
85  UT_AutoLock lock(myLock);
86  remove(handler);
87  }
88  handler->stop(true /*force*/);
89  }
90  void remove(const NET_ConnectionHandlerSPtr& handler)
91  {
92  UT_ASSERT(handler != nullptr);
93  UT_AutoLock lock(myLock);
94  remove_(handler);
95  }
96  void stopAll(bool force)
97  {
98  // It is possible that the stop calls back into this objects stop which
99  // will invalidate the iterator. Use a temp object to hold the
100  // connections we need to stop to ensure calling stop from within
101  // the users stop doesnt cause undefined behaviour.
102  UT_AutoLock lock(myLock);
103  auto head = myConnections;
104  myConnections = nullptr;
105  while (head != nullptr)
106  {
107  auto next = head->myNext;
108  // Make sure to clear the next and prev so that if stop() calls
109  // remove() it doesnt effect our actual next handler to run after
110  // the stop().
111  head->myNext.reset();
112  head->myPrev.reset();
113  head->stop(force);
114  head = next;
115  }
116  }
117 
118 private:
119  void remove_(const NET_ConnectionHandlerSPtr& handler)
120  {
121  UT_ASSERT(handler != nullptr);
122  if (myConnections == handler)
123  myConnections = handler->myNext;
124  if (auto prev = handler->myPrev.lock(); prev != nullptr)
125  prev->myNext = handler->myNext;
126  if (handler->myNext)
127  handler->myNext->myPrev = handler->myPrev;
128  }
129 
130  UT_Lock myLock;
132 };
133 
134 #endif // __NET_CONNECTIONHANDLER_H__
135 
void stop(const NET_ConnectionHandlerSPtr &handler)
UT_UniquePtr< NET_InfoCallback > myInfoCallback
UT_StringArray JOINTS head
GLuint start
Definition: glcorearb.h:475
NET_ConnectionHandler(NET_ConnectionManager &conn_mgr)
void start(const NET_ConnectionHandlerSPtr &handler)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
#define NET_API
Definition: NET_API.h:9
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
UT_SharedPtr< NET_ConnectionHandler > NET_ConnectionHandlerSPtr
SIM_API const UT_StringHolder force
NET_ConnectionManager & myConnectionManager
void setInfoCallback(UT_UniquePtr< NET_InfoCallback > info_clb)
if(num_boxed_items<=0)
Definition: UT_RTreeImpl.h:697
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
std::weak_ptr< T > UT_WeakPtr
Definition: UT_SharedPtr.h:49