forked from avin2/SensorKinect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXnSensorServer.cpp
More file actions
345 lines (289 loc) · 10.5 KB
/
XnSensorServer.cpp
File metadata and controls
345 lines (289 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*****************************************************************************
* *
* PrimeSense Sensor 5.0 Alpha *
* Copyright (C) 2010 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Common. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>. *
* *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnSensorServer.h"
#include "XnSensorClientServer.h"
#include <XnLog.h>
#include <XnIONetworkStream.h>
#include <XnStringsHash.h>
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define XN_SENSOR_SERVER_ACCEPT_CONNECTION_TIMEOUT 100
//---------------------------------------------------------------------------
// XnSensorServer class
//---------------------------------------------------------------------------
XnSensorServer::XnSensorServer(const XnChar* strConfigFile) :
m_hListenSocket(NULL),
m_hServerRunningEvent(NULL),
m_hServerRunningMutex(NULL),
m_hSessionsLock(NULL),
m_nLastClientID(0),
m_nErrorState(XN_STATUS_OK),
m_sensorsManager(strConfigFile)
{
}
XnSensorServer::~XnSensorServer()
{
ShutdownServer();
}
XnStatus XnSensorServer::Run()
{
//Initialize server
XnStatus nRetVal = InitServer();
if (nRetVal == XN_STATUS_OK)
{
//Initialization succeeded - run main loop
nRetVal = ServerMainLoop();
}
//Shutdown the server
ShutdownServer();
return nRetVal;
}
XnBool XnSensorServer::IsServerRunning()
{
if (m_hServerRunningEvent == NULL)
{
return FALSE;
}
//Poll the Server Running event
return xnOSIsEventSet(m_hServerRunningEvent);
}
XnStatus XnSensorServer::InitServer()
{
XnStatus nRetVal = XN_STATUS_OK;
nRetVal = xnOSCreateNamedMutex(&m_hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_NAME);
XN_IS_STATUS_OK(nRetVal);
XnAutoMutexLocker serverRunningLock(m_hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_TIMEOUT);
nRetVal = serverRunningLock.GetStatus();
if (nRetVal != XN_STATUS_OK)
{
//This could mean there's another server/client that's frozen and they're jamming the mutex...
xnLogError(XN_MASK_SENSOR_SERVER, "Failed to lock server mutex: %s - exiting.", xnGetStatusString(nRetVal));
XN_ASSERT(FALSE);
return XN_STATUS_OS_MUTEX_TIMEOUT;
}
//From now on we're protected by m_hServerRunningMutex until we return from this function
/*Create the Server Running event.
This is created as a manual-reset event, because only the server resets it when it's shutting down. */
nRetVal = xnOSOpenNamedEvent(&m_hServerRunningEvent, XN_SENSOR_SERVER_RUNNING_EVENT_NAME);
if (nRetVal != XN_STATUS_OK)
{
nRetVal = xnOSCreateNamedEvent(&m_hServerRunningEvent, XN_SENSOR_SERVER_RUNNING_EVENT_NAME, TRUE);
XN_IS_STATUS_OK(nRetVal);
}
if (IsServerRunning())
{
//Another server is already running.
xnLogInfo(XN_MASK_SENSOR_SERVER, "Detected another server running - exiting.");
xnOSCloseEvent(&m_hServerRunningEvent);
m_hServerRunningEvent = NULL;
return XN_STATUS_DEVICE_SERVER_ALREADY_RUNNING;
}
nRetVal = m_sensorsManager.Init();
XN_IS_STATUS_OK(nRetVal);
// init network
nRetVal = xnOSInitNetwork();
XN_IS_STATUS_OK(nRetVal);
// create lock
nRetVal = xnOSCreateCriticalSection(&m_hSessionsLock);
XN_IS_STATUS_OK(nRetVal);
// create the listen socket
nRetVal = xnOSCreateSocket(XN_OS_TCP_SOCKET, XN_SENSOR_SERVER_IP_ADDRESS, XN_SENSOR_SERVER_PORT, &m_hListenSocket);
XN_IS_STATUS_OK(nRetVal);
// bind it
nRetVal = xnOSBindSocket(m_hListenSocket);
XN_IS_STATUS_OK(nRetVal);
// start listening
nRetVal = xnOSListenSocket(m_hListenSocket);
XN_IS_STATUS_OK(nRetVal);
xnLogVerbose(XN_MASK_SENSOR_SERVER, "Server is now listening");
/*Set the event to signal that the server is ready for requests. We do this AFTER we start listening so
the clients can wait on the event and then connect to the server socket. */
nRetVal = xnOSSetEvent(m_hServerRunningEvent);
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
XnStatus XnSensorServer::ServerMainLoop()
{
XnStatus nRetVal = XN_STATUS_OK;
XnUInt64 nNow = 0;
XnUInt64 nLastClientRemovedTimestamp = 0;
XnBool bQuit = FALSE;
XN_SOCKET_HANDLE hClientSocket = NULL;
XnBool bNoClients = FALSE;
xnOSGetTimeStamp(&nLastClientRemovedTimestamp);
while (!bQuit)
{
nRetVal = xnOSAcceptSocket(m_hListenSocket, &hClientSocket, XN_SENSOR_SERVER_ACCEPT_CONNECTION_TIMEOUT);
if (nRetVal == XN_STATUS_OK)
{
xnLogInfo(XN_MASK_SENSOR_SERVER, "New client trying to connect...");
//TODO: Check if we don't have too many clients
nRetVal = AddSession(hClientSocket);
if (nRetVal != XN_STATUS_OK)
{
xnLogWarning(XN_MASK_SENSOR_SERVER, "Failed to add new client: %s", xnGetStatusString(nRetVal));
xnOSCloseSocket(hClientSocket);
//Still in loop
}
}
else // no client trying to connect, do some clean up work
{
if (nRetVal != XN_STATUS_OS_NETWORK_TIMEOUT)
{
//Any other error beside timeout is not expected, but we treat it the same.
xnLogWarning(XN_MASK_SENSOR_SERVER, "failed to accept connection: %s", xnGetStatusString(nRetVal));
}
// clean sensors
m_sensorsManager.CleanUp();
// remove all non-active sessions
XnAutoCSLocker locker(m_hSessionsLock);
if (!m_sessions.IsEmpty())
{
XnSessionsList::Iterator it = m_sessions.begin();
while (it != m_sessions.end())
{
XnSessionsList::Iterator curr = it;
++it;
XnServerSession* pSession = *curr;
if (pSession->HasEnded())
{
nRetVal = RemoveSession(curr);
if (nRetVal != XN_STATUS_OK)
{
xnLogWarning(XN_MASK_SENSOR_SERVER, "failed to remove session: %s", xnGetStatusString(nRetVal));
}
}
}
if (m_sessions.IsEmpty())
{
xnOSGetHighResTimeStamp(&nLastClientRemovedTimestamp);
}
}
else
{
// no sessions
XnUInt64 nNow;
xnOSGetTimeStamp(&nNow);
if (!m_sensorsManager.HasOpenSensors() && (nNow - nLastClientRemovedTimestamp) > m_sensorsManager.GetTimeout())
{
// shutdown
xnLogInfo(XN_MASK_SENSOR_SERVER, "No sensors are open and no client is connected for %llu ms. Shutting down...", m_sensorsManager.GetTimeout());
bQuit = TRUE;
}
}
} // clean up
} // main loop
return XN_STATUS_OK;
}
void XnSensorServer::ShutdownServer()
{
XnStatus nRetVal = XN_STATUS_OK;
XnAutoMutexLocker serverRunningLock(m_hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_TIMEOUT);
nRetVal = serverRunningLock.GetStatus();
if (nRetVal != XN_STATUS_OK)
{
//This could mean there's another server/client that's frozen and they're jamming the mutex...
xnLogWarning(XN_MASK_SENSOR_SERVER, "Failed to lock server mutex: %s - proceeding with shutdown.", xnGetStatusString(nRetVal));
XN_ASSERT(FALSE);
}
if (m_hServerRunningEvent != NULL)
{
nRetVal = xnOSResetEvent(m_hServerRunningEvent);
if (nRetVal != XN_STATUS_OK)
{
xnLogWarning(XN_MASK_SENSOR_SERVER, "Failed to reset sensor server event: %s - proceeding with shutdown.", xnGetStatusString(nRetVal));
XN_ASSERT(FALSE);
}
xnOSCloseEvent(&m_hServerRunningEvent);
m_hServerRunningEvent = NULL;
}
XN_ASSERT(m_sessions.IsEmpty());
if (m_hListenSocket != NULL)
{
xnOSCloseSocket(m_hListenSocket);
m_hListenSocket = NULL;
}
if (m_hSessionsLock != NULL)
{
xnOSCloseCriticalSection(&m_hSessionsLock);
m_hSessionsLock = NULL;
}
}
XnStatus XnSensorServer::AddSession(XN_SOCKET_HANDLE hClientSocket)
{
XnStatus nRetVal = XN_STATUS_OK;
XnUInt32 nID = 0;
{
XnAutoCSLocker locker(m_hSessionsLock);
++m_nLastClientID;
nID = m_nLastClientID;
}
// create new session
XnServerSession* pSession;
XN_VALIDATE_NEW(pSession, XnServerSession, &m_sensorsManager, nID, hClientSocket, &m_logger);
nRetVal = pSession->Init();
if (nRetVal != XN_STATUS_OK)
{
XN_DELETE(pSession);
return (nRetVal);
}
// add it to list in a lock
{
XnAutoCSLocker locker(m_hSessionsLock);
nRetVal = m_sessions.AddLast(pSession);
}
if (nRetVal != XN_STATUS_OK)
{
XN_DELETE(pSession);
return (nRetVal);
}
return (XN_STATUS_OK);
}
XnStatus XnSensorServer::RemoveSession(XnSessionsList::ConstIterator it)
{
XnStatus nRetVal = XN_STATUS_OK;
XnServerSession* pSession = *it;
XnUInt32 nID = pSession->ID();
xnLogVerbose(XN_MASK_SENSOR_SERVER, "Removing client %u...", nID);
{
XnAutoCSLocker locker(m_hSessionsLock);
nRetVal = m_sessions.Remove(it);
XN_IS_STATUS_OK(nRetVal);
}
pSession->Free();
XN_DELETE(pSession);
xnLogVerbose(XN_MASK_SENSOR_SERVER, "Client %u removed", nID);
return (XN_STATUS_OK);
}
XN_DEVICE_API XnStatus XnSensorServerGetGlobalConfigFile(const XnChar* strConfigDir, XnChar* strConfigFile, XnUInt32 nBufSize)
{
return XnSensor::ResolveGlobalConfigFileName(strConfigFile, nBufSize, strConfigDir);
}
XN_DEVICE_API XnStatus XnSensorServerRun(const XnChar* strConfigFile)
{
XnSensorServer server(strConfigFile);
return server.Run();
}