int main() { // create a client instance. pc_client_t *client = pc_client_new(); 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(ip); // add some event callback. pc_add_listener(client, "onHey", on_hey); pc_add_listener(client, PC_EVENT_DISCONNECT, on_close); // try to connect to server. if(pc_client_connect(client, &address)) { printf("fail to connect server.\n"); pc_client_destroy(client); return 1; } do_notify(client); // main thread has nothing to do and wait until child thread return. pc_client_join(client); // release the client pc_client_destroy(client); return 0; }
void login(const char *username, const char *Channel) { const char *ip = GATE_HOST; int port = GATE_PORT; user = username; channel = Channel; pc_client_t *client = pc_client_new(); // add some event callback. pc_add_listener(client, PC_EVENT_DISCONNECT, on_gate_close); 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(ip); printf("try to connect to gate server %s %d\n", ip, port); // try to connect to server. if (pc_client_connect(client, &address)) { printf("fail to connect gate server.\n"); pc_client_destroy(client); return; } const char *route = "gate.gateHandler.queryEntry"; json_t *msg = json_object(); json_t *str = json_string(username); json_object_set_new(msg, "uid", str); pc_request_t *request = pc_request_new(); pc_request(client, request, route, msg, on_request_gate_cb); }
pc_client_t *pc_client_new_with_reconnect(int delay, int delay_max, int exp_backoff) { pc_client_t* client = pc_client_new(); assert(client); if (delay <= 0 || delay_max <= 0) { fprintf(stderr, "Bad arguments, delay: %d, delay_max: %d", delay, delay_max); pc_client_destroy(client); return NULL; } client->enable_reconnect = 1; client->reconnect_delay = delay; client->reconnect_delay_max = delay_max; client->enable_exp_backoff = exp_backoff ? 1 : 0; srand(time(0)); if(!client->enable_exp_backoff){ client->max_reconnects_incr = client->reconnect_delay_max / client->reconnect_delay + 1; } else { client->max_reconnects_incr = (int)log(1.0 * client->reconnect_delay_max / client->reconnect_delay) / log(2.0) + 1; } /* uv_timer_init never fail */ uv_timer_init(client->uv_loop, &client->reconnect_timer); return client; }
int PomeloSocket::connect(const char* addr, int port) { this->client = pc_client_new(); 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); int ret = pc_client_connect(client, &address); if(ret) { CCLOG("pc_client_connect error:%d",errno); pc_client_destroy(client); return 0; } this->state=CONNECTED_OK; log("socket connected to:ip:%s,port:%d",addr,port); // add pomelo events listener void (*on_disconnect)(pc_client_t *client, const char *event, void *data) = &PomeloSocket::onDisconnectCallback; void (*on_pushData)(pc_client_t *client, const char *event, void *data) = &PomeloSocket::onPushDataCallback; pc_add_listener(client, PC_EVENT_DISCONNECT, on_disconnect); pc_add_listener(client, PC_EVENT_KICK, on_disconnect); pc_add_listener(client, "onTest", on_pushData); //onMsg Director::getInstance()->getScheduler()->scheduleUpdate(this, 0, false); return ret; }
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); } }
Net::~Net() { if(client) { pc_client_destroy(client); } msgQueue.clear(); }
void PomeloSocket::stop() { // main thread has nothing to do and wait until child thread return. //pc_client_join(client); // release the client pc_client_destroy(client); Director::getInstance()->getScheduler()->unscheduleAllForTarget(this); }
void Login::doLogin() { const char *ip = GATE_HOST; int port = GATE_PORT; pc_client_t *client = pc_client_new(); 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(ip); // try to connect to server. if(pc_client_connect(client, &address)) { CCLOG("fail to connect server.\n"); pc_client_destroy(client); return ; } // add some event callback. pc_add_listener(client, PC_EVENT_DISCONNECT, on_close); const char *route = "gate.gateHandler.queryEntry"; json_t *msg = json_object(); json_t *str = json_string(username.c_str()); json_object_set(msg, "uid", str); // decref for json object json_decref(str); pc_request_t *request = pc_request_new(); void (*on_request_gate_cb)(pc_request_t *req, int status, json_t *resp) = &Login::requstGateCallback; pc_request(client, request, route, msg, on_request_gate_cb); // main thread has nothing to do and wait until child thread return. pc_client_join(client); // release the client pc_client_destroy(client); }
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; }
int main() { pc_client_t *client = pc_client_new(); 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(ip); pc_add_listener(client, "onHey", on_hey); pc_add_listener(client, PC_EVENT_DISCONNECT, on_close); if(pc_client_connect(client, &address)) { printf("fail to connect server.\n"); goto error; } int i; for(i=0; i<count; i++) { do_notify(client); } pc_client_join(client); //sleep(3); printf("before main return.\n"); pc_client_destroy(client); return 0; error: pc_client_destroy(client); return 1; }
void on_request_gate_cb(pc_request_t *req, int status, json_t *resp) { if (status == -1) { printf("Fail to send request to server.\n"); } else if (status == 0) { connectorHost = json_string_value(json_object_get(resp, "host")); connectorPort = json_number_value(json_object_get(resp, "port")); pc_client_t *client = pc_client_new(); struct sockaddr_in address; memset(&address, 0, sizeof(struct sockaddr_in)); address.sin_family = AF_INET; address.sin_port = htons(connectorPort); address.sin_addr.s_addr = inet_addr(connectorHost); // add pomelo events listener pc_add_listener(client, "disconnect", on_disconnect); pc_add_listener(client, "onChat", on_chat); pc_add_listener(client, "onAdd", on_add); pc_add_listener(client, "onLeave", on_leave); printf("try to connect to connector server %s %d\n", connectorHost, connectorPort); // try to connect to server. if (pc_client_connect(client, &address)) { printf("fail to connect connector server.\n"); pc_client_destroy(client); return ; } const char *route = "connector.entryHandler.enter"; json_t *msg = json_object(); json_t *str = json_string(user); json_t *channel_str = json_string(channel); json_object_set_new(msg, "username", str); json_object_set_new(msg, "rid", channel_str); pc_request_t *request = pc_request_new(); printf("%s %s\n", user, channel); pc_request(client, request, route, msg, on_request_connector_cb); } // release relative resource with pc_request_t json_t *pc_msg = req->msg; pc_client_t *pc_client = req->client; json_decref(pc_msg); pc_request_destroy(req); pc_client_stop(pc_client); }
int CCPomelo::connect(const char* addr,int port){ 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(); int ret = pc_client_connect(client, &address); if(ret) { CCLOG("pc_client_connect error:%d",errno); pc_client_destroy(client); } return ret; }
bool NetImpl::connect( const char* __host, unsigned short __port ) { 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(__host); // try to connect to server. if(pc_client_connect(client_, &__address)) { printf("fail to connect server.\n"); pc_client_destroy(client_); return false; } return true; }
void CCPomelo::destroy() { if (client) { pc_client_destroy(client); } }
void CCPomelo::stop() { pc_client_destroy(client); }
void Login::requstGateCallback(pc_request_t *req, int status, json_t *resp) { if(status == -1) { CCLOG("Fail to send request to server.\n"); } else if(status == 0) { connectorHost = json_string_value(json_object_get(resp, "host")); connectorPort = json_number_value(json_object_get(resp, "port")); CCLOG("%s %d", connectorHost,connectorPort); pc_client_t *client = pc_client_new(); struct sockaddr_in address; memset(&address, 0, sizeof(struct sockaddr_in)); address.sin_family = AF_INET; address.sin_port = htons(connectorPort); address.sin_addr.s_addr = inet_addr(connectorHost); // add pomelo events listener void (*on_disconnect)(pc_client_t *client, const char *event, void *data) = &Login::onDisconnectCallback; void (*on_chat)(pc_client_t *client, const char *event, void *data) = &Login::onChatCallback; void (*on_add)(pc_client_t *client, const char *event, void *data) = &Login::onAddCallback; void (*on_leave)(pc_client_t *client, const char *event, void *data) = &Login::onLeaveCallback; pc_add_listener(client, "disconnect", on_disconnect); pc_add_listener(client, "onChat", on_chat); pc_add_listener(client, "onAdd", on_add); pc_add_listener(client, "onLeave", on_leave); // try to connect to server. if(pc_client_connect(client, &address)) { CCLOG("fail to connect server.\n"); pc_client_destroy(client); return ; } const char *route = "connector.entryHandler.enter"; json_t *msg = json_object(); json_t *str = json_string(username.c_str()); json_t *channel_str = json_string(channel.c_str()); json_object_set(msg, "username", str); json_object_set(msg, "rid", channel_str); // decref for json object json_decref(str); json_decref(channel_str); pc_request_t *request = pc_request_new(); void (*connect_cb)(pc_request_t *req, int status, json_t *resp )= &Login::requstConnectorCallback; pc_request(client, request, route, msg, connect_cb); /* char *json_str = json_dumps(resp, 0); if(json_str != NULL) { CCLOG("server response: %s %d\n", connectorHost, connectorPort); free(json_str); } */ } // release relative resource with pc_request_t json_t *pc_msg = req->msg; pc_client_t *pc_client = req->client; json_decref(pc_msg); pc_request_destroy(req); pc_client_stop(pc_client); }
void CCPomelo::stop(){ if(client){ // pc_client_stop(client); pc_client_destroy(client); } }