Home > Article > PHP Framework > How to enable swoole service
Service startup depends on the process. It is created with swoole_server in the process. The alias function in the source code is swoole_server_create. The default sock_type is tcp. The service mode serv_mode is process mode.
long sock_type = SW_SOCK_TCP; long serv_port; long serv_mode = SW_MODE_PROCESS;
Allocate swServer memory and initialize
swServer_init(serv);
Mainly factory_mode, reactor number reactor_num, distribution mode dispatch_mode, ring buffer size ringbuffer_size, timeout time (consisting of timeout_sec and timeout_usec), worker process Number of worker_num, maximum number of connections max_connection, maximum number of requests max_request, http_server service configuration information, heartbeat idle time heartbeat_idel_time and check interval heartbeat_check_interval, buffer input and output sizes buffer_input_size and buffer_output_size and pipe cache 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;
At the same time analyze the service host ip, port number, service mode and sock_type, and add the server listening port information to the listen_list 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++;
Use swoole_server->start to start (swoole_server_start), Register some default and custom callbacks, log initialization, and implement factory initialization according to the service mode (php_swoole_server_before_start).
The startup function of the factory will create multiple processes or single processes, threads or threads (swServer_create) according to the service mode.
Depending on whether openssl is used to read the certificate file, whether the guard mode is enabled to initialize the log and enable the guard. swServer_start_proxy creates main_reactor, which is responsible for processing the listening socket and creating the reactor thread.
(Recommended learning: swoole video tutorial)
The above is the detailed content of How to enable swoole service. For more information, please follow other related articles on the PHP Chinese website!