swTimeWheel* swTimeWheel_new(uint16_t size) { swTimeWheel *tw = sw_malloc(sizeof(swTimeWheel)); if (!tw) { swWarn("malloc(%ld) failed.", sizeof(swTimeWheel)); return NULL; } tw->size = size; tw->current = 0; tw->wheel = sw_calloc(size, sizeof(void*)); if (tw->wheel == NULL) { swWarn("malloc(%ld) failed.", sizeof(void*) * size); sw_free(tw); return NULL; } int i; for (i = 0; i < size; i++) { tw->wheel[i] = swHashMap_new(16, NULL); if (tw->wheel[i] == NULL) { swTimeWheel_free(tw); return NULL; } } return tw; }
/** * create timer */ int swTimer_init(int interval, int use_pipe) { swTimer *timer = &SwooleG.timer; timer->interval = interval; timer->lasttime = interval; #ifndef HAVE_TIMERFD SwooleG.use_timerfd = 0; #endif timer->list = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); if (!timer->list) { return SW_ERR; } if (SwooleG.use_timerfd) { if (swTimer_timerfd_set(timer, interval) < 0) { return SW_ERR; } timer->use_pipe = 0; } else { if (use_pipe) { if (swPipeNotify_auto(&timer->pipe, 0, 0) < 0) { return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; } else { timer->fd = 1; timer->use_pipe = 0; } if (swTimer_signal_set(timer, interval) < 0) { return SW_ERR; } swSignal_add(SIGALRM, swTimer_signal_handler); } if (timer->fd > 1) { SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_TIMER, swTimer_event_handler); SwooleG.main_reactor->add(SwooleG.main_reactor, SwooleG.timer.fd, SW_FD_TIMER); } timer->add = swTimer_add; timer->del = swTimer_del; timer->select = swTimer_select; timer->free = swTimer_free; return SW_OK; }
swTable* swTable_new(uint32_t rows_size) { swTable *table = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swTable)); if (table == NULL) { return NULL; } if (swMutex_create(&table->lock, 1) < 0) { swWarn("mutex create failed."); return NULL; } table->iterator = sw_malloc(sizeof(swTable_iterator)); if (!table->iterator) { swWarn("malloc failed."); return NULL; } table->columns = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, (swHashMap_dtor)swTableColumn_free); if (!table->columns) { return NULL; } table->size = rows_size; bzero(table->iterator, sizeof(swTable_iterator)); table->memory = NULL; return table; }
void swoole_async_init(int module_number TSRMLS_DC) { bzero(&SwooleAIO, sizeof(SwooleAIO)); REGISTER_LONG_CONSTANT("SWOOLE_AIO_BASE", 0, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SWOOLE_AIO_LINUX", 0, CONST_CS | CONST_PERSISTENT); php_swoole_open_files = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (php_swoole_open_files == NULL) { swoole_php_fatal_error(E_ERROR, "create hashmap[1] failed."); } php_swoole_aio_request = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, php_swoole_file_request_free); if (php_swoole_aio_request == NULL) { swoole_php_fatal_error(E_ERROR, "create hashmap[2] failed."); } }
static PHP_METHOD(swoole_http2_client_coro, __construct) { char *host; zend_size_t host_len; long port = 80; zend_bool ssl = SW_FALSE; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &host, &host_len, &port, &ssl) == FAILURE) { return; } if (host_len <= 0) { zend_throw_exception(swoole_exception_class_entry_ptr, "host is empty.", SW_ERROR_INVALID_PARAMS TSRMLS_CC); RETURN_FALSE; } http2_client_property *hcc; hcc = (http2_client_property*) emalloc(sizeof(http2_client_property)); bzero(hcc, sizeof(http2_client_property)); swoole_set_property(getThis(), HTTP2_CLIENT_CORO_PROPERTY, hcc); php_context *context = emalloc(sizeof(php_context)); swoole_set_property(getThis(), HTTP2_CLIENT_CORO_CONTEXT, context); context->onTimeout = NULL; #if PHP_MAJOR_VERSION < 7 context->coro_params = getThis(); #else context->coro_params = *getThis(); #endif hcc->streams = swHashMap_new(8, http2_client_stream_free); hcc->stream_id = 1; long type = SW_FLAG_ASYNC | SW_SOCK_TCP; if (ssl) { #ifdef SW_USE_OPENSSL type |= SW_SOCK_SSL; hcc->ssl = 1; #else swoole_php_fatal_error(E_ERROR, "require openssl library."); #endif } zend_update_property_long(swoole_http2_client_coro_class_entry_ptr, getThis(), ZEND_STRL("type"), type TSRMLS_CC); hcc->host = estrndup(host, host_len); hcc->host_len = host_len; hcc->port = port; }
static PHP_METHOD(swoole_http2_client, __construct) { char *host; zend_size_t host_len; long port = 80; zend_bool ssl = SW_FALSE; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &host, &host_len, &port, &ssl) == FAILURE) { return; } if (host_len <= 0) { zend_throw_exception(swoole_exception_class_entry_ptr, "host is empty.", SW_ERROR_INVALID_PARAMS TSRMLS_CC); RETURN_FALSE; } http2_client_property *hcc; hcc = (http2_client_property*) emalloc(sizeof(http2_client_property)); bzero(hcc, sizeof(http2_client_property)); swoole_set_property(getThis(), HTTP2_CLIENT_PROPERTY_INDEX, hcc); hcc->requests = swLinkedList_new(0, http2_client_request_free); hcc->stream_requests = swLinkedList_new(0, http2_client_request_free); hcc->streams = swHashMap_new(8, http2_client_stream_free); hcc->stream_id = 1; zval *ztype; SW_MAKE_STD_ZVAL(ztype); long type = SW_FLAG_ASYNC | SW_SOCK_TCP; if (ssl) { type |= SW_SOCK_SSL; hcc->ssl = 1; } ZVAL_LONG(ztype, type); zval *zobject = getThis(); zval *retval = NULL; sw_zend_call_method_with_1_params(&zobject, swoole_client_class_entry_ptr, NULL, "__construct", &retval, ztype); if (retval) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&ztype); hcc->host = estrndup(host, host_len); hcc->host_len = host_len; hcc->port = port; }
void swoole_async_init(int module_number TSRMLS_DC) { bzero(&SwooleAIO, sizeof(SwooleAIO)); REGISTER_LONG_CONSTANT("SWOOLE_AIO_BASE", SW_AIO_BASE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SWOOLE_AIO_GCC", SW_AIO_GCC, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SWOOLE_AIO_LINUX", SW_AIO_LINUX, CONST_CS | CONST_PERSISTENT); php_swoole_open_files = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (php_swoole_open_files == NULL) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "create hashmap failed."); } }
void swoole_client_init(int module_number TSRMLS_DC) { INIT_CLASS_ENTRY(swoole_client_ce, "swoole_client", swoole_client_methods); swoole_client_class_entry_ptr = zend_register_internal_class(&swoole_client_ce TSRMLS_CC); zend_declare_property_long(swoole_client_class_entry_ptr, SW_STRL("errCode")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC); zend_declare_property_long(swoole_client_class_entry_ptr, SW_STRL("sock")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC); php_sw_long_connections = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_OOB"), MSG_OOB TSRMLS_CC); zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_PEEK"), MSG_PEEK TSRMLS_CC); zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_DONTWAIT"), MSG_DONTWAIT TSRMLS_CC); zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_WAITALL"), MSG_WAITALL TSRMLS_CC); }
void swoole_coroutine_util_init(int module_number TSRMLS_DC) { SWOOLE_INIT_CLASS_ENTRY(swoole_coroutine_util_ce, "swoole_coroutine", "Swoole\\Coroutine", swoole_coroutine_util_methods); swoole_coroutine_util_class_entry_ptr = zend_register_internal_class(&swoole_coroutine_util_ce TSRMLS_CC); if (SWOOLE_G(use_namespace)) { zend_register_class_alias("swoole_coroutine", swoole_coroutine_util_class_entry_ptr); } else { zend_register_class_alias("Swoole\\Coroutine", swoole_coroutine_util_class_entry_ptr); } defer_coros = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); }
/** * create timer */ int swTimer_create(swTimer *timer, int interval, int use_pipe) { timer->interval = interval; timer->lasttime = interval; #ifndef HAVE_TIMERFD SwooleG.use_timerfd = 0; #endif timer->list = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N); if (!timer->list) { return SW_ERR; } if (SwooleG.use_timerfd) { if (swTimer_timerfd_set(timer, interval) < 0) { return SW_ERR; } timer->use_pipe = 0; } else { if (use_pipe) { if (swPipeNotify_auto(&timer->pipe, 0, 0) < 0) { return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; } else { timer->fd = 1; timer->use_pipe = 0; } if (swTimer_signal_set(timer, interval) < 0) { return SW_ERR; } } return SW_OK; }
int swTimer_init(long msec) { if (SwooleGS->start && (swIsMaster() || swIsManager())) { swWarn("cannot use timer in master and manager process."); return SW_ERR; } if (swReactorTimer_now(&SwooleG.timer.basetime) < 0) { return SW_ERR; } SwooleG.timer._current_id = -1; SwooleG.timer._next_msec = msec; SwooleG.timer._next_id = 1; SwooleG.timer.heap = swHeap_new(1024, SW_MIN_HEAP); if (!SwooleG.timer.heap) { return SW_ERR; } SwooleG.timer.map = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (!SwooleG.timer.map) { swHeap_free(SwooleG.timer.heap); SwooleG.timer.heap = NULL; return SW_ERR; } SwooleG.timer._current_id = -1; SwooleG.timer._next_msec = msec; SwooleG.timer._next_id = 1; SwooleG.timer.add = swTimer_add; if (swIsTaskWorker()) { swSystemTimer_init(msec, SwooleG.use_timer_pipe); } else { swReactorTimer_init(msec); } return SW_OK; }
int swoole_add_function(const char *name, void* func) { if (SwooleG.functions == NULL) { SwooleG.functions = swHashMap_new(64, NULL); if (SwooleG.functions == NULL) { return SW_ERR; } } if (swHashMap_find(SwooleG.functions, (char *) name, strlen(name)) != NULL) { swWarn("Function '%s' has already been added.", name); return SW_ERR; } return swHashMap_add(SwooleG.functions, (char *) name, strlen(name), func); }
swTable* swTable_new(uint32_t rows_size) { if (rows_size >= 0x80000000) { rows_size = 0x80000000; } else { uint32_t i = 10; while ((1U << i) < rows_size) { i++; } rows_size = 1 << i; } swTable *table = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swTable)); if (table == NULL) { return NULL; } if (swMutex_create(&table->lock, 1) < 0) { swWarn("mutex create failed."); return NULL; } table->iterator = sw_malloc(sizeof(swTable_iterator)); if (!table->iterator) { swWarn("malloc failed."); return NULL; } table->columns = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, (swHashMap_dtor)swTableColumn_free); if (!table->columns) { return NULL; } table->size = rows_size; table->mask = rows_size - 1; bzero(table->iterator, sizeof(swTable_iterator)); table->memory = NULL; return table; }
swModule* swModule_load(char *so_file) { int (*init_func)(swModule*); void *handle = dlopen(so_file, RTLD_LAZY); if (!handle) { swWarn("dlopen() failed. Error: %s", dlerror()); return NULL; } //malloc swModule *module = (swModule *) sw_malloc(sizeof(swModule)); if (module == NULL) { swoole_error_log(SW_LOG_ERROR, SW_ERROR_MALLOC_FAIL, "malloc failed."); return NULL; } //get init function init_func = (int (*)(swModule*)) dlsym(handle, SW_MODULE_INIT_FUNC); char *error = dlerror(); if (error != NULL) { swWarn("dlsym() failed. Error: %s", error); sw_free(module); return NULL; } module->file = strdup(so_file); //create function hashmap module->functions = swHashMap_new(64, NULL); if (module->functions == NULL) { sw_free(module); return NULL; } //init module if ((*init_func)(module) < 0) { sw_free(module); return NULL; } return module; }
void swoole_coroutine_util_init(int module_number TSRMLS_DC) { SWOOLE_INIT_CLASS_ENTRY(swoole_coroutine_util_ce, "swoole_coroutine", "Swoole\\Coroutine", swoole_coroutine_util_methods); swoole_coroutine_util_class_entry_ptr = zend_register_internal_class(&swoole_coroutine_util_ce TSRMLS_CC); INIT_CLASS_ENTRY(swoole_coroutine_iterator_ce, "Swoole\\Coroutine\\Iterator", iterator_methods); swoole_coroutine_iterator_class_entry_ptr = zend_register_internal_class(&swoole_coroutine_iterator_ce TSRMLS_CC); zend_class_implements(swoole_coroutine_iterator_class_entry_ptr TSRMLS_CC, 1, zend_ce_iterator); #ifdef SW_HAVE_COUNTABLE zend_class_implements(swoole_coroutine_iterator_class_entry_ptr TSRMLS_CC, 1, zend_ce_countable); #endif if (SWOOLE_G(use_namespace)) { sw_zend_register_class_alias("swoole_coroutine", swoole_coroutine_util_class_entry_ptr); } else { sw_zend_register_class_alias("Swoole\\Coroutine", swoole_coroutine_util_class_entry_ptr); } if (SWOOLE_G(use_shortname)) { sw_zend_register_class_alias("Co", swoole_coroutine_util_class_entry_ptr); } defer_coros = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); //prohibit exit in coroutine INIT_CLASS_ENTRY(swoole_exit_exception_ce, "Swoole\\ExitException", swoole_exit_exception_methods); swoole_exit_exception_class_entry_ptr = zend_register_internal_class_ex(&swoole_exit_exception_ce, zend_exception_get_default()); SWOOLE_DEFINE(EXIT_IN_COROUTINE); SWOOLE_DEFINE(EXIT_IN_SERVER); if (SWOOLE_G(cli)) { ori_exit_handler = zend_get_user_opcode_handler(ZEND_EXIT); zend_set_user_opcode_handler(ZEND_EXIT, coro_exit_handler); } }
int swTimer_init(long msec) { if (swTimer_now(&SwooleG.timer.basetime) < 0) { return SW_ERR; } SwooleG.timer.heap = swHeap_new(1024, SW_MIN_HEAP); if (!SwooleG.timer.heap) { return SW_ERR; } SwooleG.timer.map = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (!SwooleG.timer.map) { swHeap_free(SwooleG.timer.heap); SwooleG.timer.heap = NULL; return SW_ERR; } SwooleG.timer._current_id = -1; SwooleG.timer._next_msec = msec; SwooleG.timer._next_id = 1; SwooleG.timer.add = swTimer_add; SwooleG.timer.round = 0; if (swIsTaskWorker()) { swSystemTimer_init(msec, SwooleG.use_timer_pipe); } else { swReactorTimer_init(msec); } return SW_OK; }
/** * Process manager */ int swProcessPool_create(swProcessPool *pool, int worker_num, int max_request, key_t msgqueue_key, int ipc_mode) { bzero(pool, sizeof(swProcessPool)); pool->worker_num = worker_num; pool->max_request = max_request; pool->workers = SwooleG.memory_pool->alloc(SwooleG.memory_pool, worker_num * sizeof(swWorker)); if (pool->workers == NULL) { swSysError("malloc[1] failed."); return SW_ERR; } if (ipc_mode == SW_IPC_MSGQUEUE) { pool->use_msgqueue = 1; pool->msgqueue_key = msgqueue_key; pool->queue = sw_malloc(sizeof(swMsgQueue)); if (pool->queue == NULL) { swSysError("malloc[2] failed."); return SW_ERR; } if (swMsgQueue_create(pool->queue, 1, pool->msgqueue_key, 1) < 0) { return SW_ERR; } } else if (ipc_mode == SW_IPC_SOCKET) { pool->use_socket = 1; pool->stream = sw_malloc(sizeof(swStreamInfo)); if (pool->stream == NULL) { swWarn("malloc[2] failed."); return SW_ERR; } pool->stream->last_connection = 0; } else if (ipc_mode == SW_IPC_UNIXSOCK) { pool->pipes = sw_calloc(worker_num, sizeof(swPipe)); if (pool->pipes == NULL) { swWarn("malloc[2] failed."); return SW_ERR; } swPipe *pipe; int i; for (i = 0; i < worker_num; i++) { pipe = &pool->pipes[i]; if (swPipeUnsock_create(pipe, 1, SOCK_DGRAM) < 0) { return SW_ERR; } pool->workers[i].pipe_master = pipe->getFd(pipe, SW_PIPE_MASTER); pool->workers[i].pipe_worker = pipe->getFd(pipe, SW_PIPE_WORKER); pool->workers[i].pipe_object = pipe; } } else { swWarn("unknown ipc_type [%d].", ipc_mode); return SW_ERR; } pool->map = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (pool->map == NULL) { swProcessPool_free(pool); return SW_ERR; } pool->ipc_mode = ipc_mode; pool->main_loop = swProcessPool_worker_loop; return SW_OK; }
static int swClient_inet_addr(swClient *cli, char *host, int port) { struct hostent *host_entry; void *s_addr = NULL; if (cli->type == SW_SOCK_TCP || cli->type == SW_SOCK_UDP) { cli->server_addr.addr.inet_v4.sin_family = AF_INET; cli->server_addr.addr.inet_v4.sin_port = htons(port); cli->server_addr.len = sizeof(cli->server_addr.addr.inet_v4); s_addr = &cli->server_addr.addr.inet_v4.sin_addr.s_addr; if (inet_pton(AF_INET, host, s_addr)) { return SW_OK; } } else if (cli->type == SW_SOCK_TCP6 || cli->type == SW_SOCK_UDP6) { cli->server_addr.addr.inet_v6.sin6_family = AF_INET6; cli->server_addr.addr.inet_v6.sin6_port = htons(port); cli->server_addr.len = sizeof(cli->server_addr.addr.inet_v6); s_addr = cli->server_addr.addr.inet_v6.sin6_addr.s6_addr; if (inet_pton(AF_INET6, host, s_addr)) { return SW_OK; } } else if (cli->type == SW_SOCK_UNIX_STREAM || cli->type == SW_SOCK_UNIX_DGRAM) { cli->server_addr.addr.un.sun_family = AF_UNIX; strncpy(cli->server_addr.addr.un.sun_path, host, sizeof(cli->server_addr.addr.un.sun_path)); cli->server_addr.len = sizeof(cli->server_addr.addr.un); return SW_OK; } if (!swoole_dns_cache) { swoole_dns_cache = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); } swDNS_cache *cache = swHashMap_find(swoole_dns_cache, host, strlen(host)); if (cache == NULL) { if (cli->async) { swWarn("DNS lookup will block the process. Please use swoole_async_dns_lookup."); } if (!(host_entry = gethostbyname(host))) { swWarn("gethostbyname('%s') failed.", host); return SW_ERR; } if (host_entry->h_addrtype != AF_INET) { swWarn("Host lookup failed: Non AF_INET domain returned on AF_INET socket."); return 0; } cache = sw_malloc(sizeof(int) + host_entry->h_length); if (cache == NULL) { swWarn("malloc() failed."); memcpy(s_addr, host_entry->h_addr_list[0], host_entry->h_length); return SW_OK; } else { memcpy(cache->addr, host_entry->h_addr_list[0], host_entry->h_length); cache->length = host_entry->h_length; } swHashMap_add(swoole_dns_cache, host, strlen(host), cache); } memcpy(s_addr, cache->addr, cache->length); return SW_OK; }
/** * Process manager */ int swProcessPool_create(swProcessPool *pool, int worker_num, int max_request, key_t msgqueue_key, int create_pipe) { bzero(pool, sizeof(swProcessPool)); pool->worker_num = worker_num; pool->max_request = max_request; if (msgqueue_key > 0) { pool->use_msgqueue = 1; pool->msgqueue_key = msgqueue_key; } pool->workers = SwooleG.memory_pool->alloc(SwooleG.memory_pool, worker_num * sizeof(swWorker)); if (pool->workers == NULL) { swSysError("malloc[1] failed."); return SW_ERR; } pool->map = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL); if (pool->map == NULL) { return SW_ERR; } pool->queue = sw_malloc(sizeof(swQueue)); if (pool->queue == NULL) { swSysError("malloc[2] failed."); return SW_ERR; } int i; if (pool->use_msgqueue) { if (swQueueMsg_create(pool->queue, 1, pool->msgqueue_key, 1) < 0) { return SW_ERR; } } else if (create_pipe) { pool->pipes = sw_calloc(worker_num, sizeof(swPipe)); if (pool->pipes == NULL) { swWarn("malloc[2] failed."); sw_free(pool->workers); return SW_ERR; } swPipe *pipe; for (i = 0; i < worker_num; i++) { pipe = &pool->pipes[i]; if (swPipeUnsock_create(pipe, 1, SOCK_DGRAM) < 0) { return SW_ERR; } pool->workers[i].pipe_master = pipe->getFd(pipe, SW_PIPE_MASTER); pool->workers[i].pipe_worker = pipe->getFd(pipe, SW_PIPE_WORKER); pool->workers[i].pipe_object = pipe; } } pool->main_loop = swProcessPool_worker_start; return SW_OK; }
/** * DNS lookup */ int swoole_gethostbyname(int flags, char *name, char *addr) { SwooleGS->lock.lock(&SwooleGS->lock); swHashMap *cache_table; int __af = flags & (~SW_DNS_LOOKUP_CACHE_ONLY) & (~SW_DNS_LOOKUP_RANDOM); if (__af == AF_INET) { if (!swoole_dns_cache_v4) { swoole_dns_cache_v4 = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); } cache_table = swoole_dns_cache_v4; } else if (__af == AF_INET6) { if (!swoole_dns_cache_v6) { swoole_dns_cache_v6 = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); } cache_table = swoole_dns_cache_v6; } else { SwooleGS->lock.unlock(&SwooleGS->lock); return SW_ERR; } int name_length = strlen(name); int index = 0; swDNS_cache *cache = swHashMap_find(cache_table, name, name_length); if (cache == NULL && (flags & SW_DNS_LOOKUP_CACHE_ONLY)) { SwooleGS->lock.unlock(&SwooleGS->lock); return SW_ERR; } if (cache == NULL) { struct hostent *host_entry; if (!(host_entry = gethostbyname2(name, __af))) { SwooleGS->lock.unlock(&SwooleGS->lock); return SW_ERR; } cache = sw_malloc(sizeof(swDNS_cache)); if (cache == NULL) { SwooleGS->lock.unlock(&SwooleGS->lock); memcpy(addr, host_entry->h_addr_list[0], host_entry->h_length); return SW_OK; } bzero(cache, sizeof(swDNS_cache)); int i = 0; for (i = 0; i < SW_DNS_LOOKUP_CACHE_SIZE; i++) { if (host_entry->h_addr_list[i] == NULL) { break; } if (__af == AF_INET) { memcpy(&cache->addr[i].v4, host_entry->h_addr_list[i], host_entry->h_length); } else { memcpy(&cache->addr[i].v6, host_entry->h_addr_list[i], host_entry->h_length); } } cache->number = i; cache->addr_length = host_entry->h_length; swHashMap_add(cache_table, name, name_length, cache); } SwooleGS->lock.unlock(&SwooleGS->lock); if (flags & SW_DNS_LOOKUP_RANDOM) { index = rand() % cache->number; } if (__af == AF_INET) { memcpy(addr, &cache->addr[index].v4, cache->addr_length); } else { memcpy(addr, &cache->addr[index].v6, cache->addr_length); } return SW_OK; }
/** * Process manager */ int swProcessPool_create(swProcessPool *pool, int worker_num, int max_request, key_t msgqueue_key) { bzero(pool, sizeof(swProcessPool)); pool->worker_num = worker_num; pool->max_request = max_request; if (msgqueue_key > 0) { pool->use_msgqueue = 1; pool->msgqueue_key = msgqueue_key; } pool->workers = sw_calloc(worker_num, sizeof(swWorker)); if (pool->workers == NULL) { swWarn("malloc[1] failed."); return SW_ERR; } pool->map = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); if (pool->map == NULL) { sw_free(pool->workers); return SW_ERR; } int i; if (pool->use_msgqueue) { pool->pipes = sw_calloc(worker_num, sizeof(swPipe)); if (pool->pipes == NULL) { swWarn("malloc[2] failed."); sw_free(pool->workers); return SW_ERR; } if (swQueueMsg_create(&pool->queue, 1, pool->msgqueue_key, 1) < 0) { return SW_ERR; } } else { swPipe *pipe; for (i = 0; i < worker_num; i++) { pipe = &pool->pipes[i]; if (swPipeUnsock_create(pipe, 1, SOCK_DGRAM) < 0) { return SW_ERR; } swProcessPool_worker(pool, i).pipe_master = pipe->getFd(pipe, 1); swProcessPool_worker(pool, i).pipe_worker = pipe->getFd(pipe, 0); } } for (i = 0; i < worker_num; i++) { swProcessPool_worker(pool, i).id = i; swProcessPool_worker(pool, i).pool = pool; } pool->main_loop = swProcessPool_worker_start; return SW_OK; }