wsd_bridge.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. define('APP_ID', 'bridge-wsd');
  3. define('BASE_ROOT_PATH', str_replace('\\', '/', dirname(__FILE__)));
  4. require_once(BASE_ROOT_PATH . '/global.php');
  5. require_once(BASE_ROOT_PATH . '/fooder.php');
  6. require_once(BASE_ROOT_PATH . '/helper/event_looper.php');
  7. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  8. global $config;
  9. $port = $config['wsd_bradge_port'];
  10. class WSDBridge
  11. {
  12. private $mListenPort;
  13. const KEY = 'ac59f54faf1ffcc1';
  14. public function __construct($port)
  15. {
  16. $this->mListenPort = $port;
  17. }
  18. private function handle_error($level, $message, $file, $line)
  19. {
  20. if($level == E_NOTICE) return;
  21. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  22. $backtrace = debug_backtrace();
  23. foreach ($backtrace as $item) {
  24. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  25. }
  26. Log::record($trace,Log::ERR);
  27. }
  28. public function looper()
  29. {
  30. set_error_handler([$this, 'handle_error']);
  31. $fd = event\util::listen_block("0.0.0.0", $this->mListenPort);
  32. if ($fd === false) {
  33. Log::record("Cannot Listen on port = {$this->mListenPort}", Log::DEBUG);
  34. return false;
  35. }
  36. while (true)
  37. {
  38. Log::record("Waitting another connect....");
  39. if (($client = socket_accept($fd)) !== false)
  40. {
  41. try
  42. {
  43. Log::record("Client {$client} has connected", Log::DEBUG);
  44. socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 8, 'usec' => 0]);
  45. $body = $this->read_order($client);
  46. if (!empty($body)) {
  47. Log::record("req:{$body}", Log::DEBUG);
  48. $resp = $this->relay_request($body);
  49. Log::record("resp={$resp}",Log::ERR);
  50. $resp = mb_convert_encoding($resp, 'GBK', 'UTF-8');
  51. socket_write($client, $resp);
  52. Log::record("resp:{$resp}", Log::DEBUG);
  53. } else {
  54. Log::record("不合法的包", Log::DEBUG);
  55. }
  56. }
  57. catch (Exception $ex) {
  58. Log::record(__FUNCTION__ . " " . $ex->getMessage(),Log::ERR);
  59. }
  60. socket_close($client);
  61. }
  62. }
  63. socket_close($fd);
  64. }
  65. private function relay_request($body)
  66. {
  67. $url = BASE_SITE_URL . "/mobile/bridge/bridge_shr.php";
  68. $headers = ['Content-Type: application/json'];
  69. $resp = http_post_data($url, $body, $headers);
  70. if ($resp === false) {
  71. Log::record("Net error.", Log::ERR);
  72. return $this->err_body($body);
  73. } else {
  74. return $resp;
  75. }
  76. }
  77. private function err_body($body)
  78. {
  79. $params = json_decode($body,true);
  80. //交易结果 0:未处理 1:充值成功 2:充值结果不确定 3:充值失败
  81. $retCode = 3;
  82. $retDetail = "服务维护中.....";
  83. $result = [
  84. 'action' => 'CZ',
  85. 'chargeId' => $params['chargeId'],
  86. 'retCode' => $retCode,
  87. 'retDetail' => $retDetail,
  88. 'retRsn' => $params['retRsn']];
  89. $body = "{$params['chargeId']}{$retCode}{$params['retRsn']}" . WSDBridge::KEY;
  90. $sign = md5($body);
  91. $result['sign'] = $sign;
  92. return json_encode($result);
  93. }
  94. private function read_order($client)
  95. {
  96. $content = '';
  97. $buf = socket_read($client, 1024);
  98. if ($buf === false) {
  99. $error = socket_strerror(socket_last_error($client));
  100. Log::record("read_order err:{$error}", Log::ERR);
  101. return false;
  102. } else {
  103. $content .= $buf;
  104. }
  105. $content = mb_convert_encoding($content, 'UTF-8', 'GBK');
  106. if ($this->isbody($content)) {
  107. return $content;
  108. } else {
  109. Log::record("err body = {$content}", Log::DEBUG);
  110. return false;
  111. }
  112. }
  113. private function isbody($content)
  114. {
  115. $ret = json_decode($content, true);
  116. return !empty($ret);
  117. }
  118. }
  119. $brider = new WSDBridge($port);
  120. $brider->looper();