cocos2d-x多线程解析域名

void * CNetThread::getIpAddress(void *arg)

{

    //将原先的会引发线程不安全的代码更改为线程安全的代码

    CServerInfo *info = static_cast<CServerInfo *>(arg);

    //    CServerInfo info;

    //    GameInfo::Instance()->getServerInfo(info, index);

    

    //    CCLOG("登入账号");

    struct addrinfo *infop = NULL, hint;

    memset(&hint, 0, sizeof(hint));

    hint.ai_family = AF_INET;

    hint.ai_socktype = SOCK_STREAM;

    char buf[64] = {};

    sprintf(buf, "%d", info->m_PORT);

    CCLOG( "getaddrinfo addr:%s port:%s", info->m_IP.c_str(), buf );

    int ret = getaddrinfo(info->m_IP.c_str(), buf, &hint, &infop);

    //    sprintf(buf, "%d", info.m_PORT);

    //    int ret = getaddrinfo(info.m_IP.c_str(), buf, &hint, &infop);

    if(ret)

    {

        EAI_AGAIN;

        //        char res[256] = {0};

        //        sprintf(res, "getaddrinfo 错误id %d", ret);

        //        CCMessageBox(res, s_LocalError);

        //        GameInfo::Instance()->setAnalyzeIpSuccess(false);

        CCLOG( "getaddrinfo 错误id %d", ret );

    }

    else

    {

        struct sockaddr_in * sa = (struct sockaddr_in *)infop->ai_addr;

        string ipAddress = inet_ntoa(sa->sin_addr);

        int port = ntohs(sa->sin_port);

        //解析成功设置GameInfo中的标志位和ip信息

        GameInfo::Instance()->setIpAddress(ipAddress);

        GameInfo::Instance()->setPort(port);

        //        GameInfo::Instance()->setAnalyzeIpSuccess(true);

    }

    GameInfo::Instance()->setAnalyzeIpSuccess(ret);

    

    GameInfo::Instance()->setThreadFinish(true);

    delete info;

    returnNULL;

}

 

void CNetThread::createThread()

{

    CServerInfo * info = newCServerInfo();

    unsigned int index = GameInfo::Instance()->getServerSelect();

    GameInfo::Instance()->getServerInfo( *info, index );

    pthread_create( & m_tNetThreadLoad, NULL, CNetThread::getIpAddress, info );

}

 

posted on 2014-06-05 23:27  the seal  阅读(347)  评论(0编辑  收藏  举报