static int cpWorker_loop(int worker_id, int group_id) { cpWorker_init(worker_id, group_id); cpGroup *G = &CPGS->G[group_id]; cpShareMemory *sm_obj = &(G->workers[worker_id].sm_obj); int ret; cpSettitle(G->name); cpSignalSet(SIGALRM, cpWorker_do_ping, 1, 0); cpSignalSet(SIGTERM, cpWorker_do_stop, 1, 0); alarm(CP_PING_SLEEP); while (CPGS->running) { zval *ret_value; ALLOC_INIT_ZVAL(ret_value); bzero(&CPWG.event, sizeof (cpWorkerInfo)); ret = cpFifoRead(CPWG.pipe_fd_read, &CPWG.event, sizeof (cpWorkerInfo)); if (ret < 0) { cpLog("fifo read Error: %s [%d]", strerror(errno), errno); } CPWG.working = 1; php_msgpack_unserialize(ret_value, sm_obj->mem, CPWG.event.len); worker_onReceive(ret_value); CPWG.working = 0; } return SUCCESS; }
static int cpWorker_loop(int worker_id, int group_id) { CPWG.id = worker_id; CPWG.gid = group_id; cpGroup *G = &CPGS->G[group_id]; char fifo_name[CP_FIFO_NAME_LEN] = {0}; sprintf(fifo_name, "%s_%d", CP_FIFO_NAME_PRE, group_id * CP_GROUP_LEN + worker_id); //client 2 worker int pipe_fd_read = cpCreateFifo(fifo_name); sprintf(fifo_name, "%s_%d_1", CP_FIFO_NAME_PRE, group_id * CP_GROUP_LEN + worker_id); //worker 2 client int pipe_fd_write = cpCreateFifo(fifo_name); G->workers[worker_id].pipe_fd_write = pipe_fd_write; cpShareMemory *sm_obj = &(G->workers[worker_id].sm_obj); cpWorkerInfo event; bzero(&event, sizeof (event)); int ret, len = 0; int event_len = sizeof (event); cpSettitle(G->name); cpSignalSet(SIGALRM, cpWorker_do_ping, 1, 0); alarm(CP_PING_SLEEP); while (CPGS->running) { zval *ret_value; ALLOC_INIT_ZVAL(ret_value); do { ret = cpFifoRead(pipe_fd_read, &event, event_len); if (ret < 0) { cpLog("fifo read Error: %s [%d]", strerror(errno), errno); } } while (event.pid != G->workers[worker_id].CPid); //有可能有脏数据 读出来 CPWG.working = 1; len = event.len; CPWG.clientPid = event.pid; if (ret < 0) { cpLog("fifo read Error: %s [%d]", strerror(errno), errno); } php_msgpack_unserialize(ret_value, sm_obj->mem, len); worker_onReceive(ret_value); CPWG.working = 0; } return SUCCESS; }
void cpServer_init(zval *conf, char *title, char *ini_file, int group_id) { CPGS = (cpServerGS*) cp_mmap_calloc(sizeof (cpServerGS)); if (CPGS == NULL) { php_printf("calloc[1] fail\n"); return; } bzero(&CPGL, sizeof (cpServerG)); CPGC.backlog = CP_BACKLOG; CPGC.reactor_num = CP_CPU_NUM; CPGC.timeout_sec = CP_REACTOR_TIMEO_SEC; CPGC.timeout_usec = CP_REACTOR_TIMEO_USEC; CPGC.max_conn = CP_MAX_FDS; CPGC.max_request = CP_MAX_REQUEST; CPGC.idel_time = CP_IDEL_TIME; CPGC.recycle_num = CP_RECYCLE_NUM; CPGC.max_read_len = CP_DEF_MAX_READ_LEN; CPGC.group_id = group_id; CPGS->worker_max = CP_MAX_WORKER; CPGC.worker_min = CP_MIN_WORKER; CPGC.ser_fail_hits = 1; CPGC.max_fail_num = 2; strcpy(CPGC.title, title); strcpy(CPGC.ini_file, ini_file); cpSettitle(title); zval **v; //daemonize,守护进程化 if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("daemonize"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.daemonize = (int) Z_LVAL_PP(v); } //pool_max if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("pool_max"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGS->worker_max = (int) Z_LVAL_PP(v); } //pool_min if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("pool_min"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.worker_min = (int) Z_LVAL_PP(v); } //pool_min if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("recycle_num"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.recycle_num = (int) Z_LVAL_PP(v); } //error_file if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("log_file"), (void **) &v) == SUCCESS) { memcpy(CPGC.log_file, Z_STRVAL_PP(v), Z_STRLEN_PP(v)); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("max_read_len"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.max_read_len = (int) Z_LVAL_PP(v); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("port"), (void **) &v) == SUCCESS) {//todo check null convert_to_long(*v); CPGC.port = (int) Z_LVAL_PP(v); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("idel_time"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.idel_time = (int) Z_LVAL_PP(v); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("use_wait_queue"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.use_wait_queue = (int) Z_LVAL_PP(v); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("ser_fail_hits"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.ser_fail_hits = (int) Z_LVAL_PP(v); } if (zend_hash_find(Z_ARRVAL_P(conf), ZEND_STRS("max_fail_num"), (void **) &v) == SUCCESS) { convert_to_long(*v); CPGC.max_fail_num = (int) Z_LVAL_PP(v); } }