|
|
@@ -25,7 +25,7 @@ class StatisticWorker extends Man\Core\SocketWorker
|
|
|
* unsigned char module_name_len;
|
|
|
* unsigned char interface_name_len;
|
|
|
* float cost_time_ms;
|
|
|
- * unsigned char $success;
|
|
|
+ * unsigned char success;
|
|
|
* int code;
|
|
|
* unsigned short msg_len;
|
|
|
* unsigned int time;
|
|
|
@@ -39,50 +39,90 @@ class StatisticWorker extends Man\Core\SocketWorker
|
|
|
*/
|
|
|
class statisticProtocol
|
|
|
{
|
|
|
+ /**
|
|
|
+ * 包头长度
|
|
|
+ * @var integer
|
|
|
+ */
|
|
|
+ const PACKEGE_FIXED_LENGTH = 21;
|
|
|
|
|
|
- public static function encode($module, $interface , $cost_time_ms, $success, $code = 0,$msg = '')
|
|
|
- {
|
|
|
- $module_len = strlen($module);
|
|
|
- $interface_len = strlen($interface);
|
|
|
- $bin_data = pack("CC", $module_len, $interface_len);
|
|
|
- $bin_data .= self::FToN($cost_time_ms);
|
|
|
- $bin_data .= pack("C", $success);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * udp 包最大长度
|
|
|
+ * @var integer
|
|
|
+ */
|
|
|
+ const MAX_UDP_PACKGE_SIZE = 65507;
|
|
|
|
|
|
- public static function decode($bin_data)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * char类型能保存的最大数值
|
|
|
+ * @var integer
|
|
|
+ */
|
|
|
+ const MAX_CHAR_VALUE = 255;
|
|
|
|
|
|
- public static function IToN($val)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * usigned short 能保存的最大数值
|
|
|
+ * @var integer
|
|
|
+ */
|
|
|
+ const MAX_UNSIGNED_SHORT_VALUE = 65535;
|
|
|
|
|
|
- public static function NToI($val)
|
|
|
+ /**
|
|
|
+ * 编码
|
|
|
+ * @param string $module
|
|
|
+ * @param string $interface
|
|
|
+ * @param float $cost_time_ms
|
|
|
+ * @param int $success
|
|
|
+ * @param int $code
|
|
|
+ * @param string $msg
|
|
|
+ * @param string $ip
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function encode($module, $interface , $cost_time_ms, $success, $code = 0,$msg = '', $ip = '127.0.0.1')
|
|
|
{
|
|
|
- $foo = unpack("N", $val);
|
|
|
- if($foo[1] > 0x7fffffff)
|
|
|
+ // 防止模块名过长
|
|
|
+ if(strlen($module) > self::MAX_CHAR_VALUE)
|
|
|
{
|
|
|
- $foo[1] = 0 - (($foo[1] - 1) ^ 0xffffffff);
|
|
|
+ $module = substr($module, 0, self::MAX_CHAR_VALUE);
|
|
|
}
|
|
|
- return $foo[1];
|
|
|
- }
|
|
|
-
|
|
|
- /* Convert float from HostOrder to Network Order */
|
|
|
- public static function FToN( $val )
|
|
|
- {
|
|
|
- $a = unpack("I",pack( "f",$val ));
|
|
|
- return pack("N",$a[1] );
|
|
|
+
|
|
|
+ // 防止接口名过长
|
|
|
+ if(strlen($interface) > self::MAX_CHAR_VALUE)
|
|
|
+ {
|
|
|
+ $interface = substr($interface, 0, self::MAX_CHAR_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 防止msg过长
|
|
|
+ $module_name_length = strlen($module);
|
|
|
+ $interface_name_length = strlen($interface);
|
|
|
+ $avalible_size = self::MAX_UDP_PACKGE_SIZE - self::PACKEGE_FIXED_LENGTH - $module_name_length - $interface_name_length;
|
|
|
+ if(strlen($msg) > $avalible_size)
|
|
|
+ {
|
|
|
+ $msg = substr($msg, 0, $avalible_size);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 打包
|
|
|
+ return pack('CCfCNnNN', $module_name_length, $interface_name_length, $cost_time_ms, $success ? 1 : 0, $code, strlen($msg), time(), ip2long($ip)).$module.$interface.$msg;
|
|
|
}
|
|
|
-
|
|
|
- /* Convert float from Network Order to HostOrder */
|
|
|
- public static function NToF($val )
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解包
|
|
|
+ * @param string $bin_data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function decode($bin_data)
|
|
|
{
|
|
|
- $a = unpack("N",$val);
|
|
|
- $b = unpack("f",pack( "I",$a[1]));
|
|
|
- return $b[1];
|
|
|
+ // 解包
|
|
|
+ $data = unpack("Cmodule_name_len/Cinterface_name_len/fcost_time_ms/Csuccess/Ncode/nmsg_len/Ntime/Nip", $data);
|
|
|
+ $module = substr($bin_data, self::PACKEGE_FIXED_LENGTH, $data['module_name_len']);
|
|
|
+ $interface = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'], $data['interface_name_len']);
|
|
|
+ $msg = substr($bin_data, self::PACKEGE_FIXED_LENGTH + $data['module_name_len'] + $data['interface_name_len']);
|
|
|
+ return array(
|
|
|
+ 'module' => $module,
|
|
|
+ 'interface' => $interface,
|
|
|
+ 'cost_time_ms' => $data['cost_time_ms'],
|
|
|
+ 'success' => $data['success'],
|
|
|
+ 'time' => $data['time'],
|
|
|
+ 'ip' => $data['ip'],
|
|
|
+ 'code' => $data['code'],
|
|
|
+ 'msg' => $msg,
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|