GatewayProtocol.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace 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 socket_id,
  13. * unsigned int client_ip,
  14. * unsigned short client_port,
  15. * unsigned int client_id,
  16. * unsigned int ext_len,
  17. * char[ext_len] ext_data,
  18. * char[pack_length-HEAD_LEN] body//包体
  19. * }
  20. *
  21. *
  22. * @author walkor <walkor@workerman.net>
  23. */
  24. class GatewayProtocol
  25. {
  26. // 发给worker,gateway有一个新的连接
  27. const CMD_ON_GATEWAY_CONNECTION = 1;
  28. // 发给worker的,客户端有消息
  29. const CMD_ON_MESSAGE = 3;
  30. // 发给worker上的关闭链接事件
  31. const CMD_ON_CLOSE = 4;
  32. // 发给gateway的向单个用户发送数据
  33. const CMD_SEND_TO_ONE = 5;
  34. // 发给gateway的向所有用户发送数据
  35. const CMD_SEND_TO_ALL = 6;
  36. // 发给gateway的踢出用户
  37. const CMD_KICK = 7;
  38. // 发给gateway,通知用户session更改
  39. const CMD_UPDATE_SESSION = 9;
  40. // 获取在线状态
  41. const CMD_GET_ONLINE_STATUS = 10;
  42. // 判断是否在线
  43. const CMD_IS_ONLINE = 11;
  44. /**
  45. * 包头长度
  46. * @var integer
  47. */
  48. const HEAD_LEN = 29;
  49. /**
  50. * 协议头
  51. * @var array
  52. */
  53. public $header = array(
  54. 'pack_len' => self::HEAD_LEN,
  55. 'cmd' => 0,
  56. 'local_ip' => '',
  57. 'local_port' => 0,
  58. 'socket_id' => 0,
  59. 'client_ip' => '',
  60. 'client_port' => 0,
  61. 'client_id' => 0,
  62. 'ext_len' => 0,
  63. );
  64. /**
  65. * 扩展数据,
  66. * gateway发往worker时这里存储的是session字符串
  67. * worker发往gateway时,并且CMD_UPDATE_SESSION时存储的是session字符串
  68. * worker发往gateway时,并且CMD_SEND_TO_ALL时存储的是接收的client_id序列,可能是空(代表向所有人发)
  69. * @var string
  70. */
  71. public $ext_data = '';
  72. /**
  73. * 包体
  74. * @var string
  75. */
  76. public $body = '';
  77. /**
  78. * 初始化
  79. * @return void
  80. */
  81. public function __construct($buffer = null)
  82. {
  83. if($buffer)
  84. {
  85. $data = self::decode($buffer);
  86. $this->ext_data = $data['ext_data'];
  87. $this->body = $data['body'];
  88. unset($data['ext_data'], $data['body']);
  89. $this->header = $data;
  90. }
  91. }
  92. /**
  93. * 判断数据包是否都到了
  94. * @param string $buffer
  95. * @return int int=0数据是完整的 int>0数据不完整,还要继续接收int字节
  96. */
  97. public static function input($buffer)
  98. {
  99. $len = strlen($buffer);
  100. // 至少需要四字节才能解出包的长度
  101. if($len < 4)
  102. {
  103. return 4 - $len;
  104. }
  105. $data = unpack("Npack_len", $buffer);
  106. return $data['pack_len'] - $len;
  107. }
  108. /**
  109. * 获取整个包的buffer
  110. * @param string $data
  111. * @return string
  112. */
  113. public function getBuffer()
  114. {
  115. $this->header['ext_len'] = strlen($this->ext_data);
  116. $this->header['pack_len'] = self::HEAD_LEN + $this->header['ext_len'] + strlen($this->body);
  117. return pack("NCNnNNnNN", $this->header['pack_len'],
  118. $this->header['cmd'], ip2long($this->header['local_ip']),
  119. $this->header['local_port'], $this->header['socket_id'],
  120. ip2long($this->header['client_ip']), $this->header['client_port'],
  121. $this->header['client_id'],
  122. $this->header['ext_len']) . $this->ext_data . $this->body;
  123. }
  124. /**
  125. * 从二进制数据转换为数组
  126. * @param string $buffer
  127. * @return array
  128. */
  129. protected static function decode($buffer)
  130. {
  131. $data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nsocket_id/Nclient_ip/nclient_port/Nclient_id/Next_len", $buffer);
  132. $data['local_ip'] = long2ip($data['local_ip']);
  133. $data['client_ip'] = long2ip($data['client_ip']);
  134. if($data['ext_len'] > 0)
  135. {
  136. $data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
  137. $data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
  138. }
  139. else
  140. {
  141. $data['ext_data'] = '';
  142. $data['body'] = substr($buffer, self::HEAD_LEN);
  143. }
  144. return $data;
  145. }
  146. }