Exemple #1
0
void CCPomelo::asyncConnect(const char* addr,int port,CCObject* pTarget, SEL_CallFuncND pSelector){
   
    if (!connect_content) {
        connect_status = 0;
        connect_content = new CCPomeloConnect_;
        connect_content->content = new CCPomeloContent_;
        connect_content->content->pTarget = pTarget;
        connect_content->content->pSelector = pSelector;
    }else{
        CCLOG("can not call again before the first connect callback");
        return ;
    }
    
    struct sockaddr_in address;
    memset(&address, 0, sizeof(struct sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    address.sin_addr.s_addr = inet_addr(addr);
    
    client = pc_client_new();
    pc_connect_t* async = pc_connect_req_new(&address);
    int ret = pc_client_connect2(client,async,cc_pomelo_on_ansync_connect_cb);
    if(ret) {
        CCLOG("pc_client_connect2 error:%d",errno);
        pc_client_destroy(client);
        cc_pomelo_on_ansync_connect_cb(NULL,ret);
    }

}
void CCPomelo::asyncConnect(const char* addr, int port, std::function<void(Node*, void*)> f)
{
    if (!connect_content) {
        connect_status = 0;
        connect_content = new CCPomeloConnect_;
        connect_content->func = f;
    }else{
        CCLOG("can not call again before the first connect callback");
        return ;
    }
    
    struct sockaddr_in address;
    memset(&address, 0, sizeof(struct sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    address.sin_addr.s_addr = inet_addr(addr);
    
    client = pc_client_new();

    pc_connect_t* async = pc_connect_req_new(&address);
    int ret = pc_client_connect2(client,async,cc_pomelo_on_ansync_connect_cb);
    if(ret)
    {
        log("pc_client_connect2 error:%d", errno);
        pc_client_destroy(client);
        cc_pomelo_on_ansync_connect_cb(NULL,ret);
    }
}
Exemple #3
0
int CCPomelo::connect(const char* host,int port, CCObject* pTarget, SEL_CallFuncND pSelector){
    struct sockaddr_in address;
    char* ip = new char[40];
    if (isIPAddr(host)) {
        strcpy(ip, host);
    } else {
        int ret = getIPbyDomain(host, ip);
        if (ret) {
            CCLuaLog("域名解析失败");
            return 0;
        }
    }
    CCLuaLog(ip);
    memset(&address, 0, sizeof(struct sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    address.sin_addr.s_addr = inet_addr(ip);
    if (client) {
        client = pc_client_new();
    }else{
        pc_client_destroy(client);
        client = pc_client_new();
    }
    
    s_CCPomelo->connectContent = new CCPomeloContent_;
    s_CCPomelo->connectContent->pTarget = pTarget;
    s_CCPomelo->connectContent->pSelector = pSelector;
    pc_connect_t *conn_req = pc_connect_req_new(&address);
    s_CCPomelo->connect_stage = KCCPomeloConnectStageDoing;//连接中
    int ret = pc_client_connect2(client, conn_req, cc_pomelo_on_connect_cb);
    if (ret) {
        CCLog("pc_client_connect error:%d",errno);
        pc_client_destroy(client);
        s_CCPomelo->connect_stage = KCCPomeloConnectStageDone;
        s_CCPomelo->connect_status = -1;
    }
    CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(CCPomelo::connectTick),this, 0.5,false);
    return ret;
}