旗下导航:搜·么
当前位置:网站首页 > PHP框架 > Swoole > 正文

swoole效劳怎样开启【swoole】,swoole

作者:搜搜PHP网发布时间:2019-12-09分类:Swoole浏览:73


导读:效劳启动依赖于历程,在历程顶用swoole_server来建立,源码中的别号函数为swoole_server_create,默许的sock_type是tcp,效劳形式ser...

效劳启动依赖于历程,在历程顶用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效劳怎样开启的细致内容,更多请关注ki4网别的相干文章!

标签:swoole