• PHP之高性能I/O框架:Libevent(三)


    Swoole

    Swoole里也提供了一些直接操作底层epoll/kqueue事件循环的接口,可将其他扩展创建的socket、PHP代码中stream/socket扩展创建的socket等加入到Swoole的EventLoop中。

    文档:https://wiki.swoole.com/wiki/page/242.html

    这里我也简单介绍一下。

    基本使用

    swoole_tcp_server.php

    <?php 
    /**
     * Created by PhpStorm.
     * User: 公众号: 飞鸿影的博客(fhyblog)
     * Date: 2018/6/30
     */
    
    use SwooleEvent;
    
    $socket = stream_socket_server ("tcp://0.0.0.0:9201", $errno, $errstr);
    if (false === $socket ) {
        echo "$errstr($errno)
    ";
        exit();
    }
    if (!$socket) die($errstr."--".$errno);
    // stream_set_blocking($socket,0); //可以去掉,没有涉及到read这个socket
    
    echo "waiting client...
    ";
    
    
    //accept事件回调函数,参数分别是$fd
    function ev_accept($socket){
        global $master;
    
        $connection = stream_socket_accept($socket);
    
        //参数的设置将会影响到像 fgets() 和 fread() 这样的函数从资源流里读取数据。 
        //在非阻塞模式下,调用 fgets() 总是会立即返回;而在阻塞模式下,将会一直等到从资源流里面获取到数据才能返回。
        stream_set_blocking($connection, 0);//如果不设置,后续读取会阻塞
        $id = (int)$connection;
    
        echo "new Client $id
    ";
    
        Event::add($connection, 'ev_read', null, SWOOLE_EVENT_READ); 
    }
    
    //read事件回调函数,参数是fd
    function ev_read($buffer)
    {
        $receive = '';
    
        //循环读取并解析客户端消息
        while( 1 ) {
            $read = @fread($buffer, 2);
    
            //客户端异常断开
            if($read === '' || $read === false){
                break;
            }
    
            $pos = strpos($read, "
    ");
            if($pos === false)
            {
                $receive .= $read;
                // echo "received:".$read.";not all package,continue recdiveing
    ";
            }else{
                $receive .= trim(substr ($read,0,$pos+1));
                $read = substr($read,$pos+1);
                
                switch ( $receive ){
                    case "quit":
                        echo "client close conn
    ";
                        
                        //关闭客户端连接
                        Event::del($buffer);//删除事件
                        fclose($buffer);//断开客户端连接
                        break;
                    default:
                        // echo "all package:
    ";
                        echo $receive."
    ";
                        break;
                }
                $receive='';
            }
        }
    }
    
    Event::add($socket, 'ev_accept', null, SWOOLE_EVENT_READ); 
    echo  "start run...
    ";
    
    //进入事件循环
    Event::wait(); //PHP5.4或更高版本不需要加此函数
    
    //下面这句不会被执行
    echo "This code will not be executed.
    ";
    
    

    Event::add()等方法也有对应的函数,例如swoole_event_add()

    定时器

    swoole提供了两个定时器:

    • swoole_timer_tick 周期定时器,面向对象写法:Timer::tick
    • swoole_timer_after 一次性定时器,面向对象写法:Timer::after

    swoole定时器的精度是毫秒。

    示例:

    <?php 
    /**
     * Created by PhpStorm.
     * User: 公众号: 飞鸿影的博客(fhyblog)
     * Date: 2018/6/30
     */
    
    use SwooleTimer;
    
    Timer::after(1000, function(){
        echo time(). " hello
    ";
        Timer::tick(1000, function($timer_id, $params){
            static $c = 0;
            echo time(). " hello $params $c
    ";
    
            $c++;
            if($c > 5){
                Timer::clear($timer_id);
            }
        }, 'this is param');
    });
    

    说明:Timer::after回调函数没有参数,Timer::tick回调函数参数是定时器ID及附加参数。

    总结

    我曾经查阅了Workerman的源码,看到作者把常见的Event(系统自带Select、libevet、Event、Ev、Swoole)进行了一次封装,对外暴露了统一的接口(add、del、loop),大家有兴趣可以看看。

    参考

    1、php libevent扩展的简单用例 - NickBai - 博客园
    https://www.cnblogs.com/nickbai/articles/6197689.html
    2、PHP使用pcntl和libevent 实现Timer功能 | 博客水木
    http://www.4u4v.net/php-uses-pcntl-and-libevent-achieve-timer-function.html
    3、socket服务的模型以及实现(4)–单进程IO复用libevent
    http://www.xtgxiso.com/socket服务的模型以及实现4-单进程io复用libevent/
    4、libevent中的bufferevent原理 - nengm - 博客园
    https://www.cnblogs.com/nengm1988/p/8203784.html
    5、EventLoop-Swoole-Swoole文档中心
    https://wiki.swoole.com/wiki/page/242.html

  • 相关阅读:
    springboot 的一般配置
    springmvc 中将MultipartFile转为file,springboot 注入CommonsMultipartResolver
    .OPF文件剖析
    Epub格式的电子书——文件组成
    解决js代码中加入alert()就成功执行,不加就不对的问题!
    java编程之POI读取excel表格的内容
    MyBatis之一对多映射查询sql配置文件。
    springMVC的拦截器
    SpringMvc的上传和下载
    SpringMVC传递数据的流线图
  • 原文地址:https://www.cnblogs.com/52fhy/p/9261325.html
Copyright © 2020-2023  润新知