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; }
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 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); }
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 Net::nonSyncConnect() { 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.c_str()); if(pc_client_connect(client, &address)) { pushMsg(PC_EVENT_CONNECTEFAIL, ""); int a = 3; } else { pushMsg(PC_EVENT_CONNECTED, ""); } }
int main() { pc_lib_init(NULL, NULL, NULL, NULL); client = (pc_client_t*)malloc(pc_client_size()); PC_TEST_ASSERT(client); pc_client_config_t config = PC_CLIENT_CONFIG_DEFAULT; config.local_storage_cb = local_storage_cb; pc_client_init(client, (void*)0x11, &config); PC_TEST_ASSERT(pc_client_ex_data(client) == (void*)0x11); PC_TEST_ASSERT(pc_client_state(client) == PC_ST_INITED); pc_client_add_ev_handler(client, PC_EV_USER_DEFINED_PUSH, EV_HANDLER_EX, SERVER_PUSH, event_cb); pc_client_add_ev_handler(client, PC_EV_CONNECTED , EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_CONNECT_ERROR, EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_CONNECT_FAILED , EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_DISCONNECT , EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_KICKED_BY_SERVER, EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_UNEXPECTED_DISCONNECT, EV_HANDLER_EX, NULL, event_cb); pc_client_add_ev_handler(client, PC_EV_PROTO_ERROR , EV_HANDLER_EX, NULL, event_cb); pc_client_connect(client, "127.0.0.1", 3010, NULL); SLEEP(1); pc_request_with_timeout(client, REQ_ROUTE, REQ_MSG, REQ_EX, REQ_TIMEOUT, request_cb); pc_notify_with_timeout(client, NOTI_ROUTE, NOTI_MSG, NOTI_EX, NOTI_TIMEOUT, notify_cb); SLEEP(50); pc_client_disconnect(client); pc_client_cleanup(client); PC_TEST_ASSERT(pc_client_state(client) == PC_ST_NOT_INITED); free(client); pc_lib_cleanup(); return 0; }
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 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 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 test3() { pc_client_config_t config = PC_CLIENT_CONFIG_DEFAULT; int handler_id; pc_lib_init(NULL, NULL, NULL, NULL); client = (pc_client_t*)malloc(pc_client_size()); TEST_ASSERT(client); config.local_storage_cb = local_storage_cb; pc_client_init(client, (void*)0x11, &config); TEST_ASSERT(pc_client_ex_data(client) == (void*)0x11); TEST_ASSERT(pc_client_state(client) == PC_ST_INITED); handler_id = pc_client_add_ev_handler(client, event_cb, EV_HANDLER_EX, NULL); pc_client_connect(client, "127.0.0.1", 3010, NULL); SLEEP(1); if(0) { const char* sRoute = "connector.connectorHandler.entry"; const char* sMsg = "{\"name\": \"test\"}"; pc_request_with_timeout(client, sRoute, sMsg, (void*)101, REQ_TIMEOUT, request_cb); } if(1) { const char* sRoute = "room.roomHandler.test"; const char* sMsg = "{\"name\": \"请求\"}"; char pBuff[1024]; if(1) { int nUseLen = GbToUtf(sMsg, strlen(sMsg), pBuff, 1024); TEST_ASSERT(nUseLen > 0); pBuff[nUseLen] = '\0'; sMsg = pBuff; } pc_request_with_timeout(client, sRoute, sMsg, (void*)102, REQ_TIMEOUT, request_cb); } //pc_notify_with_timeout(client, NOTI_ROUTE, NOTI_MSG, NOTI_EX, NOTI_TIMEOUT, notify_cb); SLEEP(50); pc_client_disconnect(client); pc_client_rm_ev_handler(client, handler_id); pc_client_cleanup(client); TEST_ASSERT(pc_client_state(client) == PC_ST_NOT_INITED); free(client); pc_lib_cleanup(); }