• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

phplibevent扩展使用示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php  
define('CONNECT_IN_MSG', chr(1));  
define('CONNECT_OUT_MSG', chr(2));  
  
  
class epoll{  
    private static $socket;  
    public static $connections;  
    private static $buffers;  
    //private static $msgs;  
    public static $fd2pid = array();  
    public static $blocked_ips = array();  
    //private static $link_msg;  
    function epoll($port, &$_msgs){//, &$_link_msg  
        //self::$msgs = $_msgs;  
        //self::$link_msg = $_link_msg;  
        if($port<1024) die("Port must be a number which bigger than 1024\n");  
        self::$socket = stream_socket_server ('tcp://0.0.0.0:'.$port, $errno, $errstr);  
        stream_set_blocking(self::$socket, 0);  
        $base = event_base_new();  
        $event = event_new();  
        event_set($event, self::$socket , EV_READ | EV_PERSIST, 'epoll::ev_accept', $base);  
        event_base_set($event, $base);  
        event_add($event);  
        event_base_loop($base);  
  
        self::$connections = array();  
        self::$buffers = array();  
        //echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);  
    }  
    public static function ev_accept($socket, $flag, $base) {  
        //echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);  
        static $id = 0;  
  
        $connection = stream_socket_accept($socket);  
        stream_set_blocking($connection, 0);  
        list($remoteIP, $remotePort) = explode(':',stream_socket_get_name($connection,true));  
          
        $id ++;  
  
        $buffer = event_buffer_new($connection, 'epoll::ev_read', NULL, 'epoll::ev_error', $id);  
        event_buffer_base_set($buffer, $base);  
        event_buffer_timeout_set($buffer, 30, 30);  
        event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);  
        event_buffer_priority_set($buffer, 60);//超时自动断开时间  
        event_buffer_enable($buffer, EV_READ | EV_PERSIST);  
  
        // we need to save both buffer and connection outside  
        self::$connections[$id] = $connection;  
        self::$buffers[$id] = $buffer;  
        //self::$link_msg->send(CONNECT_IN_MSG.$remoteIP.','.$remotePort, $id);  
          
        self::$fd2pid[$id] = 0;  
        //echo 'In-> $id='.$id.',$connection='.$connection."\n";  
    }  
    public static function ev_error($buffer, $error, $id) {  
        event_buffer_disable(self::$buffers[$id], EV_READ | EV_WRITE);  
        event_buffer_free(self::$buffers[$id]);  
        //echo 'Ot-> $id='.$id."\n";  
        //self::$link_msg->send(CONNECT_OUT_MSG, $id);  
        fclose(self::$connections[$id]);  
        unset(self::$fd2pid[$id]);  
        unset(self::$buffers[$id], self::$connections[$id]);  
    }  
    public static function ev_read($buffer, $id) {  
        //echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);  
        static $ct=0;  
        while ($read = event_buffer_read($buffer, 256))  
        {  
            $ct+=strlen($read);  
            if(strpos($read,'ct')!==false) echo 'Ct=>'.count(self::$connections).'\n';  
            $mid = self::$fd2pid[$id];  
            //self::$msgs[$mid]->send($read , $id);  
        }  
    }  
  
}  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP-验证类发布时间:2022-07-10
下一篇:
PHP中多维数组查找某个值是否存在的方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap