////////////////////////////////////////////////////////////////// // 设置输出格式 ////////////////////////////////////////////////////////////////// void CTreeOutCtrl::SetCtrlFormat(CString strSet) { CStringArray aLines; // 拆分成若干行 ParseLines(strSet, aLines); for(int i=0; i<aLines.GetSize(); i++) { CString strLine = aLines[i]; ParseALine(strLine); // 分析行 } }
void Client::run() { static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; while (running) { pthread_mutex_lock(&lock); if (running) // got access but am i still running? pthread_cond_wait(&g_wait, &lock); else { pthread_mutex_unlock(&lock); break; } DBG(logWrite(cout << "Thread " << threadID << " was signaled\n");) // Lock on the linked list of incoming connections and retrieve one of them pthread_mutex_lock(&g_locklist); if (incomingConn->size() == 0) { pthread_mutex_unlock(&g_locklist); pthread_mutex_unlock(&lock); continue; } IncomingConnection *clientConnection = incomingConn->front(); incomingConn->pop_front(); clientSocketHandler = new SocketHandler(clientConnection->sockfd()); pthread_mutex_unlock(&g_locklist); pthread_mutex_unlock(&lock); if (!running) break; logWrite(cout << "Thread " << threadID << " serves host " << clientConnection->ip() << endl); // Get the HTTP Request recvData.clear(); keepAlive = true; timeout.tv_sec = 15; timeout.tv_usec = 0; do { FD_ZERO(&readfds); FD_SET(clientConnection->sockfd(), &readfds); timeout.tv_sec = 15; timeout.tv_usec = 0; DBG(cout << timeout.tv_sec << ", " << timeout.tv_usec << ".\n";) switch (select(clientConnection->sockfd()+1, &readfds, 0, 0, &timeout)){ case -1: running = false; break; case 0: keepAlive = false; break; // Timeout default: recvData += clientSocketHandler->recieve(); // If CRLF is in the string, we are ready to process it if (recvData.find("\r\n\r\n") < string::npos) { // Break the HTTP request into multiple lines vector<string> lines; lines.reserve(8); ParseLines(lines, recvData); // In the case that the only input was \r\n\r\n then close connection if (lines.size() == 0) { keepAlive = false; break; } // Log the request in the log file char timebuf[20] = {0}; time_t t = time(0); struct tm * timeinfo = localtime ( &t ); strftime(timebuf, 20, "%Y-%b-%d %H:%M", timeinfo); logWrite(cout << clientConnection->ip() << " - [" << timebuf << "] - \"" << lines[0] << "\"\n";) if (ProcessRequest(recvData, lines) == true) { // If we finished processing the request recvData.clear(); // Clear the failbits recvData = ""; // Reset the string timeout.tv_sec = 15; // Also refresh the timeout for the next request timeout.tv_usec = 0; requestedMethod.clear(); requestedFile.clear(); httpVersion.clear(); requestedMethod = requestedFile = httpVersion = ""; } }