unsigned __stdcall serviceDataThread( void* pArguments ){ ListenSocket * clientDataSocket; ClientThreadResource * res = (ClientThreadResource*) pArguments; clientDataSocket = acceptRequest(res->dataSocket); if(res->mode == FTP_DATA_MODE_PASIVE){ if ( res->transferTask == STOR_TRANSFER ){ storeFile( res , clientDataSocket ); } else if ( res->transferTask == RETR_TRANSFER ){ retrieveFile( res , clientDataSocket ); } else if ( res->transferTask == LIST_TRANSFER ){ listFiles( res , clientDataSocket ); } }else if(res->mode == FTP_DATA_MODE_ACTIVE){ while(res->mode == FTP_DATA_MODE_ACTIVE){ blockMutex(res->dataMutex ); executeActiveCommand(res); //unblockMutex(res->controlMutex); } } res->mode = FTP_DATA_MODE_INACTIVE; _endthread(); }
void executeCommandList(ClientThreadResource * res , int * result) { sendMessage(res->controlSocket , concat(res->heapHandler , 2 , "150 Iniciando transferencia de datos " , CHARACTER_CRLF) , &result); unblockMutex(res->dataMutex); blockMutex(res->controlMutex); res->argument = getDataDirectory(); res->transferTask = LIST_TRANSFER; doDataService(res); WaitForSingleObject( res->dataThreadHandler , INFINITE ); debug(res->resultMessage); sendMessage(res->controlSocket , res->resultMessage , &result); }
void ios_proxy::run(size_t threadNum) { assert(threadNum >= 1); boost::lock_guard<boost::mutex> lg(_runMutex); if (!_opend) { _opend = true; _runCount = 0; _runLock = new boost::asio::io_service::work(_ios); _handleList.resize(threadNum); size_t rc = 0; std::shared_ptr<boost::mutex> blockMutex(new boost::mutex); std::shared_ptr<boost::condition_variable> blockConVar(new boost::condition_variable); boost::unique_lock<boost::mutex> ul(*blockMutex); for (size_t i = 0; i < threadNum; i++) { boost::thread* newThread = new boost::thread([&, i] { try { { SetThreadPriority(GetCurrentThread(), _priority); DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &_handleList[i], 0, FALSE, DUPLICATE_SAME_ACCESS); auto lockMutex = blockMutex; auto lockConVar = blockConVar; boost::unique_lock<boost::mutex> ul(*lockMutex); if (threadNum == ++rc) { lockConVar->notify_all(); } else { lockConVar->wait(ul); } } _runCount += _ios.run(); } catch (msg_data::pool_memory_exception&) { MessageBoxA(NULL, "内存不足", NULL, NULL); exit(1); } catch (boost::exception&) { MessageBoxA(NULL, "未处理的BOOST异常", NULL, NULL); exit(2); } catch (std::exception&) { MessageBoxA(NULL, "未处理的STD异常", NULL, NULL); exit(3); } catch (std::shared_ptr<std::string> msg) { MessageBoxA(NULL, msg->c_str(), NULL, NULL); exit(4); } catch (...) { MessageBoxA(NULL, "未知异常", NULL, NULL); exit(-1); } }); _threadsID.insert(newThread->get_id()); _runThreads.add_thread(newThread); } blockConVar->wait(ul); } }