TDME2  1.9.200
ServerWorkerThread.cpp
Go to the documentation of this file.
1 #include <exception>
2 #include <memory>
3 #include <string>
4 #include <typeinfo>
5 
6 #include <tdme/tdme.h>
13 #include <tdme/utilities/Console.h>
15 #include <tdme/utilities/RTTI.h>
16 
17 using std::string;
18 using std::to_string;
19 using std::unique_ptr;
20 
30 
31 ServerWorkerThread::ServerWorkerThread(const unsigned int id, ServerWorkerThreadPool* threadPool, Barrier* startUpBarrier) :
32  Thread("nioworkerthread"),
33  id(id),
34  threadPool(threadPool),
35  startUpBarrier(startUpBarrier) {
36  //
37 }
38 
40 }
41 
43  Console::println("ServerWorkerThread[" + to_string(id) + "]::run(): start");
44 
45  // wait on startup barrier
47 
48  //
49  while (true) {
50  // get request
51  auto request = unique_ptr<ServerRequest>(threadPool->getElement());
52  if (request == nullptr) break;
53  // get request parameter
54  auto requestType = request->getRequestType();
55  ServerClient* client = nullptr;
56  ServerGroupBase* group = nullptr;
57 
58  // handle request types
59  switch(requestType) {
61  client = static_cast<ServerClient*>(request->getObject());
62  auto packet = unique_ptr<const UDPPacket>(request->getMessagePacket());
63  auto messageId = request->getMessageId();
64  auto retries = request->getMessageRetries();
65 
66  // handle request
67  try {
68  client->onRequest(packet.get(), messageId, retries);
69  } catch(Exception& exception) {
70  Console::println(
71  "ServerWorkerThread[" +
72  to_string(id) +
73  "]::run(): client: request: " +
74  (RTTI::demangle(typeid(exception).name())) +
75  ": " +
76  (exception.what())
77  );
78 
79  // unhandled exception, so shutdown the client
80  client->shutdown();
81  }
82 
83  //
84  break;
85  }
87  client = static_cast<ServerClient*>(request->getObject());
88  // handle close
89  try {
90  client->onInit();
91  } catch(Exception& exception) {
92  Console::println(
93  "ServerWorkerThread[" +
94  to_string(id) +
95  "]::run(): client: init: " +
96  (RTTI::demangle(typeid(exception).name())) +
97  ": " +
98  (exception.what())
99  );
100  }
101  break;
102  }
104  client = static_cast<ServerClient*>(request->getObject());
105  // handle close
106  try {
107  client->onClose();
108  } catch(Exception& exception) {
109  Console::println(
110  "ServerWorkerThread[" +
111  to_string(id) +
112  "]::run(): client: close: " +
113  (RTTI::demangle(typeid(exception).name())) +
114  ": " +
115  (exception.what())
116  );
117  }
118  break;
119  }
121  client = static_cast<ServerClient*>(request->getObject());
122  // handle close
123  try {
124  client->onCustom(request->getCustomEvent());
125  } catch(Exception& exception) {
126  Console::println(
127  "ServerWorkerThread[" +
128  to_string(id) +
129  "]::run(): client: custom: " +
130  (RTTI::demangle(typeid(exception).name())) +
131  ": " +
132  (exception.what())
133  );
134  }
135  break;
136  }
138  group = static_cast<ServerGroupBase*>(request->getObject());
139  // handle close
140  try {
141  group->onInit();
142  } catch(Exception& exception) {
143  Console::println(
144  "ServerWorkerThread[" +
145  to_string(id) +
146  "]::run(): group: init: " +
147  (RTTI::demangle(typeid(exception).name())) +
148  ": " +
149  (exception.what())
150  );
151  }
152  break;
153  }
155  group = static_cast<ServerGroupBase*>(request->getObject());
156  // handle close
157  try {
158  group->onClose();
159  } catch(Exception& exception) {
160  Console::println(
161  "ServerWorkerThread[" +
162  to_string(id) +
163  "]::run(): group: close: " +
164  (RTTI::demangle(typeid(exception).name())) +
165  ": " +
166  (exception.what())
167  );
168  }
169  break;
170  }
172  group = static_cast<ServerGroupBase*>(request->getObject());
173  // handle close
174  try {
175  group->onCustomEvent(request->getCustomEvent());
176  } catch(Exception& exception) {
177  Console::println(
178  "ServerWorkerThread[" +
179  to_string(id) +
180  "]::run(): group: custom: " +
181  (RTTI::demangle(typeid(exception).name())) +
182  ": " +
183  (exception.what())
184  );
185  }
186  break;
187  }
188  }
189 
190  // release reference
191  if (client != nullptr) client->releaseReference();
192  if (group != nullptr) group->releaseReference();
193  }
194 
195  //
196  Console::println("ServerWorkerThread[" + to_string(id) + "]::run(): done");
197 }
Base class for network server clients.
Definition: ServerClient.h:29
virtual void shutdown()=0
Shuts down this network client.
virtual void onRequest(const UDPPacket *packet, const uint32_t messageId, const uint8_t retries)=0
To be overwritten with a request handler, will be called from worker.
virtual void onCustom(const string &type)=0
Base class for network server group.
virtual void onCustomEvent(const string &type)=0
Base class for network server groups.
Definition: ServerGroup.h:34
Simple server worker thread pool class.
Barrier implementation.
Definition: Barrier.h:21
bool wait()
Waits on barrier.
Definition: Barrier.cpp:28
T * getElement()
Gets an element from this queue, if no element exists yet the calling thread will be blocked until an...
Definition: Queue.h:65
Base class for threads.
Definition: Thread.h:20
Console class.
Definition: Console.h:29
Run time type information utility class.
Definition: RTTI.h:14
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38
std::exception Exception
Exception base class.
Definition: Exception.h:18