StatisticProtocol.php 3.1 KB

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