void Loop::frameHandler() { _frameRound++; //处理异步Post auto posts = _asyncPosts.moveAll(); for (auto data : posts) { try { _postHandler ? _postHandler(*data) : onPost(*data); } catch (...) {} delete data; } //处理异步Call请求 auto requests = _asyncRequests.moveAll(); for (auto context : requests) { try { auto asyncHandlerIter = _asyncHandlers.find(context->cmd); (asyncHandlerIter != _asyncHandlers.end()) ? asyncHandlerIter->second(context->cmd, context->request, context->response) : onAsync(context->cmd, context->request, context->response); } catch (...) {}; ec::Loop *fromLoop = ec::Loop::get(context->loopId); if ((NULL == fromLoop) || (!context->handler)) { delete context; continue; } fromLoop->_asyncResponses.push(context); } //处理异步Call返回 auto responses = _asyncResponses.moveAll(); for (auto context : responses) { _asyncPendingCount--; context->handler(context->cmd, context->request, context->response); delete context; } //处理每幀逻辑 try { _frameHandler ? _frameHandler(_frameRound) : onFrame(_frameRound); } catch (...) {} if (_isStopping && (_asyncPendingCount == 0)) { stop(false); } }
size_t Buffer::post(void *buf, timeout_t timeout) { size_t rc; if(!timeout) timeout = INFINITE; if(Thread::waitThread(sem_tail, timeout) != WAIT_OBJECT_0) return Buffer::timeout; enterMutex(); rc = onPost(buf); ++_used; leaveMutex(); ::ReleaseSemaphore(sem_head, 1, (LPLONG)NULL); return rc; }
size_t Buffer::post(void *buf, timeout_t timeout) { size_t rc = 0; enterMutex(); while(_used == _size) { if(!Conditional::wait(timeout, true)) { leaveMutex(); return Buffer::timeout; } } rc = (ssize_t)onPost(buf); ++_used; Conditional::signal(false); leaveMutex(); return rc; }