ProtoWorkThread::ProtoWorkThread(const std::shared_ptr<Manager>& manager){ LOG_ENTER_FUNC(""); if(-1 == pipe(this->threadCtrlChan)){ log("failed to create control channel." ); exit(1); } log("this->threadCtrlChan[0] = ", this->threadCtrlChan[0], " this->threadCtrlChan[1] = ", this->threadCtrlChan[1]); if(-1 == fcntl(this->threadCtrlChan[0], F_SETFL, O_NONBLOCK|fcntl(this->threadCtrlChan[0], F_GETFL))){ log("set nonblocking to this->threadCtrlChan[0]", this->threadCtrlChan[0], "failed. errno =", errno); exit(1); } if(-1 == fcntl(this->threadCtrlChan[1], F_SETFL, O_NONBLOCK|fcntl(this->threadCtrlChan[1], F_GETFL))){ log("set nonblocking to this->threadCtrlChan[1]", this->threadCtrlChan[1], "failed. errno =", errno); exit(1); } if(-1 == pipe(this->connCtrlChan)){ log("failed to create connection channel." ); exit(1); } log("this->connCtrlChan[0] = ", this->connCtrlChan[0], " this->connCtrlChan[1] = ", this->connCtrlChan[1]); if(-1 == fcntl(this->connCtrlChan[0], F_SETFL, O_NONBLOCK|fcntl(this->connCtrlChan[0], F_GETFL))){ log("set nonblocking to this->connCtrlChan[0]", this->connCtrlChan[0], "failed. errno =", errno); exit(1); } if(-1 == fcntl(this->connCtrlChan[1], F_SETFL, O_NONBLOCK|fcntl(this->connCtrlChan[1], F_GETFL))){ log("set nonblocking to this->connCtrlChan[1]", this->connCtrlChan[1], "failed. errno =", errno); exit(1); } LOG_LEAVE_FUNC(""); }
ClientSocket* ServerSocket::accept(){ LOG_ENTER_FUNC(""); struct sockaddr addr; struct sockaddr_in inaddr; socklen_t socklen = sizeof(addr); int fd = ::accept(this->fd, &addr, &socklen); ClientSocket* cs = NULL; if (fd>=0){ cs = new ClientSocket(); memcpy(&inaddr, &addr, sizeof(inaddr)); cs->setFd(fd); cs->setPort(inaddr.sin_port); cs->setHost(inet_ntoa(inaddr.sin_addr)); cs->setAddr(addr); cs->setInaddr(inaddr); if(this->reuse){ cs->setReuse(true); } if(this->nodelay){ cs->setNodelay(true); } if(this->nonblocking){ cs->setNonblocking(true); } if(this->keepalive){ cs->setKeepalive(true); } } LOG_LEAVE_FUNC(""); return cs; }
Listener::Listener(const std::weak_ptr<Manager>& manager){ LOG_ENTER_FUNC(""); std::string host; if(manager.expired()){ //todo: notify main thread to eventHandler error. exit(1); } this->manager = manager.lock(); log("3333333333333 this->manager.use_count:",this->manager.use_count()); std::string tmp; int port; if(this->manager->getConfig("port", tmp)){ port = boost::lexical_cast<int>(tmp); } int backlog; if(this->manager->getConfig("backlog", tmp)){ backlog = boost::lexical_cast<int>(tmp); } if(this->manager->getConfig("host", tmp)){ host = tmp; } this->serverSocket = std::make_shared<ServerSocket>(port, host, backlog); LOG_LEAVE_FUNC(""); }
ProtoEngine::ProtoEngine(const std::shared_ptr<Manager>& manager){ LOG_ENTER_FUNC(""); this->manager = manager; log("manager.use_count:",manager.use_count()); log("this->manager.use_count:",this->manager.use_count()); LOG_LEAVE_FUNC(""); }
bool ProtoWorkThread::hasTask(){ LOG_ENTER_FUNC(""); bool rv = (!this->taskQueue.empty()); log("thread ", std::this_thread::get_id()," has Task:", rv?"true":"false"); LOG_LEAVE_FUNC(""); return rv; }
void GLWidget::setupGL() { LOG_ENTER_FUNC(); #if WIN32 if(glewInit() != GLEW_OK) qFatal("Unable to init Glew"); #endif _fbo.reset( new QOpenGLFramebufferObject(size()) ); if( !_debug_desktop_image.empty() ) { // LOG_INFO("Using image instead of screenshot: " << _debug_desktop_image); // if( _slot_desktop->isValid() ) // LOG_WARN("Already initialized!"); // // static QPixmap img( QString::fromStdString(_debug_desktop_image) ); // // _slot_desktop->_data->id = bindTexture(img); // _slot_desktop->_data->width = img.width(); // _slot_desktop->_data->height = img.height(); // // _slot_desktop->setValid(true); } else { if( _fbo_desktop[0] || _fbo_desktop[1] ) qDebug("FBO already initialized!"); _fbo_desktop[0].reset( new QGLFramebufferObject(size()) ); _fbo_desktop[1].reset( new QGLFramebufferObject(size()) ); if( !_fbo_desktop[0]->isValid() || !_fbo_desktop[1]->isValid() ) qFatal("Failed to create framebufferobject!"); _slot_desktop->_data->id = _fbo_desktop[_cur_fbo]->texture(); _slot_desktop->_data->width = size().width(); _slot_desktop->_data->height = size().height(); shader = loadShader("simple.vert", "remove_links.frag"); if( !shader ) qFatal("Failed to load shader."); } shader_blend = loadShader("simple.vert", "blend.frag"); if( !shader_blend ) qFatal("Failed to load blend shader."); glClearColor(0, 0, 0, 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glViewport(0,0, width(), height()); _core.initGL(); }
std::shared_ptr<ITask>& ProtoWorkThread::getTask(){ LOG_ENTER_FUNC(""); std::shared_ptr<ITask>& task = this->taskQueue.front(); this->taskQueue.pop(); log("thread ", std::this_thread::get_id()," get task:", task.get()); LOG_LEAVE_FUNC(""); return task; }
void Dispatcher::serve(){ LOG_ENTER_FUNC(""); while(true){ sleep(1); //log("in loop."); } LOG_LEAVE_FUNC(""); }
Dispatcher::Dispatcher(const std::weak_ptr<Manager>& manager){ LOG_ENTER_FUNC("constructor"); if(manager.expired()){ exit(1); } this->manager = manager.lock(); log("manager.use_count()", this->manager.use_count()); LOG_LEAVE_FUNC("constructor"); }
ServerSocket::ServerSocket(const int& port, const std::string& host, const int& backlog){ LOG_ENTER_FUNC(""); this->fd = -1; this->port = port; this->host = host; this->backlog = backlog; this->nonblocking = true; this->reuse = true; LOG_LEAVE_FUNC(""); }
ServerSocket::ServerSocket(){ LOG_ENTER_FUNC(""); this->fd = -1; this->backlog = 1024; this->port = 9544; this->host = "127.0.0.1"; this->nonblocking = true; this->reuse = true; LOG_LEAVE_FUNC(""); }
void ProtoWorkThread::shutdown(){ LOG_ENTER_FUNC(""); close(this->connCtrlChan[0]);//不再接收新任务 while(this->hasTask());//等待剩余任务处理完成 uint8_t op = 1; if(write(this->threadCtrlChan[1], &op, 1) == -1){//退出线程 log("failed to write this->threadCtrlChan[1]. errno = ", errno); } close(this->threadCtrlChan[0]);//关闭通道 LOG_LEAVE_FUNC(""); }
bool Configure::parseFromConfigFile(){ bool rv = true; LOG_ENTER_FUNC(""); this->set("port", "9544"); this->set("host", "0.0.0.0"); this->set("backlog", "1024"); this->set("protoThreadNumber", "2"); this->set("serviceThreadNumber", "1"); LOG_LEAVE_FUNC(""); return rv; }
void Listener::serve(){ LOG_ENTER_FUNC(""); std::function<void(std::shared_ptr<ListenEventHandler>)> fn = [](std::shared_ptr<ListenEventHandler> sp){return sp->serve();}; try{ this->thread = std::make_shared<std::thread>(fn, this->eventHandler); this->thread->detach(); log("start listen thread:", this->thread->get_id()); log("this->thread.use_count() = ", this->thread.use_count()); }catch(std::exception& e){ log("exception info:", e.what()); } LOG_LEAVE_FUNC(""); }
bool Listener::init(){ LOG_ENTER_FUNC(""); bool rv = true; int result = this->serverSocket->open(); if (result<0){ log("openned server socket failed. errno", errno); return false; } this->eventHandler = std::make_shared<ListenEventHandler>(this->serverSocket->getFd(), this->getSharedPtr(), listenCallback); this->eventHandler->init(); LOG_LEAVE_FUNC(""); return rv; }
bool ProtoWorkThread::init(){ bool rv = true; LOG_ENTER_FUNC(""); this->eventHandler = std::make_shared<ProtoEventHandler>( this->connCtrlChan[0], this->threadCtrlChan[0], this->getSharedPtr(), connReadCb, connCtrlCb, threadCtrlCb ); rv = this->eventHandler->init(); LOG_LEAVE_FUNC(""); return rv; }
void ProtoWorkThread::notify(std::shared_ptr<ITask> task){ LOG_ENTER_FUNC(""); log("task.use_count() = ", task.use_count()); this->taskQueue.push(task); log("task.use_count() = ", task.use_count()); uint8_t op = 1; /* notify eventHandler. */ int result = write(this->connCtrlChan[1], &op, 1); if (result<=0){ log("write this->connCtrlChan[1] failed. errno = ", errno); }else{ log("write this->connCtrlChan[1] successful."); } LOG_LEAVE_FUNC(""); }
bool Configure::get(const std::string& key, std::string& value){ LOG_ENTER_FUNC(""); bool rv = false; std::map<std::string, std::string>::iterator it; it = this->configure.find(key); if (it != this->configure.end()){ value = it->second; rv = true; }else{ value = ""; rv = false; } LOG_LEAVE_FUNC("") return rv; }
void ProtoEngine::serve(){ LOG_ENTER_FUNC(""); int threadNum = 0; std::string threadNumStr; this->manager->getConfig("protoThreadNumber", threadNumStr); if(threadNumStr.size()>0){ threadNum = boost::lexical_cast<int>(threadNumStr); } log("total number of protocol thread to start: ", threadNum); for(int i=0; i<threadNum; i++){ this->workers.push_back(std::make_shared<ProtoWorkThread>(this->manager)); bool result = this->workers.back()->init(); if(!result){log("initialized protocol thread ", i,"failed.");}; this->workers.back()->serve(); } LOG_LEAVE_FUNC(""); }
int ServerSocket::close(){ LOG_ENTER_FUNC(""); int rv = 0; int result = -1; if(this->fd>=0){ result = ::close(this->fd); if (result == -1){ log("there is something wrong when closing socket", this->fd, "errno = ", errno); rv = -1; }else{ this->fd = -1; } }else{ log("we would not close client socket. fd<0."); return -1; } LOG_LEAVE_FUNC(""); return rv; }
void ProtoEngine::notifyThread(const EVENT event, ClientSocket* cs){ LOG_ENTER_FUNC(""); int threadIndex = cs->getFd()%this->workers.size(); log("send client(socket fd) ", cs->getFd()," to thread(index of workers voector)", threadIndex); std::shared_ptr<ITask> task = NULL; log("task.use_count() = ", task.use_count()); switch(event){ case CREATE_CONNECTION: task = std::shared_ptr<ITask>(dynamic_cast<ITask*>(new ConnCreateTask(cs, this->workers[threadIndex]))); break; case CLOSE_CONNECTION: task = std::shared_ptr<ITask>(dynamic_cast<ITask*>(new ConnCloseTask(cs, this->workers[threadIndex]))); break; default: log("error: event", event, "not found!"); break; } log("task.use_count() = ", task.use_count()); this->workers[threadIndex]->notify(task); log("task.use_count() = ", task.use_count()); LOG_LEAVE_FUNC(""); }
void Dispatcher::notifyServiceEngine(const EVENT event, const std::string proto){ LOG_ENTER_FUNC(""); this->manager->serviceEngine->notifyThread(event, proto); LOG_LEAVE_FUNC(""); }
void Configure::set(const std::string& key, const std::string& value){ LOG_ENTER_FUNC(""); this->configure[key] = value; LOG_LEAVE_FUNC(""); }
Configure::Configure(bool flags){ LOG_ENTER_FUNC("bool flags"); LOG_LEAVE_FUNC("bool flags"); }
Configure::~Configure(){ LOG_ENTER_FUNC("default destructor"); LOG_LEAVE_FUNC("default destructor"); }
Listener::~Listener(){ LOG_ENTER_FUNC("default destructor"); LOG_LEAVE_FUNC("default destructor"); }
void Listener::shutdown(){ LOG_ENTER_FUNC(""); int rv = this->serverSocket->close(); this->eventHandler->shutdown(); LOG_LEAVE_FUNC(""); }
Listener::Listener(){ LOG_ENTER_FUNC(""); LOG_LEAVE_FUNC(""); }
Configure::Configure(){ LOG_ENTER_FUNC("default constructor"); LOG_LEAVE_FUNC("default constructor"); }
bool Dispatcher::init(){ bool rv = true; LOG_ENTER_FUNC(""); LOG_LEAVE_FUNC(""); return rv; }