StatisticWorker.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
  3. /**
  4. *
  5. * @author walkor <worker-man@qq.com>
  6. */
  7. class StatisticWorker extends Man\Core\SocketWorker
  8. {
  9. public function dealInput($recv_str)
  10. {
  11. }
  12. public function dealProcess($recv_str)
  13. {
  14. }
  15. }
  16. /**
  17. *
  18. * struct statisticPortocol
  19. * {
  20. * unsigned char module_name_len;
  21. * unsigned char interface_name_len;
  22. * float cost_time_ms;
  23. * unsigned char success;
  24. * int code;
  25. * unsigned short msg_len;
  26. * unsigned int time;
  27. * unsigned int ip;
  28. * char[module_name_len] module_name;
  29. * char[interface_name_len] interface_name;
  30. * char[msg_len] msg;
  31. * }
  32. *
  33. * @author valkor
  34. */
  35. class statisticProtocol
  36. {
  37. /**
  38. * 包头长度
  39. * @var integer
  40. */
  41. const PACKEGE_FIXED_LENGTH = 21;
  42. /**
  43. * udp 包最大长度
  44. * @var integer
  45. */
  46. const MAX_UDP_PACKGE_SIZE = 65507;
  47. /**
  48. * char类型能保存的最大数值
  49. * @var integer
  50. */
  51. const MAX_CHAR_VALUE = 255;
  52. /**
  53. * usigned short 能保存的最大数值
  54. * @var integer
  55. */
  56. const MAX_UNSIGNED_SHORT_VALUE = 65535;
  57. /**
  58. * 编码
  59. * @param string $module
  60. * @param string $interface
  61. * @param float $cost_time_ms
  62. * @param int $success
  63. * @param int $code
  64. * @param string $msg
  65. * @param string $ip
  66. * @return string
  67. */
  68. public static function encode($module, $interface , $cost_time_ms, $success, $code = 0,$msg = '', $ip = '127.0.0.1')
  69. {
  70. // 防止模块名过长
  71. if(strlen($module) > self::MAX_CHAR_VALUE)
  72. {
  73. $module = substr($module, 0, self::MAX_CHAR_VALUE);
  74. }
  75. // 防止接口名过长
  76. if(strlen($interface) > self::MAX_CHAR_VALUE)
  77. {
  78. $interface = substr($interface, 0, self::MAX_CHAR_VALUE);
  79. }
  80. // 防止msg过长
  81. $module_name_length = strlen($module);
  82. $interface_name_length = strlen($interface);
  83. $avalible_size = self::MAX_UDP_PACKGE_SIZE - self::PACKEGE_FIXED_LENGTH - $module_name_length - $interface_name_length;
  84. if(strlen($msg) > $avalible_size)
  85. {
  86. $msg = substr($msg, 0, $avalible_size);
  87. }
  88. // 打包
  89. return pack('CCfCNnNN', $module_name_length, $interface_name_length, $cost_time_ms, $success ? 1 : 0, $code, strlen($msg), time(), ip2long($ip)).$module.$interface.$msg;
  90. }
  91. /**
  92. * 解包
  93. * @param string $bin_data
  94. * @return array
  95. */
  96. public static function decode($bin_data)
  97. {
  98. // 解包
  99. $data = unpack("Cmodule_name_len/Cinterface_name_len/fcost_time_ms/Csuccess/Ncode/nmsg_len/Ntime/Nip", $data);
  100. $module = substr($bin_data, self::PACKEGE_FIXED_LENGTH, $data['module_name_len']);
  101. $interface = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'], $data['interface_name_len']);
  102. $msg = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'] + $data['interface_name_len']);
  103. return array(
  104. 'module' => $module,
  105. 'interface' => $interface,
  106. 'cost_time_ms' => $data['cost_time_ms'],
  107. 'success' => $data['success'],
  108. 'time' => $data['time'],
  109. 'ip' => $data['ip'],
  110. 'code' => $data['code'],
  111. 'msg' => $msg,
  112. );
  113. }
  114. }