cocos2dx异步网络UI界面更新设计

来源:互联网 发布:mac word2016 取代功能 编辑:程序博客网 时间:2024/05/20 05:30

这里只写了简单的思路和一些实现,其他的没有写
主要有两个部分
a.异步网络请求
b.UI界面更新
主要思路:
创建一个用于UI界面更新的类:
主要代码如下
界面的更新界面任务的更添加
*这里选择用update而不是用director中的scheduler 来更新界面的原因是,有时候在子线程中添加定时任务时会有线程安全问题

void UIUpdateTask::update(float delta) {//    if () {//       //    }    this->m_resLock.lock();    do{        if (this->m_bClearRes) {            std::deque<UpdateInvocation*>::iterator it = this->m_pTaskDeque.begin();            for (; it != this->m_pTaskDeque.end(); ++it) {                //CCObject * t = static_cast<CCObject*>(*it);                (*it)->release();//            }            this->m_pTaskDeque.clear();//            CC_BREAK_IF(true);        }        int count = this->getTaskCount();//获得任务数量        CC_BREAK_IF(count <= 0);        //        UpdateInvocation * task = this->getTaskInvocation();//获得第一个任务        task->invoke(task);//执行任务        CCLog("%d",task->retainCount());        //this->removeTaskFromDeque(task);//删除任务        this->m_pTaskDeque.pop_front();// (action);        task->release();//    }while(0);    this->m_resLock.unlock();}//添加更新任务void UIUpdateTask::addUpdateTask(UpdateInvocation * action,int priority) {    this->m_resLock.lock();    do{        CC_BREAK_IF(action == NULL);        this->addTaskToDeque(action);    }while(0);    this->m_resLock.unlock();}//添加任务到队列void UIUpdateTask::addTaskToDeque(UpdateInvocation * action) {    this->m_pTaskDeque.push_back(action);    action->retain();//}//从队列中删除任务void UIUpdateTask::removeTaskFromDeque(UpdateInvocation * action) {    this->m_pTaskDeque.pop_front();// (action);    action->release();//}UpdateInvocation * UIUpdateTask::getTaskInvocation() {     UpdateInvocation * task = this->m_pTaskDeque.front();    return task;}

创建一个用于异步收发数据的类,发送数据接受数据在子线程中执行
子线程的函数代码如下

//这个子线程的回调函数void ClientNetReq::requestThread(CCObject * pSender) {    ClientNetRespone * request = NULL;    while (true)    {        do{            request = NULL;            this->m_reqTask.lock();            do{                //发送请求获得资源路径                int taskCount = this->m_requestQueue->count();                CC_BREAK_IF(taskCount <= 0);                request = dynamic_cast<ClientNetRespone*>(this->m_requestQueue->objectAtIndex(0));                CC_SAFE_RETAIN(request);//保留                this->m_requestQueue->removeObjectAtIndex(0);            }while (0);            this->m_reqTask.unlock();            CC_BREAK_IF(request == NULL);//为空跳出            //发送和接受前的回调(在这里可以准备发送的数据)            request->runBeforeSend();            //数据的发送和接收            if (request->getSocket() != NULL) {                request->getSocket()->Send((char*)request->getUserData(), kNetBufSize);                request->clearDataBuffer();                int rvcSize = request->getSocket()->Recv((char*)request->getUserData(), kNetBufSize);                request->setRvcSize(rvcSize);//设置接受的大小                request->runRcvDidFinshDataAnalyze();//数据解析                //错误码                //发送后的回调            }else{                printf("没有初始化socket");            }            //更新界面            UIUpdateTask::getInstance()->addUpdateTask(request, 0);            CC_SAFE_RELEASE_NULL(request);//释放        }while (true);        m_bUpdateLock.lock();//        this->m_bUpdate = false;//不更新        m_bUpdateLock.unlock();//        this->m_waitLocker.wait();//等待        m_bUpdateLock.lock();//        this->m_bUpdate = true;//是正在更新        m_bUpdateLock.unlock();//    }    pthread_exit(NULL);}
0 0
原创粉丝点击