Loading

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 Swoole\Event;

$socket = stream_socket_server ("tcp://0.0.0.0:9201", $errno, $errstr);
if (false === $socket ) {
    echo "$errstr($errno)\n";
    exit();
}
if (!$socket) die($errstr."--".$errno);
// stream_set_blocking($socket,0); //可以去掉,没有涉及到read这个socket

echo "waiting client...\n";


//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\n";

    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, "\n");
        if($pos === false)
        {
            $receive .= $read;
            // echo "received:".$read.";not all package,continue recdiveing\n";
        }else{
            $receive .= trim(substr ($read,0,$pos+1));
            $read = substr($read,$pos+1);
            
            switch ( $receive ){
                case "quit":
                    echo "client close conn\n";
                    
                    //关闭客户端连接
                    Event::del($buffer);//删除事件
                    fclose($buffer);//断开客户端连接
                    break;
                default:
                    // echo "all package:\n";
                    echo $receive."\n";
                    break;
            }
            $receive='';
        }
    }
}

Event::add($socket, 'ev_accept', null, SWOOLE_EVENT_READ); 
echo  "start run...\n";

//进入事件循环
Event::wait(); //PHP5.4或更高版本不需要加此函数

//下面这句不会被执行
echo "This code will not be executed.\n";

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 Swoole\Timer;

Timer::after(1000, function(){
    echo time(). " hello\n";
    Timer::tick(1000, function($timer_id, $params){
        static $c = 0;
        echo time(). " hello $params $c\n";

        $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

posted @ 2018-07-07 16:12  飞鸿影  阅读(1145)  评论(0编辑  收藏  举报