Esempio n. 1
0
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);
    }
}
Esempio n. 2
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);
    }

}
Esempio n. 3
0
int pc_client_connect3(pc_client_t *client, struct sockaddr_in *addr) {
  pc_connect_t *conn_req = pc_connect_req_new(addr);

  if(client->enable_reconnect){
    memcpy(&client->addr, addr, sizeof(struct sockaddr_in));
  }

  if(conn_req == NULL) {
    fprintf(stderr, "Fail to malloc pc_connect_t.\n");
    goto error;
  }

  if(pc_connect(client, conn_req, NULL, pc__client_connect3_cb)) {
    fprintf(stderr, "Fail to connect to server.\n");
    goto error;
  }

  uv_thread_create(&client->worker, pc__worker, client);

  return 0;

error:
  if(conn_req) pc_connect_req_destroy(conn_req);
  return -1;
}
Esempio n. 4
0
int pc_client_connect(pc_client_t *client, struct sockaddr_in *addr) {
  pc_connect_t *conn_req = pc_connect_req_new(addr);

  if(conn_req == NULL) {
    LOGD( "Fail to malloc pc_connect_t.\n");
    goto error;
  }

  if(pc_connect(client, conn_req, NULL, pc__client_connected_cb)) {
    LOGD( "Fail to connect to server.\n");
    goto error;
  }

  // 1. start work thread
  // 2. wait connect result

  uv_thread_create(&client->worker, pc__worker, client);

  // TODO should set a timeout?
  pc__cond_wait(client, 0);

  pc_connect_req_destroy(conn_req);

  if(PC_ST_WORKING != client->state) {
    return -1;
  }

  return 0;
error:
  if(conn_req) pc_connect_req_destroy(conn_req);
  return -1;
}
Esempio n. 5
0
void pc__client_reconnect_timer_cb(uv_timer_t* timer, int status) {
  /* unused */
  (void)status;
  pc_client_t* client = (pc_client_t*)timer->data;
  pc_connect_t* conn_req = pc_connect_req_new(&client->addr);
  if (!conn_req) {
    fprintf(stderr, "out of memory");
    pc_client_stop(client);
  }

  if(pc_connect(client, conn_req, NULL, pc__client_reconnected_cb)) {
    fprintf(stderr, "Fail to connect to server.\n");
    pc_connect_req_destroy(conn_req);
    client->reconnecting = 0;
    pc_client_stop(client);
  }
}
Esempio n. 6
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;
}