wsd_bridge.php 4.0 KB

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