tcp_client.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. abstract class tcp_client
  11. {
  12. const time_out = 3600;
  13. const body_header_len = 10;
  14. //协议打包格式,PHP序列化用于PHP进程间通信,json 格式用于PHP与C++进程间通信
  15. const SerializeType = 1;
  16. const JsonType = 2;
  17. private $mSocket;
  18. protected $mBodyType;
  19. protected function __construct()
  20. {
  21. $this->mSocket = false;
  22. }
  23. public function __destruct()
  24. {
  25. $this->fini_socket();
  26. }
  27. protected function request($param)
  28. {
  29. $times = 0;
  30. do
  31. {
  32. if($this->init_socket() == false) {
  33. return false;
  34. }
  35. $ret = $this->write_param($param);
  36. if($ret === false) {
  37. $this->fini_socket();
  38. }
  39. } while($ret == false && ++$times < 2);
  40. if($ret == true)
  41. {
  42. $body = $this->read_body();
  43. if($body != false)
  44. {
  45. if($this->mBodyType == tcp_client::SerializeType) {
  46. return unserialize($body);
  47. } else {
  48. return json_decode($body,true);
  49. }
  50. }
  51. }
  52. else
  53. {
  54. $this->fini_socket();
  55. return false;
  56. }
  57. }
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. private function init_socket()
  60. {
  61. if($this->mSocket != false)
  62. {
  63. if(feof($this->mSocket) == true) {
  64. $this->fini_socket();
  65. } else {
  66. return true;
  67. }
  68. }
  69. $host = $this->remote_addr();
  70. $this->mSocket = stream_socket_client ($host,$err,$errno,30);
  71. if($this->mSocket == false) {
  72. Log::record("connect search server err --- code:{$errno} : {$err}");
  73. return false;
  74. } else {
  75. socket_set_timeout($this->mSocket,self::time_out);
  76. return true;
  77. }
  78. }
  79. private function fini_socket()
  80. {
  81. if($this->mSocket != false) {
  82. Log::record("tcp_client fini_socket",Log::DEBUG);
  83. stream_socket_shutdown($this->mSocket,STREAM_SHUT_RDWR);
  84. fclose($this->mSocket);
  85. $this->mSocket = false;
  86. }
  87. }
  88. private function read_body()
  89. {
  90. $body_len = $this->read(self::body_header_len);
  91. $body_len = intval($body_len);
  92. if($body_len > 0) {
  93. $body = $this->read($body_len);
  94. return $body;
  95. } else {
  96. return false;
  97. }
  98. }
  99. private function read($len)
  100. {
  101. $left = $len;
  102. $data = "";
  103. while ($left > 0)
  104. {
  105. if(feof($this->mSocket) == true) {
  106. return false;
  107. } else {
  108. $tmp = fread($this->mSocket,$left);
  109. $left = $left - strlen($tmp);
  110. $data .= $tmp;
  111. }
  112. }
  113. return $data;
  114. }
  115. private function write($data,$len)
  116. {
  117. while ($len > 0)
  118. {
  119. $ret = fwrite($this->mSocket,$data);
  120. if($ret == false || $ret <= 0) {
  121. return false;
  122. }
  123. else {
  124. $len -= $ret;
  125. $count = strlen($data);
  126. $data = substr($data,$ret,$count - $ret);
  127. }
  128. }
  129. return true;
  130. }
  131. private function write_param($param)
  132. {
  133. if($this->mBodyType == tcp_client::SerializeType) {
  134. $param = serialize($param);
  135. }
  136. else {
  137. $param = json_encode($param);
  138. }
  139. $len = sprintf("%010d",strlen($param));
  140. $data = $len . $param;
  141. return $this->write($data,strlen($data));
  142. }
  143. abstract function remote_addr();
  144. }
  145. class search_client extends tcp_client
  146. {
  147. const GetRelatedWord = 1;
  148. const SearchReasult = 2;
  149. const MatchPrice = 3;
  150. const PromoteGoods = 4;
  151. const ValidateArea = 20;
  152. protected static $stInstance;
  153. public function __construct()
  154. {
  155. parent::__construct();
  156. $this->mBodyType = tcp_client::SerializeType;
  157. }
  158. public function __destruct()
  159. {
  160. parent::__destruct();
  161. }
  162. public static function instance()
  163. {
  164. if(self::$stInstance == null) {
  165. self::$stInstance = new search_client();
  166. }
  167. return self::$stInstance;
  168. }
  169. public function get_words($word)
  170. {
  171. $param = array("type" => self::GetRelatedWord, "keyword" => $word);
  172. return $this->request($param);
  173. }
  174. public function get_result($params)
  175. {
  176. $param = array("type" => self::SearchReasult, "params" => $params);
  177. return $this->request($param);
  178. }
  179. public function match_price($params)
  180. {
  181. $param = array("type" => self::MatchPrice, "params" => $params);
  182. return $this->request($param);
  183. }
  184. public function promote_goods($params)
  185. {
  186. $param = array("type" => self::PromoteGoods, "params" => $params);
  187. return $this->request($param);
  188. }
  189. public function get_area($area_id)
  190. {
  191. $param = array("type" => self::ValidateArea, "params" => array('area_id' => $area_id));
  192. return $this->request($param);
  193. }
  194. public function remote_addr()
  195. {
  196. global $config;
  197. $host = $config['searcher']['host'];
  198. $port = $config['searcher']['port'];
  199. return "{$host}:{$port}";
  200. }
  201. }
  202. class relation_client extends tcp_client
  203. {
  204. const GetRelatedWord = 1;
  205. const SearchReasult = 2;
  206. const MatchPrice = 3;
  207. protected static $stInstance;
  208. public function __construct()
  209. {
  210. parent::__construct();
  211. $this->mBodyType = tcp_client::JsonType;
  212. }
  213. public static function instance()
  214. {
  215. if(self::$stInstance == null) {
  216. self::$stInstance = new relation_client();
  217. }
  218. return self::$stInstance;
  219. }
  220. public function __destruct()
  221. {
  222. parent::__destruct();
  223. }
  224. public function remote_addr()
  225. {
  226. global $config;
  227. $host = $config['relation']['host'];
  228. $port = $config['relation']['port'];
  229. return "{$host}:{$port}";
  230. }
  231. public function add_follow($param)
  232. {
  233. $param = array("act" => 'follow','op' => 'add', "params" => $param);
  234. return $this->request($param);
  235. }
  236. public function del_follow($param)
  237. {
  238. $param = array("act" => 'follow','op' => 'del', "params" => $param);
  239. return $this->request($param);
  240. }
  241. public function fetch_follow($param)
  242. {
  243. $param = array("act" => 'follow','op' => 'fetch', "params" => $param);
  244. return $this->request($param);
  245. }
  246. public function add_special($param)
  247. {
  248. $param = array("act" => 'special','op' => 'add', "params" => $param);
  249. return $this->request($param);
  250. }
  251. public function del_special($param)
  252. {
  253. $param = array("act" => 'special','op' => 'del', "params" => $param);
  254. return $this->request($param);
  255. }
  256. public function fetch_self_special($param)
  257. {
  258. $param = array("act" => 'special','op' => 'fetch_self', "params" => $param);
  259. return $this->request($param);
  260. }
  261. public function fetch_pri_special($param)
  262. {
  263. $param = array("act" => 'special','op' => 'fetch_pri', "params" => $param);
  264. return $this->request($param);
  265. }
  266. public function fetch_pub_special($param)
  267. {
  268. $param = array("act" => 'special','op' => 'fetch_pub', "params" => $param);
  269. return $this->request($param);
  270. }
  271. }