1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * logs server class
- * 使用方法:
- * $serv = new logsServer();
- * $serv->run( $array);
- * User: KevinXie
- * Date: 2016/08/10
- */
- class logsServer
- {
- private $serv;
- private $mode = SWOOLE_PROCESS; //运行的模式
- private $sock_type = SWOOLE_SOCK_UDP; //类型
-
- public function __construct()
- {
-
- }
- /**
- * 执行
- * @param unknown $array = array(
- * ip => 'ip',
- * port => 'port',
- * worker_num => 'worker_num',
- * dispatch_mode => 'dispatch_mode',
- * daemonize => 'daemonize'
- * );
- * @return boolean
- */
- public function run( $array)
- {
- if (( ! $ip = oo::functions()->escape( $array['ip'])) || ( ! $port = oo::functions()->uint( $array['port'])))
- {
- return false;
- }
- $worker_num = oo::functions()->uint( $array['worker_num']);
- $dispatch_mode = oo::functions()->uint( $array['dispatch_mode']);
- $daemonize = oo::functions()->uint( $array['daemonize']);
-
- $serv = new swoole_server( $ip, $port, $this->mode, $this->sock_type);
- $serv->set(array(
- 'worker_num' => $worker_num, //worker process num
- 'dispatch_mode' => $dispatch_mode, //数据包分发策略
- 'daemonize' => $daemonize
- ));
- $serv->on('Packet', array( $this, 'onPacket'));
- $serv->start();
- }
-
- /**
- * 监听数据发送事件
- * @param unknown $serv
- * @param unknown $data
- * @param unknown $clientInfo
- * @return boolean
- */
- public function onPacket( $serv, $data, $clientInfo)
- {
- $array = json_decode( $data, true);
- if( $array['type'] == 'reload')
- {
- $serv->reload();
- }
- elseif ( $array['type'] == 'swoole_async_writefile')//覆盖写
- {
- swoole_async_writefile( PATH_DAT . 'log/' .$array['filename'], $array['content']);
- }
- elseif ( $array['type'] == 'swoole_async_write')//异步写文件,可后面追加
- {
- swoole_async_write( PATH_DAT . 'log/' .$array['filename'], $array['content']);
- }
- else
- {
- $function = $array['type'];
- $obj = oo::logs();
- if ( ! method_exists( $obj, $function)) {
- return false;
- }
- $obj->$function( $array['content'], $array['filename']);
- }
- }
- }//end logsServer
|