bool CWRequest::StartServer(string sServerPath, int iTimeoutMS, string* pSessionID, string* pErrorMessage) { if(sServerPath == "") { if(pErrorMessage != NULL) *pErrorMessage = "Server application not specified."; return false; } if(!FileExists(sServerPath)) { if(pErrorMessage != NULL) *pErrorMessage = "Server application not found."; return false; } // create session ID string session_id = CreateSessionID(); // start server string args = "-s " + session_id; if(!Execute(sServerPath, args, 0, pErrorMessage)) return false; if(!IsServerRunning(session_id, iTimeoutMS, pErrorMessage)) return false; *pSessionID = session_id; return true; }
void MainWindow::OnNetServer() { if (IsServerRunning()) StopServer(); }
bool CWRequest::SendToServer(string sServerPath, string* pSessionID, string sRequestID, bool bLongPolling, bool bKeepAlive, string* pErrorMessage) { string session_id = *pSessionID; if(session_id == "" || !IsServerRunning(session_id, INSTANCE_CHECK_TIMEOUT, pErrorMessage)) { if(bLongPolling || bKeepAlive) return false; if(!StartServer(sServerPath, INSTANCE_START_TIMEOUT, &session_id, pErrorMessage)) return false; } // --- // write request to file // --- string request_filename = TempDir() + sRequestID + ".request"; int fd = -1; if(!FileCreateNonExistingAndLock(request_filename, REQUEST_WRITE_TIMEOUT, &fd, pErrorMessage)) return false; // query if(!FileWriteStringList(fd, Query, pErrorMessage)) { FileUnlockAndClose(fd, NULL); return false; } // post if(!FileWriteStringList(fd, Post, pErrorMessage)) { FileUnlockAndClose(fd, NULL); return false; } // cookie if(!FileWriteStringList(fd, Cookie, pErrorMessage)) { FileUnlockAndClose(fd, NULL); return false; } // env if(!FileWriteStringList(fd, Env, pErrorMessage)) { FileUnlockAndClose(fd, NULL); return false; } FileUnlockAndClose(fd, NULL); // request type string queue_filename = ""; if(bLongPolling) queue_filename = TempDir() + session_id + ".queue_lp"; else queue_filename = TempDir() + session_id + ".queue"; // --- // register request in queue // --- if(!PushData(queue_filename, (char*)sRequestID.c_str(), sRequestID.size(), REQUEST_SEND_TIMEOUT, pErrorMessage)) return false; *pSessionID = session_id; Clear(); return true; }
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); }