CS_POMELO_EXPORT int request(pc_client_t* client, const char* route, const char* msg, unsigned int cbid, int timeout, request_callback cb) { request_cb_t* rp = (request_cb_t*)malloc(sizeof(request_cb_t)); if (!rp) { return PC_RC_ERROR; } rp->cb = cb; rp->cbid= cbid; return pc_request_with_timeout(client, route, msg, rp, timeout, default_request_cb); }
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 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(); }