VM暫存
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

122 satır
3.4KB

  1. <?php
  2. require_once '/home/bigbang/libs/Workerman/Autoloader.php';
  3. use Workerman\Worker;
  4. Worker::$logFile = '/dev/null'; // 不記錄log file
  5. //Worker::$pidFile = '/tmp/run/'.basename(__FILE__).'.pid';
  6. //Worker::$logFile = __DIR__ . '/../mitac2server.log';
  7. // 場站共用設定檔
  8. require_once '/home/bigbang/apps/coworker/station.config.php';
  9. define('APP_NAME', 'mitac'); // application name
  10. define('WORKERMAN_DEBUG', 1);
  11. if (WORKERMAN_DEBUG)
  12. {
  13. ini_set('display_errors', '1');
  14. error_reporting(E_ALL);
  15. set_error_handler('error_handler', E_ALL);
  16. }
  17. ///////////////////////////////
  18. //
  19. // 主程式
  20. //
  21. ///////////////////////////////
  22. // 傳送主機資料
  23. $ch = curl_init();
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
  26. // 建立一個Worker監聽49993埠,不使用任何應用層協定
  27. $tcp_worker = new Worker("tcp://0.0.0.0:49993");
  28. // 啟動N個進程對外提供服務
  29. $tcp_worker->count = 6;
  30. $tcp_worker->onConnect = function($connection)
  31. {
  32. echo APP_NAME . "..New Connection\n";
  33. };
  34. $tcp_worker->onClose = function($connection)
  35. {
  36. echo APP_NAME . "..Connection closed\n";
  37. };
  38. $tcp_worker->onMessage = function($connection, $tcp_in)
  39. {
  40. global $ch;
  41. trigger_error("..tcp_in..". json_encode($tcp_in, true) .'|');
  42. $tcp_in = mb_convert_encoding($tcp_in, 'UTF-8', 'UTF-16LE'); // unicode to utf-8
  43. $explode_tcp_in = explode(',', $tcp_in);
  44. // 處理
  45. if(empty($explode_tcp_in) || empty($explode_tcp_in[0]))
  46. {
  47. trigger_error("..empty..". print_r($explode_tcp_in, true) .'|');
  48. }
  49. else if($explode_tcp_in[0] == 'Mitac')
  50. {
  51. trigger_error("..". $tcp_in .'..'); // Mitac 回傳
  52. }
  53. else if($explode_tcp_in[0] == 'Altob')
  54. {
  55. if($explode_tcp_in[1] == 'DeductResult' && count($explode_tcp_in) == 10)
  56. {
  57. // 回應扣款成功 (MITAC to ALTOB) 目前只有這支
  58. $function_name = 'deduct_result';
  59. $seqno = $explode_tcp_in[2];
  60. $lpr = $explode_tcp_in[3];
  61. $in_time = $explode_tcp_in[4];
  62. $out_time = $explode_tcp_in[5];
  63. $gate_id = $explode_tcp_in[6];
  64. $amt = $explode_tcp_in[7];
  65. $amt_discount = $explode_tcp_in[8];
  66. $amt_real = $explode_tcp_in[9];
  67. // 建立通訊內容
  68. $parms = array(
  69. 'seqno' => $seqno,
  70. 'lpr' => $lpr,
  71. 'in_time' => $in_time,
  72. 'out_time' => $out_time,
  73. 'gate_id' => $gate_id,
  74. 'amt' => $amt,
  75. 'amt_discount' => $amt_discount,
  76. 'amt_real' => $amt_real);
  77. // 加驗証
  78. $parms['ck'] = md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['amt']. 'o'. $parms['amt_discount'] . 'b'. $parms['amt_real'] . $function_name);
  79. curl_setopt($ch, CURLOPT_URL, "http://localhost/mitac_service.html/{$function_name}/");
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms));
  81. $result = curl_exec($ch);
  82. trigger_error(".. curl {$function_name}|{$result} ..".print_r($parms, true));
  83. }
  84. else
  85. {
  86. trigger_error('..unknown cmd..' . print_r($explode_tcp_in, true));
  87. }
  88. }
  89. // 回覆
  90. //$connection->send('OK');
  91. // 斷開
  92. $connection->close();
  93. };
  94. // 執行worker
  95. Worker::runAll();
  96. // 發生錯誤時集中在此處理
  97. function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  98. {
  99. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  100. error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  101. }