XnStatus XnSensorClient::InitImpl(const XnDeviceConfig* pDeviceConfig) { XnStatus nRetVal = XN_STATUS_OK; XN_MUTEX_HANDLE hServerRunningMutex = NULL; XnOSEvent serverRunningEvent; nRetVal = serverRunningEvent.Open(XN_SENSOR_SERVER_RUNNING_EVENT_NAME); if (nRetVal != XN_STATUS_OK) { nRetVal = serverRunningEvent.Create(XN_SENSOR_SERVER_RUNNING_EVENT_NAME, TRUE); if (nRetVal != XN_STATUS_OK) { xnLogError(XN_MASK_SENSOR_CLIENT, "Failed to create server running event: %s", xnGetStatusString(nRetVal)); return nRetVal; } } nRetVal = xnOSCreateNamedMutex(&hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_NAME); XN_IS_STATUS_OK(nRetVal); nRetVal = xnOSLockMutex(hServerRunningMutex, XN_SENSOR_SERVER_RUNNING_MUTEX_TIMEOUT); if (nRetVal != XN_STATUS_OK) { xnOSCloseMutex(&hServerRunningMutex); return nRetVal; } XnBool bServerRunning = (serverRunningEvent.Wait(1) == XN_STATUS_OK); nRetVal = xnOSUnLockMutex(hServerRunningMutex); xnOSCloseMutex(&hServerRunningMutex); XN_IS_STATUS_OK(nRetVal); if (!bServerRunning) { nRetVal = StartServerProcess(); if (nRetVal != XN_STATUS_OK) { xnLogError(XN_MASK_SENSOR_CLIENT, "Failed to start server process: %s", xnGetStatusString(nRetVal)); return nRetVal; } } nRetVal = serverRunningEvent.Wait(XN_SENSOR_CLIENT_WAIT_FOR_SERVER); if (nRetVal != XN_STATUS_OK) { xnLogError(XN_MASK_SENSOR_CLIENT, "Failed to wait for server to start: %s", xnGetStatusString(nRetVal)); return nRetVal; } // init network nRetVal = xnOSInitNetwork(); XN_IS_STATUS_OK(nRetVal); nRetVal = xnOSCreateCriticalSection(&m_hLock); XN_IS_STATUS_OK(nRetVal); // now init nRetVal = XnStreamReaderDevice::InitImpl(pDeviceConfig); if (nRetVal != XN_STATUS_OK) { xnOSCloseCriticalSection(&m_hLock); return nRetVal; } m_bConnected = TRUE; nRetVal = xnOSCreateEvent(&m_hReplyEvent, FALSE); if (nRetVal != XN_STATUS_OK) { xnOSCloseCriticalSection(&m_hLock); return nRetVal; } nRetVal = xnOSCreateThread(ListenThread, this, &m_hListenThread); if (nRetVal != XN_STATUS_OK) { xnOSCloseEvent(&m_hReplyEvent); xnOSCloseCriticalSection(&m_hLock); return nRetVal; } return (XN_STATUS_OK); }
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); }