• cocos2dx中的curl


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

       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目录下)

       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深入分析

  • 相关阅读:
    Serialize&Deserialize
    Async&Await
    Generic
    Enum
    Array
    String
    Nullable
    Collection
    Class&Struct
    Interface
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/3071347.html
Copyright © 2020-2023  润新知