tcp_client.php 4.1 KB

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