wsd_bridge.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Log::record("Client {$client} has connected", Log::DEBUG);
  32. socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 8, 'usec' => 0]);
  33. $body = $this->read_order($client);
  34. if($body !== false && !empty($body)) {
  35. Log::record("req:{$body}",Log::DEBUG);
  36. $response = $this->relay_request($body);
  37. socket_write($client,$response);
  38. Log::record("resp:{$response}",Log::DEBUG);
  39. }
  40. socket_close($client);
  41. }
  42. }
  43. }
  44. private function relay_request($body)
  45. {
  46. $url = BASE_SITE_URL . "/mobile/bridge_shr.php";
  47. $headers = ['Content-Type: application/json'];
  48. $resp = http_post_data($url,$body,$headers);
  49. if($resp === false) {
  50. Log::record("Net error.",Log::ERR);
  51. return $this->err_body($body);
  52. }
  53. else {
  54. return $resp;
  55. }
  56. }
  57. private function err_body($body)
  58. {
  59. $params = json_decode($body);
  60. //交易结果 0:未处理 1:充值成功 2:充值结果不确定 3:充值失败
  61. $retCode = 3;
  62. $retDetail = "服务维护中.....";
  63. $result = [
  64. 'action' => 'CZ',
  65. 'chargeId' => $params['chargeId'],
  66. 'retCode' => $retCode,
  67. 'retDetail' => $retDetail,
  68. 'retRsn' => $params['retRsn'] ];
  69. $body = "{$params['chargeId']}{$retCode}{$params['retRsn']}" . WSDBridge::KEY;
  70. $sign = md5($body);
  71. $result['sign'] = $sign;
  72. return json_encode($result);
  73. }
  74. private function read_order($client)
  75. {
  76. $content = '';
  77. while (true)
  78. {
  79. $buf = socket_read($client, 1024);
  80. if ($buf === false) {
  81. $error = socket_strerror(socket_last_error($client));
  82. Log::record("read_order err:{$error}", Log::ERR);
  83. return false;
  84. } else {
  85. $content .= $buf;
  86. }
  87. if ($this->isbody($content)) {
  88. return $content;
  89. }
  90. if(strlen($content) > 1024) {
  91. return false;
  92. }
  93. }
  94. }
  95. private function isbody($content)
  96. {
  97. $ret = json_decode($content, true);
  98. return ($ret !== false);
  99. }
  100. }
  101. $brider = new WSDBridge($port);
  102. $brider->looper();