前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cocos2d-x中的curl

cocos2d-x中的curl

作者头像
meteoric
发布2018-11-19 17:04:44
5920
发布2018-11-19 17:04:44
举报
文章被收录于专栏:游戏杂谈游戏杂谈

将请求的接口的动作放在互斥锁中进行

代码语言:javascript
复制
1:  

       2:  

       3: pthread_mutex_t mutex;

       4: pthread_t thread;

       5: sem_t * m_structSem;

       6:  

       7: struct SimpleStructure

       8: {

       9:     int data;

      10:     float otherData;

      11: };

      12:  

      13: void* ThreadFunction(void* arg)

      14: {

      15:     pthread_mutex_lock(&mutex);

      16:     SimpleStructure* args = (SimpleStructure*)arg;

      17:     

      18:     //todo...

      19:  

      20:     delete args;

      21:     pthread_mutex_unlock(&mutex);

      22:  

      23:     pthread_mutex_destroy(&mutex);

      24:     sem_destroy(m_structSem);

      25:  

      26:     return NULL;

      27: }

      28:  

      29: pthread_mutex_init(&mutex, NULL);

      30: m_structSem = sem_open(strThreadName.c_str(), O_CREAT, 0644, 0)

      31:  

      32: //

      33: SimpleStructure* args = new SimpleStructure();

      34: args->data = 1;

      35: args->otherData = 2.0f;

      36: //

      37: pthread_create(&thread, NULL, &ThreadFunction, args);

      38:  

      39:

使用CURL(Cocos2d-x中将curl做为第三方库加入进来,它被放在cocos2dx/platform/third-party/win32的curl目录下)

代码语言:javascript
复制
1: CURL * curl;

       2:  

       3: string strHtml;

       4: string strRetData = "";

       5:  

       6: //第一步:初始化CURL,取得初始化成功后的CURL指针。

       7: curl = curl_easy_init();

       8:  

       9: if (!curl)

      10: {

      11:     return false;

      12: }

      13:  

      14: CCLOG("Http get string, conn: %s, url: %s", strConnName.c_str(), strUrl.c_str());

      15:  

      16: curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpWriteString);

      17: curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strHtml);

      18: curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);

      19: curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

      20:  

      21: //这里定义一个CURL库中API的返回值,用于取得API调用的结果。  

      22: CURLcode res;

      23:  

      24: //第二步,设定我们用此CURL指针来完成的动作。

      25: //参数一为CURL指针,参数二为相应的动作类型枚举,这个枚举值在curl.h中定义,比如本例中的CURLOPT_URL,定义为CINIT(URL,  OBJECTPOINT, 2),即联接一个网站的HTTP服务。

      26: //参数三为动作对应的数据参数,这里是网站的URL地址。  

      27: curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());

      28:  

      29: ///第三步,执行上面设定的动作处理。返回结果放在res中。  

      30: res = curl_easy_perform(curl);  

      31:  

      32: //最后一步,清除CURL指针,结束对CURL库的使用。  

      33: curl_easy_cleanup(curl); 

      34:  

      35: if (res == CURLE_OK)

      36: {

      37:     strRetData = strHtml;

      38:  

      39:     CCLOG("Http get string, conn: %s, ret: %s", strConnName.c_str(), strRetData.c_str());    

      40: }

      41: else 

      42: {

      43:     //request error

      44: }

      45:  

      46: strRetData = "";

      47:  

      48: CC_SAFE_DELETE(curl);

参考:

How to use pthread

cocos2d-x中的CurlTest深入分析

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-05-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档