tcp_client.php 4.2 KB

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