search
HomePHP FrameworkSwooleHow to enable swoole service

How to enable swoole service

Dec 09, 2019 am 10:00 AM
swoole

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use