DomainServerService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. require_once dirname(dirname(__FILE__)).'/util/HttpClient.php';
  3. require_once dirname(dirname(__FILE__)).'/util/LoggerHelper.php';
  4. require_once dirname(dirname(__FILE__)).'/util/Block.php';
  5. require_once dirname(dirname(__FILE__)).'/util/FileCache.php';
  6. require_once dirname(dirname(__FILE__)).'/util/ConfigHelper.php';
  7. require_once dirname(dirname(__FILE__)).'/model/DomainInfo.php';
  8. require_once dirname(dirname(__FILE__)).'/constant/ServiceName.php';
  9. /**
  10. * 后羿系统服务类
  11. */
  12. class DomainServerService{
  13. private $domain_server = array();
  14. private $expireTime = 300;//秒
  15. private $expireTimeHy = 86400;//秒,24小时
  16. private $hy_url = "http://hy.9game.cn:8080/v2/m";
  17. function get_cache_id($dn) {
  18. // maintain list of caches here
  19. $id=array(
  20. ServiceName::$DOMAINSERVER_REQ_DN => 6559,
  21. ServiceName::$DOMAINSERVER_GAMEDATA_DN => 6560,
  22. ServiceName::$DOMAINSERVER_REALNAME_DN => 6561,
  23. ServiceName::$ACCOUNT_ID => 6562
  24. );
  25. return $id[$dn];
  26. }
  27. public function getHyInfoByCache($accountId) {
  28. if (extension_loaded('shmop')) {
  29. $saveCache = false;
  30. $memory = new Block(self::get_cache_id($accountId));
  31. $hyInfoStr = $memory->read();
  32. if (empty($hyInfoStr)) {
  33. $saveCache = true;
  34. } else {
  35. $hyInfoArray = json_decode($hyInfoStr,true);
  36. if(time() - $hyInfoArray['ctime'] > $this->expireTimeHy){
  37. $saveCache = true;
  38. } else {
  39. LoggerHelper::info("缓存中的后羿系统的IP缓存有效,从缓存直接获取的信息为:".$hyInfoStr);
  40. return $hyInfoArray;
  41. }
  42. }
  43. if ($saveCache) {
  44. $hyInfo = HttpClient::quickPost($this->hy_url, "account_id=".$accountId);
  45. if ($hyInfo) {
  46. $hyInfoArray['urls'] = $this->getHyUrlList(json_decode($hyInfo,true));
  47. if (! empty($hyInfoArray['urls'])) {
  48. $hyInfoArray['ctime'] = time();
  49. //新数据,存储到共享区
  50. $memory->write(json_encode($hyInfoArray));
  51. LoggerHelper::info("缓存后羿系统的IP成功,数据:".json_encode($hyInfoArray));
  52. return $hyInfoArray;
  53. }
  54. }
  55. }
  56. } else if ($this->fileCacheOpen()) {
  57. $fileCache = new FileCache($this->getFileCachePath());
  58. $hyInfoStr = $fileCache->get(self::get_cache_id($accountId));
  59. if (empty($hyInfoStr)) {
  60. $hyInfo = HttpClient::quickPost($this->hy_url, "account_id=".$accountId);
  61. if ($hyInfo) {
  62. $hyInfoArray['urls'] = $this->getHyUrlList(json_decode($hyInfo,true));
  63. if (! empty($hyInfoArray['urls'])) {
  64. $hyInfoArray['ctime'] = time();
  65. $fileCache->set(self::get_cache_id($accountId), json_encode($hyInfoArray), $this->expireTimeHy);
  66. return $hyInfoArray;
  67. }
  68. }
  69. } else {
  70. $hyInfoArray = json_decode($hyInfoStr,true);
  71. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存有效,从缓存直接获取的信息为:".$hyInfoStr);
  72. return $hyInfoArray;
  73. }
  74. }
  75. return false;
  76. }
  77. private function getHyInfoByServer($accountId){
  78. $hyInfoArray = $this->getHyInfoByCache($accountId);
  79. if (! $hyInfoArray) {
  80. $hyInfo = HttpClient::quickPost($this->hy_url, "account_id=".$accountId);
  81. if ($hyInfo) {
  82. $hyInfoArray['urls'] = $this->getHyUrlList(json_decode($hyInfo,true));
  83. if (! empty($hyInfoArray['urls'])) {
  84. $hyInfoArray['ctime'] = time();
  85. }
  86. }
  87. }
  88. return $hyInfoArray;
  89. }
  90. public function getDomainByServer($accountId, $dn, $domainServerReqBody, $refCache = false){
  91. $domainInfo = array();
  92. if (extension_loaded('shmop')) {
  93. $memory = new Block(self::get_cache_id($dn));
  94. $domainStr = $memory->read();
  95. if (empty($domainStr)) {
  96. $saveCache = true;
  97. } else {
  98. $domainInfo = json_decode($domainStr,true);
  99. if (time() - $domainInfo['ctime'] > $this->expireTime) {
  100. $saveCache = true;
  101. } else {
  102. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存有效,从缓存直接获取的信息为:".$domainStr);
  103. return $domainInfo;
  104. }
  105. }
  106. if ($saveCache || $refCache) {
  107. $domainInfo = self::getDomainInfoByMutiThread($accountId, $domainServerReqBody);
  108. if (!empty($domainInfo)) {
  109. $memory->write(json_encode($domainInfo));
  110. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存已过期,再次请求智能域名解析系统。");
  111. return $domainInfo;
  112. }
  113. }
  114. } else if ($this->fileCacheOpen()) {
  115. $fileCache = new FileCache($this->getFileCachePath());
  116. $domainStr = $fileCache->get(self::get_cache_id($dn));
  117. if (empty($domainStr) || $refCache) {
  118. $domainInfo = self::getDomainInfoByMutiThread($accountId, $domainServerReqBody);
  119. if (!empty($domainInfo)) {
  120. $fileCache->set(self::get_cache_id($dn), json_encode($domainInfo), $this->expireTime);
  121. LoggerHelper::info("从后羿服务器获取域名IP成功,获取的信息为:".json_encode($domainInfo));
  122. return $domainInfo;
  123. }
  124. } else {
  125. $domainInfo = json_decode($domainStr,true);
  126. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存有效,从缓存直接获取的信息为:".$domainStr);
  127. return $domainInfo;
  128. }
  129. } else {
  130. LoggerHelper::info("未开启内存读取shmop模块且缓存目录不可以写,直接请求智能域名解析系统。");
  131. $domainInfo = self::getDomainInfoByMutiThread($accountId, $domainServerReqBody);
  132. return $domainInfo;
  133. }
  134. }
  135. public function getDomainByCache($dn){
  136. $domainInfo = array();
  137. if (extension_loaded('shmop')) {
  138. $memory = new Block(self::get_cache_id($dn));
  139. $domainStr = $memory->read();
  140. if (empty($domainStr)) {
  141. LoggerHelper::info("缓存中不存在智能域名解析系统的IP缓存,直接请求域名.");
  142. return false;
  143. } else {
  144. $domainInfo = json_decode($domainStr,true);
  145. if (time() - $domainInfo['ctime'] > $this->expireTime) {
  146. //已过期,删除
  147. $memory->delete();
  148. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存已过期,直接请求域名。");
  149. return false;
  150. } else {
  151. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存有效,从缓存直接获取的信息为:".$domainStr);
  152. return $domainInfo;
  153. }
  154. }
  155. } else if ($this->fileCacheOpen()) {
  156. $fileCache = new FileCache($this->getFileCachePath());
  157. $domainStr = $fileCache->get(self::get_cache_id($dn));
  158. if (empty($domainStr)) {
  159. LoggerHelper::info("缓存中不存在智能域名解析系统的IP缓存,直接请求域名.");
  160. return false;
  161. } else {
  162. $domainInfo = json_decode($domainStr,true);
  163. LoggerHelper::info("缓存中的智能域名解析系统的IP缓存有效,从缓存直接获取的信息为:".$domainStr);
  164. return $domainInfo;
  165. }
  166. } else {
  167. LoggerHelper::info("未开启内存读取shmop模块且缓存目录不可以写,无缓存设置。");
  168. return false;
  169. }
  170. }
  171. private function getDomainInfoByMutiThread($accountId, $domainServerReqBody){
  172. $hyUrls = $this->getHyInfoByServer($accountId);
  173. if (!$hyUrls || !isset($hyUrls['urls'])) {
  174. return false;
  175. }
  176. $resp_data = $this->curlMuti($hyUrls['urls'], $domainServerReqBody);
  177. $mutiServer = json_decode($resp_data,true);
  178. if (empty($mutiServer) || !isset($mutiServer['res'])) {
  179. LoggerHelper::info("获取到智能域名解析服务端接口返回的IP地址为空。");
  180. return false;
  181. }
  182. $domainInfo = array();
  183. $domainInfo['domain'] = isset($mutiServer['res'][0]['dn']) ? $mutiServer['res'][0]['dn'] : "";
  184. $domainInfo['ipAddress'] = isset($mutiServer['res'][0]['ips']) ? $mutiServer['res'][0]['ips'] : "";
  185. $domainInfo['ctime'] = time();
  186. return $domainInfo;
  187. }
  188. private function curlMuti($urls, $domainServerReqBody){
  189. $mh = curl_multi_init();
  190. $conn = array();
  191. foreach ($urls as $i => $url) {
  192. $conn[$i] = curl_init($url);
  193. curl_setopt($conn[$i], CURLOPT_USERAGENT, "UCSDK");
  194. curl_setopt($conn[$i], CURLOPT_POST, true);
  195. curl_setopt($conn[$i], CURLOPT_HEADER, false);
  196. curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, true);
  197. curl_setopt($conn[$i], CURLOPT_POSTFIELDS, $domainServerReqBody);
  198. curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,3);
  199. curl_setopt($conn[$i], CURLOPT_TIMEOUT, 3);//设置超时时间
  200. curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,true); // 设置不将爬取代码写到浏览器,而是转化为字符串
  201. curl_multi_add_handle ($mh,$conn[$i]);
  202. }
  203. do {
  204. curl_multi_exec($mh,$active);
  205. } while ($active);
  206. $resp_data = "";
  207. foreach ($urls as $i => $url) {
  208. $resp_data = curl_multi_getcontent($conn[$i]); // 获得返回值
  209. if($resp_data != null){
  210. break; //终止循环
  211. }
  212. }
  213. foreach ($urls as $i => $url) {
  214. curl_multi_remove_handle($mh,$conn[$i]);
  215. curl_close($conn[$i]);
  216. }
  217. curl_multi_close($mh);
  218. return $resp_data;
  219. }
  220. /**
  221. * 获取毫秒级的时间参数
  222. *
  223. */
  224. private function getMillisecond() {
  225. $time = explode ( " ", microtime () );
  226. $time = $time [1] . ($time [0] * 1000);
  227. $time2 = explode ( ".", $time );
  228. $time = $time2 [0];
  229. return $time;
  230. }
  231. private function getHyUrlList($hyInfo) {
  232. if (! is_array($hyInfo)) {
  233. return false;
  234. }
  235. if (!isset($hyInfo['res']) || !isset($hyInfo['res']['network'])) {
  236. return false;
  237. }
  238. $network = $hyInfo['res']['network'];
  239. if (!is_array($network)) {
  240. return false;
  241. }
  242. $hyUrls = array();
  243. foreach ($network as $val) {
  244. if (isset($val['item']) && is_array($val['item'])) {
  245. foreach ($val['item'] as $value) {
  246. $hyUrls[] = $value['protocol']."://".$value['ip'].":".$value['port'].ServiceName::$SERVER_PATH;
  247. }
  248. }
  249. }
  250. return $hyUrls;
  251. }
  252. public function cacheOpen() {
  253. if (extension_loaded('shmop') || $this->fileCacheOpen()) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. private function fileCacheOpen() {
  259. if (is_writable($this->getFileCachePath())) {
  260. return true;
  261. }
  262. return false;
  263. }
  264. private function getFileCachePath() {
  265. return ConfigHelper::getStrVal("sdkserver.file.cache.path");
  266. }
  267. }