GatewayProtocol.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Workerman\Protocols;
  3. /**
  4. * Gateway与Worker间通讯的二进制协议
  5. *
  6. * struct GatewayProtocol
  7. * {
  8. * unsigned int pack_len,
  9. * unsigned char cmd,//命令字
  10. * unsigned int local_ip,
  11. * unsigned short local_port,
  12. * unsigned int client_ip,
  13. * unsigned short client_port,
  14. * unsigned int client_id,
  15. * unsigned int ext_len,
  16. * char[ext_len] ext_data,
  17. * char[pack_length-HEAD_LEN] body//包体
  18. * }
  19. *
  20. *
  21. * @author walkor <walkor@workerman.net>
  22. */
  23. class GatewayProtocol
  24. {
  25. // 发给worker,gateway有一个新的连接
  26. const CMD_ON_CONNECTION = 1;
  27. // 发给worker的,客户端有消息
  28. const CMD_ON_MESSAGE = 3;
  29. // 发给worker上的关闭链接事件
  30. const CMD_ON_CLOSE = 4;
  31. // 发给gateway的向单个用户发送数据
  32. const CMD_SEND_TO_ONE = 5;
  33. // 发给gateway的向所有用户发送数据
  34. const CMD_SEND_TO_ALL = 6;
  35. // 发给gateway的踢出用户
  36. const CMD_KICK = 7;
  37. // 发给gateway,通知用户session更改
  38. const CMD_UPDATE_SESSION = 9;
  39. // 获取在线状态
  40. const CMD_GET_ONLINE_STATUS = 10;
  41. // 判断是否在线
  42. const CMD_IS_ONLINE = 11;
  43. /**
  44. * 包头长度
  45. * @var integer
  46. */
  47. const HEAD_LEN = 25;
  48. public static $empty = array(
  49. 'cmd' => 0,
  50. 'local_ip' => '0.0.0.0',
  51. 'local_port' => 0,
  52. 'client_ip' => '0.0.0.0',
  53. 'client_port' => 0,
  54. 'client_id' => 0,
  55. 'ext_data' => '',
  56. 'body' => '',
  57. );
  58. /**
  59. * 返回包长度
  60. * @param string $buffer
  61. * @return int return current package length
  62. */
  63. public static function input($buffer)
  64. {
  65. if(strlen($buffer) < self::HEAD_LEN)
  66. {
  67. return 0;
  68. }
  69. $data = unpack("Npack_len", $buffer);
  70. return $data['pack_len'];
  71. }
  72. /**
  73. * 获取整个包的buffer
  74. * @param array $data
  75. * @return string
  76. */
  77. public static function encode($data)
  78. {
  79. $ext_len = strlen($data['ext_data']);
  80. $package_len = self::HEAD_LEN + $ext_len + strlen($data['body']);
  81. return pack("NCNnNnNN", $package_len,
  82. $data['cmd'], ip2long($data['local_ip']),
  83. $data['local_port'], ip2long($data['client_ip']),
  84. $data['client_port'], $data['client_id'],
  85. $ext_len) . $data['ext_data'] . $data['body'];
  86. }
  87. /**
  88. * 从二进制数据转换为数组
  89. * @param string $buffer
  90. * @return array
  91. */
  92. public static function decode($buffer)
  93. {
  94. $data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nclient_ip/nclient_port/Nclient_id/Next_len", $buffer);
  95. $data['local_ip'] = long2ip($data['local_ip']);
  96. $data['client_ip'] = long2ip($data['client_ip']);
  97. if($data['ext_len'] > 0)
  98. {
  99. $data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
  100. $data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
  101. }
  102. else
  103. {
  104. $data['ext_data'] = '';
  105. $data['body'] = substr($buffer, self::HEAD_LEN);
  106. }
  107. return $data;
  108. }
  109. }