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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/****************************************************************************
* *
* PrimeSense Sensor 5.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Sensor. *
* *
* 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),
m_strConfigFile(strConfigFile)
{
}
XnSensorServer::~XnSensorServer()
{
Free();
}
XnStatus XnSensorServer::Run()
{
//Initialize server
XnStatus nRetVal = InitServer();
if (nRetVal == XN_STATUS_OK)
{
//Initialization succeeded - run main loop
nRetVal = ServerMainLoop();
}
Free();
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;
XnBool bEnableMultiUsers = FALSE;
XnUInt32 nValue;
if (XN_STATUS_OK == xnOSReadIntFromINI(m_strConfigFile, XN_SENSOR_SERVER_CONFIG_FILE_SECTION, XN_MODULE_PROPERTY_ENABLE_MULTI_USERS, &nValue))
{
bEnableMultiUsers = (nValue == TRUE);
}
nRetVal = xnOSCreateNamedMutexEx(&m_hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_NAME, bEnableMultiUsers);
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 = xnOSOpenNamedEventEx(&m_hServerRunningEvent, XN_SENSOR_SERVER_RUNNING_EVENT_NAME, bEnableMultiUsers);
if (nRetVal != XN_STATUS_OK)
{
nRetVal = xnOSCreateNamedEventEx(&m_hServerRunningEvent, XN_SENSOR_SERVER_RUNNING_EVENT_NAME, TRUE, bEnableMultiUsers);
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);
xnOSGetTimeStamp(&m_nLastSessionActivity);
return (XN_STATUS_OK);
}
XnStatus XnSensorServer::ServerMainLoop()
{
for (;;)
{
CheckForNewClients(XN_SENSOR_SERVER_ACCEPT_CONNECTION_TIMEOUT);
// do some clean-up
m_sensorsManager.CleanUp();
CleanUpSessions();
// now check if we should shutdown
if (ShutdownIfPossible())
{
break;
}
}
return XN_STATUS_OK;
}
void XnSensorServer::CheckForNewClients(XnUInt32 nTimeout)
{
XnStatus nRetVal = XN_STATUS_OK;
// run in loop until we break due to timeout
XN_SOCKET_HANDLE hClientSocket;
for (;;)
{
nRetVal = xnOSAcceptSocket(m_hListenSocket, &hClientSocket, nTimeout);
if (nRetVal == XN_STATUS_OS_NETWORK_TIMEOUT)
{
return;
}
else if (nRetVal != XN_STATUS_OK)
{
//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));
}
else
{
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
}
}
}
}
void XnSensorServer::CleanUpSessions()
{
XnStatus nRetVal = XN_STATUS_OK;
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));
}
}
}
}
}
XnBool XnSensorServer::ShutdownIfPossible()
{
XnStatus nRetVal = XN_STATUS_OK;
// lock sessions list
XnAutoCSLocker locker(m_hSessionsLock);
// check if no sessions and no sensors
if (CanShutdown())
{
// lock the running lock
XnAutoMutexLocker serverRunningLock(m_hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_TIMEOUT);
nRetVal = serverRunningLock.GetStatus();
if (nRetVal == XN_STATUS_OK)
{
// make sure no client is waiting to connect
CheckForNewClients(0);
// re-check shutdown condition
if (CanShutdown())
{
xnLogInfo(XN_MASK_SENSOR_SERVER, "No sensors are open and no client is connected. Shutting down...");
// reset the event (to notify server is no longer up)
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);
}
// and close the socket (to free the port for another server)
xnOSCloseSocket(m_hListenSocket);
m_hListenSocket = NULL;
return TRUE;
}
}
}
return FALSE;
}
void XnSensorServer::Free()
{
if (m_hServerRunningEvent != NULL)
{
xnOSCloseEvent(&m_hServerRunningEvent);
m_hServerRunningEvent = NULL;
}
if (m_hListenSocket != NULL)
{
xnOSCloseSocket(m_hListenSocket);
m_hListenSocket = NULL;
}
if (m_hSessionsLock != NULL)
{
xnOSCloseCriticalSection(&m_hSessionsLock);
m_hSessionsLock = NULL;
}
}
XnBool XnSensorServer::CanShutdown()
{
XnUInt64 nNow;
xnOSGetTimeStamp(&nNow);
XnAutoCSLocker locker(m_hSessionsLock);
return (!m_sensorsManager.HasOpenSensors() && m_sessions.IsEmpty() &&
(nNow - m_nLastSessionActivity) > m_sensorsManager.GetTimeout());
}
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);
if (m_sessions.IsEmpty())
{
xnOSGetTimeStamp(&m_nLastSessionActivity);
}
}
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();
}
|