tcp_client.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/15
  6. * Time: 下午3:52
  7. */
  8. namespace search;
  9. use Log;
  10. class tcp_client
  11. {
  12. const GetRelatedWord = 1;
  13. const SearchReasult = 2;
  14. const MatchPrice = 3;
  15. const PromoteGoods = 4;
  16. const ValidateArea = 20;
  17. const time_out = 3600;
  18. const body_header_len = 10;
  19. private static $stInstance;
  20. private $mSocket;
  21. private function __construct()
  22. {
  23. $this->mSocket = false;
  24. }
  25. public function __destruct()
  26. {
  27. $this->fini_socket();
  28. }
  29. public static function instance()
  30. {
  31. if(self::$stInstance == null) {
  32. self::$stInstance = new tcp_client();
  33. }
  34. return self::$stInstance;
  35. }
  36. public function get_words($word)
  37. {
  38. $param = array("type" => self::GetRelatedWord, "keyword" => $word);
  39. return $this->request($param);
  40. }
  41. public function get_result($params)
  42. {
  43. $param = array("type" => self::SearchReasult, "params" => $params);
  44. return $this->request($param);
  45. }
  46. public function match_price($params)
  47. {
  48. $param = array("type" => self::MatchPrice, "params" => $params);
  49. return $this->request($param);
  50. }
  51. public function promote_goods($params)
  52. {
  53. $param = array("type" => self::PromoteGoods, "params" => $params);
  54. return $this->request($param);
  55. }
  56. public function get_area($area_id)
  57. {
  58. $param = array("type" => self::ValidateArea, "params" => array('area_id' => $area_id));
  59. return $this->request($param);
  60. }
  61. private function request($param)
  62. {
  63. $times = 0;
  64. do
  65. {
  66. if($this->init_socket() == false) {
  67. return false;
  68. }
  69. $ret = $this->write_param($param);
  70. if($ret === false) {
  71. $this->fini_socket();
  72. }
  73. } while($ret == false && ++$times < 2);
  74. if($ret == true)
  75. {
  76. $body = $this->read_body();
  77. if($body != false) {
  78. return unserialize($body);
  79. }
  80. }
  81. else
  82. {
  83. $this->fini_socket();
  84. return false;
  85. }
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. private function init_socket()
  89. {
  90. if($this->mSocket != false)
  91. {
  92. if(feof($this->mSocket) == true) {
  93. $this->fini_socket();
  94. } else {
  95. return true;
  96. }
  97. }
  98. $host = $this->remote_addr();
  99. $this->mSocket = stream_socket_client ($host,$err,$errno,30);
  100. if($this->mSocket == false) {
  101. Log::record("connect search server err --- code:{$errno} : {$err}");
  102. return false;
  103. } else {
  104. socket_set_timeout($this->mSocket,self::time_out);
  105. return true;
  106. }
  107. }
  108. private function fini_socket()
  109. {
  110. if($this->mSocket != false) {
  111. Log::record("tcp_client fini_socket",Log::DEBUG);
  112. stream_socket_shutdown($this->mSocket,STREAM_SHUT_RDWR);
  113. fclose($this->mSocket);
  114. $this->mSocket = false;
  115. }
  116. }
  117. private function read_body()
  118. {
  119. $body_len = $this->read(self::body_header_len);
  120. $body_len = intval($body_len);
  121. if($body_len > 0) {
  122. $body = $this->read($body_len);
  123. return $body;
  124. } else {
  125. return false;
  126. }
  127. }
  128. private function read($len)
  129. {
  130. $left = $len;
  131. $data = "";
  132. while ($left > 0)
  133. {
  134. if(feof($this->mSocket) == true) {
  135. return false;
  136. } else {
  137. $tmp = fread($this->mSocket,$left);
  138. $left = $left - strlen($tmp);
  139. $data .= $tmp;
  140. }
  141. }
  142. return $data;
  143. }
  144. private function write($data,$len)
  145. {
  146. while ($len > 0)
  147. {
  148. $ret = fwrite($this->mSocket,$data);
  149. if($ret == false || $ret <= 0) {
  150. return false;
  151. }
  152. else {
  153. $len -= $ret;
  154. $count = strlen($data);
  155. $data = substr($data,$ret,$count - $ret);
  156. }
  157. }
  158. return true;
  159. }
  160. private function write_param($param)
  161. {
  162. $param = serialize($param);
  163. $len = sprintf("%010d",strlen($param));
  164. $data = $len . $param;
  165. return $this->write($data,strlen($data));
  166. }
  167. private function remote_addr()
  168. {
  169. global $config;
  170. $host = $config['searcher']['host'];
  171. $port = $config['searcher']['port'];
  172. return "{$host}:{$port}";
  173. }
  174. }