BusinessWorker.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace GatewayWorker;
  3. use \Workerman\Worker;
  4. use \Workerman\Connection\AsyncTcpConnection;
  5. use \Workerman\Protocols\GatewayProtocol;
  6. use \Workerman\Lib\Timer;
  7. use \GatewayWorker\Lib\Lock;
  8. use \GatewayWorker\Lib\Store;
  9. use \GatewayWorker\Lib\Context;
  10. use \Event;
  11. class BusinessWorker extends Worker
  12. {
  13. const MAX_RETRY_COUNT = 5;
  14. public $gatewayConnections = array();
  15. public $badGatewayAddress = array();
  16. public function __construct($socket_name = '', $context_option = array())
  17. {
  18. $this->onWorkerStart = array($this, 'onWorkerStart');
  19. parent::__construct($socket_name, $context_option);
  20. }
  21. protected function onWorkerStart()
  22. {
  23. Timer::add(1, array($this, 'checkGatewayConnections'));
  24. $this->checkGatewayConnections();
  25. \GatewayWorker\Lib\Gateway::setBusinessWorker($this);
  26. }
  27. public function onGatewayMessage($connection, $data)
  28. {
  29. Context::$client_ip = $data['client_ip'];
  30. Context::$client_port = $data['client_port'];
  31. Context::$local_ip = $data['local_ip'];
  32. Context::$local_port = $data['local_port'];
  33. Context::$client_id = $data['client_id'];
  34. $_SERVER = array(
  35. 'REMOTE_ADDR' => Context::$client_ip,
  36. 'REMOTE_PORT' => Context::$client_port,
  37. 'GATEWAY_ADDR' => Context::$local_ip,
  38. 'GATEWAY_PORT' => Context::$local_port,
  39. 'GATEWAY_CLIENT_ID' => Context::$client_id,
  40. );
  41. if($data['ext_data'] != '')
  42. {
  43. $_SESSION = Context::sessionDecode($data['ext_data']);
  44. }
  45. else
  46. {
  47. $_SESSION = null;
  48. }
  49. // 备份一次$data['ext_data'],请求处理完毕后判断session是否和备份相等,不相等就更新session
  50. $session_str_copy = $data['ext_data'];
  51. $cmd = $data['cmd'];
  52. try{
  53. switch($cmd)
  54. {
  55. case GatewayProtocol::CMD_ON_CONNECTION:
  56. Event::onConnect(Context::$client_id);
  57. break;
  58. case GatewayProtocol::CMD_ON_MESSAGE:
  59. Event::onMessage(Context::$client_id, $data['body']);
  60. break;
  61. case GatewayProtocol::CMD_ON_CLOSE:
  62. Event::onClose(Context::$client_id);
  63. break;
  64. }
  65. }
  66. catch(\Exception $e)
  67. {
  68. $msg = 'client_id:'.Context::$client_id."\tclient_ip:".Context::$client_ip."\n".$e->__toString();
  69. $this->log($msg);
  70. }
  71. $session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : '';
  72. if($session_str_copy != $session_str_now)
  73. {
  74. \GatewayWorker\Lib\Gateway::updateSocketSession(Context::$client_id, $session_str_now);
  75. }
  76. Context::clear();
  77. }
  78. public function onClose($connection)
  79. {
  80. unset($this->gatewayConnections[$connection->remoteAddress]);
  81. }
  82. public function checkGatewayConnections()
  83. {
  84. $key = 'GLOBAL_GATEWAY_ADDRESS';
  85. $addresses_list = Store::instance('gateway')->get($key);
  86. if(empty($addresses_list))
  87. {
  88. return;
  89. }
  90. foreach($addresses_list as $addr)
  91. {
  92. if(!isset($this->gatewayConnections[$addr]))
  93. {
  94. $gateway_connection = new AsyncTcpConnection("GatewayProtocol://$addr");
  95. $gateway_connection->remoteAddress = $addr;
  96. $gateway_connection->onConnect = array($this, 'onConnectGateway');
  97. $gateway_connection->onMessage = array($this, 'onGatewayMessage');
  98. $gateway_connection->onClose = array($this, 'onClose');
  99. $gateway_connection->onError = array($this, 'onError');
  100. }
  101. }
  102. }
  103. public function onConnectGateway($connection)
  104. {
  105. $this->gatewayConnections[$connection->remoteAddress] = $connection;
  106. unset($this->badGatewayAddress[$connection->remoteAddress]);
  107. }
  108. public function onError($connection, $error_no, $error_msg)
  109. {
  110. $this->tryToDeleteGatewayAddress($connection->remoteAddress, $error_msg);
  111. }
  112. public function tryToDeleteGatewayAddress($addr, $errstr)
  113. {
  114. $key = 'GLOBAL_GATEWAY_ADDRESS';
  115. if(!isset($this->badGatewayAddress[$addr]))
  116. {
  117. $this->badGatewayAddress[$addr] = 0;
  118. }
  119. // 删除连不上的端口
  120. if($this->badGatewayAddress[$addr]++ > self::MAX_RETRY_COUNT)
  121. {
  122. Lock::get();
  123. $addresses_list = Store::instance('gateway')->get($key);
  124. unset($addresses_list[$addr]);
  125. Store::instance('gateway')->set($key, $addresses_list);
  126. Lock::release();
  127. $this->log("tcp://$addr ".$errstr." del $addr from store", false);
  128. }
  129. }
  130. }