tcp_client.php 4.7 KB

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