服務啟動依賴進程,在進程中用swoole_server來創建,原始碼中的別名函數為swoole_server_create,預設的sock_type是tcp,服務模式serv_mode為進程模式。
long sock_type = SW_SOCK_TCP; long serv_port; long serv_mode = SW_MODE_PROCESS;
分配swServer記憶體並初始化
swServer_init(serv);
主要是factory_mode,反應器個數reactor_num,分發模式dispatch_mode,環形緩衝區大小ringbuffer_size,逾時時間(由timeout_sec和timeout_usec),工作進程數workker_num,最大連線數max_connection,最大請求數max_request,http_server服務配置訊息,心跳空閒時間heartbeat_idel_time和檢查時間間隔heartbeat_check_interval,緩衝區的輸入輸出大小buffer_input_size和buffer_output_size及管道快取大小。
swoole_init(); bzero(serv, sizeof(swServer)); serv->factory_mode = SW_MODE_BASE; serv->reactor_num = SW_REACTOR_NUM > SW_REACTOR_MAX_THREAD ? SW_REACTOR_MAX_THREAD : SW_REACTOR_NUM; serv->dispatch_mode = SW_DISPATCH_FDMOD; serv->ringbuffer_size = SW_QUEUE_SIZE; serv->timeout_sec = SW_REACTOR_TIMEO_SEC; serv->timeout_usec = SW_REACTOR_TIMEO_USEC; //300ms; serv->worker_num = SW_CPU_NUM; serv->max_connection = SwooleG.max_sockets; serv->max_request = 0; //http server serv->http_parse_post = 1; serv->upload_tmp_dir = "/tmp"; //heartbeat check serv->heartbeat_idle_time = SW_HEARTBEAT_IDLE; serv->heartbeat_check_interval = SW_HEARTBEAT_CHECK; serv->buffer_input_size = SW_BUFFER_INPUT_SIZE; serv->buffer_output_size = SW_BUFFER_OUTPUT_SIZE; serv->pipe_buffer_size = SW_PIPE_BUFFER_SIZE; SwooleG.serv = serv;
同時解析服務主機ip,連接埠號,服務模式及sock_type,將伺服器監聽連接埠資訊加入listen_list清單中swServer_add_port。
//create server socket int sock = swSocket_create(ls->type); if (sock < 0) { swSysError("create socket failed."); return NULL; } //bind address and port if (swSocket_bind(sock, ls->type, ls->host, ls->port) < 0) { return NULL; } //dgram socket, setting socket buffer size if (swSocket_is_dgram(ls->type)) { setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &ls->socket_buffer_size, sizeof(int)); setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &ls->socket_buffer_size, sizeof(int)); } //O_NONBLOCK & O_CLOEXEC swoole_fcntl_set_option(sock, 1, 1); ls->sock = sock; if (swSocket_is_dgram(ls->type)) { serv->have_udp_sock = 1; serv->dgram_port_num++; if (ls->type == SW_SOCK_UDP) { serv->udp_socket_ipv4 = sock; } else if (ls->type == SW_SOCK_UDP6) { serv->udp_socket_ipv6 = sock; } } else { serv->have_tcp_sock = 1; } LL_APPEND(serv->listen_list, ls); serv->listen_port_num++;
用swoole_server->start來啟動(swoole_server_start),註冊一些預設及自訂回調,日誌初始化,根據服務模式實現工廠的初始化(php_swoole_server_before_start)。
工廠的啟動函數會根據服務模式是建立多進程 還是單進程,執行緒還是執行緒(swServer_create)。
根據是否使用openssl來讀取憑證檔案 ,是否開啟守護模式來做日誌初始化和開啟守護。 swServer_start_proxy建立main_reactor,負責處理監聽套接字,同時建立reactor執行緒。
(建議學習: swoole影片教學)
以上是swoole服務怎麼開啟的詳細內容。更多資訊請關注PHP中文網其他相關文章!