logs.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * logs server class
  4. * 使用方法:
  5. * $serv = new logsServer();
  6. * $serv->run( $array);
  7. * User: KevinXie
  8. * Date: 2016/08/10
  9. */
  10. class logsServer
  11. {
  12. private $serv;
  13. private $mode = SWOOLE_PROCESS; //运行的模式
  14. private $sock_type = SWOOLE_SOCK_UDP; //类型
  15. public function __construct()
  16. {
  17. }
  18. /**
  19. * 执行
  20. * @param unknown $array = array(
  21. * ip => 'ip',
  22. * port => 'port',
  23. * worker_num => 'worker_num',
  24. * dispatch_mode => 'dispatch_mode',
  25. * daemonize => 'daemonize'
  26. * );
  27. * @return boolean
  28. */
  29. public function run( $array)
  30. {
  31. if (( ! $ip = oo::functions()->escape( $array['ip'])) || ( ! $port = oo::functions()->uint( $array['port'])))
  32. {
  33. return false;
  34. }
  35. $worker_num = oo::functions()->uint( $array['worker_num']);
  36. $dispatch_mode = oo::functions()->uint( $array['dispatch_mode']);
  37. $daemonize = oo::functions()->uint( $array['daemonize']);
  38. $serv = new swoole_server( $ip, $port, $this->mode, $this->sock_type);
  39. $serv->set(array(
  40. 'worker_num' => $worker_num, //worker process num
  41. 'dispatch_mode' => $dispatch_mode, //数据包分发策略
  42. 'daemonize' => $daemonize
  43. ));
  44. $serv->on('Packet', array( $this, 'onPacket'));
  45. $serv->start();
  46. }
  47. /**
  48. * 监听数据发送事件
  49. * @param unknown $serv
  50. * @param unknown $data
  51. * @param unknown $clientInfo
  52. * @return boolean
  53. */
  54. public function onPacket( $serv, $data, $clientInfo)
  55. {
  56. $array = json_decode( $data, true);
  57. if( $array['type'] == 'reload')
  58. {
  59. $serv->reload();
  60. }
  61. elseif ( $array['type'] == 'swoole_async_writefile')//覆盖写
  62. {
  63. swoole_async_writefile( PATH_DAT . 'log/' .$array['filename'], $array['content']);
  64. }
  65. elseif ( $array['type'] == 'swoole_async_write')//异步写文件,可后面追加
  66. {
  67. swoole_async_write( PATH_DAT . 'log/' .$array['filename'], $array['content']);
  68. }
  69. else
  70. {
  71. $function = $array['type'];
  72. $obj = oo::logs();
  73. if ( ! method_exists( $obj, $function)) {
  74. return false;
  75. }
  76. $obj->$function( $array['content'], $array['filename']);
  77. }
  78. }
  79. }//end logsServer