tcp_client.php 4.5 KB

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