当前位置:Gxlcms > php框架 > swoole服务怎么开启

swoole服务怎么开启

时间:2021-07-01 10:21:17 帮助过:261人阅读

服务启动依赖于进程,在进程中用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服务怎么开启的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行