BusinessWorker.php 4.9 KB

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