TestEvent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/7/21
  6. * Time: 下午6:06
  7. */
  8. define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
  9. require_once(BASE_ROOT_PATH . '/fooder.php');
  10. require_once(BASE_ROOT_PATH . '/helper/event_looper.php');
  11. require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
  12. require_once(BASE_ROOT_PATH . '/helper/room/factory_processor.php');
  13. require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
  14. require_once(BASE_ROOT_PATH . '/helper/room/room_info.php');
  15. require_once(BASE_ROOT_PATH . '/helper/room/msg_builder.php');
  16. require_once(BASE_ROOT_PATH . '/helper/room/base_room.php');
  17. require_once(BASE_ROOT_PATH . '/helper/room/chatwo.php');
  18. require_once(BASE_ROOT_PATH . '/helper/room/chat_room.php');
  19. require_once(BASE_ROOT_PATH . '/helper/room/bargain_room.php');
  20. require_once(BASE_ROOT_PATH . '/helper/room/shake_room.php');
  21. require_once(BASE_ROOT_PATH . '/helper/room/factory.php');
  22. require_once(BASE_ROOT_PATH . '/helper/room/room_client.php');
  23. require_once(BASE_ROOT_PATH . '/helper/room/factory_client.php');
  24. class TestServer extends PHPUnit_Framework_TestCase
  25. {
  26. const admin_member_id = 36429;
  27. public static function setUpBeforeClass()
  28. {
  29. Base::run_util();
  30. }
  31. public function testFactory()
  32. {
  33. room\factory_client::instance()->access();
  34. }
  35. public function testAsyncALl()
  36. {
  37. global $config;
  38. $host = $config['room_factory']['host'];
  39. $port = $config['room_factory']['port'];
  40. $fac_socket = $this->open_socket($host,$port);
  41. $host = $config['room_srv']['host'];
  42. $port = $config['room_srv']['port'];
  43. $room_socket = $this->open_socket($host,$port);
  44. $processor = new empty_processor();
  45. $looper = new event\event_looper();
  46. $looper->init($processor);
  47. $looper->add_listen($fac_socket);
  48. $looper->add_listen($room_socket);
  49. $looper->run_loop();
  50. }
  51. public function testAsyncFactroy()
  52. {
  53. global $config;
  54. $host = $config['room_factory']['host'];
  55. $port = $config['room_factory']['port'];
  56. $socket = $this->open_socket($host,$port);
  57. $processor = new empty_processor();
  58. $looper = new event\buffer_looper();
  59. $looper->init($processor);
  60. $looper->add_listen($socket);
  61. $looper->run_loop();
  62. }
  63. public function testSyncFactroy()
  64. {
  65. global $config;
  66. $host = $config['room_factory']['host'];
  67. $port = $config['room_factory']['port'];
  68. $socket = $this->open_socket($host,$port,false);
  69. while (true)
  70. {
  71. $stream = socket_accept($socket);
  72. while (true)
  73. {
  74. $read = @socket_read($stream,1024);
  75. if(strlen($read) == 0)
  76. break;
  77. else {
  78. Log::record("read={$read}",Log::DEBUG);
  79. }
  80. }
  81. }
  82. }
  83. public function testAsyncOrgFactroy()
  84. {
  85. global $config;
  86. $host = $config['room_factory']['host'];
  87. $port = $config['room_factory']['port'];
  88. $contents = [];
  89. $socket = $this->open_socket($host,$port,false);
  90. while (true)
  91. {
  92. $stream = socket_accept($socket);
  93. $fd = intval($stream);
  94. $contents[$fd] = '';
  95. socket_set_nonblock($stream);
  96. while (true)
  97. {
  98. $rfds = [$stream];
  99. $wfds = [];
  100. $efds = [];
  101. if(socket_select($rfds,$wfds,$efds,0) < 1) continue;
  102. if (in_array($stream, $rfds))
  103. {
  104. $rfd = intval($stream);
  105. $read = @socket_read($stream, 1024);
  106. if($read === false) {
  107. continue;
  108. }
  109. else
  110. {
  111. if (strlen($read) == 0)
  112. break;
  113. else {
  114. $contents[$rfd] .= $read;
  115. Log::record("read={$read}", Log::DEBUG);
  116. $ret = $this->proc($contents[$fd]);
  117. if($ret === false) {
  118. $x = 0;
  119. }
  120. else {
  121. $contents[$rfd] = $ret;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. public function testAsyncRoom()
  130. {
  131. global $config;
  132. $host = $config['room_srv']['host'];
  133. $port = $config['room_srv']['port'];
  134. $socket = $this->open_socket($host,$port);
  135. $processor = new empty_processor();
  136. $looper = new event_looper();
  137. $looper->init($processor);
  138. $looper->add_listen($socket);
  139. $looper->run_loop();
  140. }
  141. private function open_socket($host,$port,$nonbolck = true)
  142. {
  143. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  144. if(!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
  145. echo "socket_set_option 地址重用失败.\n";
  146. return false;
  147. }
  148. if($nonbolck && !socket_set_nonblock($sock)) {
  149. $err = socket_last_error();
  150. Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
  151. return false;
  152. }
  153. if(!socket_bind($sock, $host, $port)) {
  154. echo "无法绑定socket {$host} : {$port},请退出之前进程.\n";
  155. return false;
  156. }
  157. if(!socket_listen($sock)) {
  158. echo "无法监听socket,请退出之前进程.\n";
  159. return false;
  160. }
  161. return $sock;
  162. }
  163. const body_header_len = 10;
  164. public function testProc()
  165. {
  166. $content = $this->get_content();
  167. $this->proc($content);
  168. }
  169. private function proc($content)
  170. {
  171. $start = 0;
  172. $left = strlen($content);
  173. $i = 0;
  174. while($left > self::body_header_len)
  175. {
  176. $header = substr($content,$start,self::body_header_len);
  177. if(!is_numeric($header)) {
  178. return false;
  179. }
  180. $body_len = intval($header);
  181. if($body_len == 0) { //这是一个心跳包
  182. $start += self::body_header_len;
  183. $left -= self::body_header_len;
  184. $i++;
  185. }
  186. elseif($body_len < 0) {
  187. return false;
  188. }
  189. else
  190. {
  191. if($left >= self::body_header_len + $body_len)
  192. {
  193. $i++;
  194. $body = substr($content,$start + self::body_header_len,$body_len);
  195. $start += self::body_header_len + $body_len;
  196. $left -= self::body_header_len + $body_len;
  197. }
  198. else {
  199. break;
  200. }
  201. }
  202. }
  203. if($start > 0)
  204. {
  205. $str = substr($content,$start);
  206. if($str === false) {
  207. return '';
  208. }
  209. else {
  210. return $str;
  211. }
  212. }
  213. return $content;
  214. }
  215. private function get_content()
  216. {
  217. return '0000000089{
  218. "act" : "access",
  219. "msgtype" : "message",
  220. "op" : "build",
  221. "room" : 37,
  222. "set" : 88
  223. }0000000089{
  224. "act" : "access",
  225. "msgtype" : "message",
  226. "op" : "build",
  227. "room" : 37,
  228. "set" : 89
  229. }0000000089{
  230. "act" : "access",
  231. "msgtype" : "message",
  232. "op" : "build",
  233. "room" : 37,
  234. "set" : 90
  235. }0000000089{
  236. "act" : "access",
  237. "msgtype" : "message",
  238. "op" : "build",
  239. "room" : 37,
  240. "set" : 91
  241. }0000000089{
  242. "act" : "access",
  243. "msgtype" : "message",
  244. "op" : "build",
  245. "room" : 37,
  246. "set" : 92
  247. }0000000089{
  248. "act" : "access",
  249. "msgtype" : "message",
  250. "op" : "build",
  251. "room" : 37,
  252. "set" : 93
  253. }0000000089{
  254. "act" : "access",
  255. "msgtype" : "message",
  256. "op" : "build",
  257. "room" : 37,
  258. "set" : 94
  259. }0000000089{
  260. "act" : "access",
  261. "msgtype" : "message",
  262. "op" : "build",
  263. "room" : 37,
  264. "set" : 95
  265. }0000000089{
  266. "act" : "access",
  267. "msgtype" : "message",
  268. "op" : "build",
  269. "room" : 37,
  270. "set" : 96
  271. }0000000089{
  272. "act" : "access",
  273. "msgtype" : "message",
  274. "op" : "build",
  275. "room" : 37,
  276. "set" : 97
  277. }0000000089{
  278. "act" : "access",
  279. "msgtype" : "message",
  280. "op" : "build",
  281. "room" : 37,
  282. "set" : 98
  283. }0000000089{
  284. "act" : "access",
  285. "msgtype" : "message",
  286. "op" : "build",
  287. "room" : 37,
  288. "set" : 99
  289. }0000000090{
  290. "act" : "access",
  291. "msgtype" : "message",
  292. "op" : "build",
  293. "room" : 37,
  294. "set" : 100
  295. }0000000090{
  296. "act" : "access",
  297. "msgtype" : "message",
  298. "op" : "build",
  299. "room" : 37,
  300. "set" : 101
  301. }0000000090{
  302. "act" : "access",
  303. "msgtype" : "message",
  304. "op" : "build",
  305. "room" : 37,
  306. "set" : 102
  307. }0000000090{
  308. "act" : "access",
  309. "msgtype" : "message",
  310. "op" : "build",
  311. "room" : 37,
  312. "set" : 103
  313. }0000000090{
  314. "act" : "access",
  315. "msgtype" : "message",
  316. "op" : "build",
  317. "room" : 37,
  318. "set" : 104
  319. }0000000090{
  320. "act" : "access",
  321. "msgtype" : "message",
  322. "op" : "build",
  323. "room" : 37,
  324. "set" : 105
  325. }0000000090{
  326. "act" : "access",
  327. "msgtype" : "message",
  328. "op" : "build",
  329. "room" : 37,
  330. "set" : 106
  331. }0000000090{
  332. "act" : "access",
  333. "msgtype" : "message",
  334. "op" : "build",
  335. "room" : 37,
  336. "set" : 107
  337. }0000000090{
  338. "act" : "access",
  339. "msgtype" : "message",
  340. "op" : "build",
  341. "room" : 37,
  342. "set" : 108
  343. }0000000090{
  344. "act" : "access",
  345. "msgtype" : "message",
  346. "op" : "build",
  347. "room" : 37,
  348. "set" : 109
  349. }0000000090{
  350. "act" : "access",
  351. "msgtype" : "message",
  352. "op" : "build",
  353. "room" : 37,
  354. "set" : 110
  355. }"room" : 37,
  356. "set" : 69
  357. }0000000089{
  358. "act" : "access",
  359. "msgtype" : "message",
  360. "op" : "build",
  361. "room" : 37,
  362. "set" : 70
  363. }0000000089{
  364. "act" : "access",
  365. "msgtype" : "message",
  366. "op" : "build",
  367. "room" : 37,
  368. "set" : 71
  369. }0000000089{
  370. "act" : "access",
  371. "msgtype" : "message",
  372. "op" : "build",
  373. "room" : 37,
  374. "set" : 72
  375. }0000000089{
  376. "act" : "access",
  377. "msgtype" : "message",
  378. "op" : "build",
  379. "room" : 37,
  380. "set" : 73
  381. }0000000089{
  382. "act" : "access",
  383. "msgtype" : "message",
  384. "op" : "build",
  385. "room" : 37,
  386. "set" : 74
  387. }0000000089{
  388. "act" : "access",
  389. "msgtype" : "message",
  390. "op" : "build",
  391. "room" : 37,
  392. "set" : 75
  393. }0000000089{
  394. "act" : "access",
  395. "msgtype" : "message",
  396. "op" : "build",
  397. "room" : 37,
  398. "set" : 76
  399. }0000000089{
  400. "act" : "access",
  401. "msgtype" : "message",
  402. "op" : "build",
  403. "room" : 37,
  404. "set" : 77
  405. }0000000089{
  406. "act" : "access",
  407. "msgtype" : "message",
  408. "op" : "build",
  409. "room" : 37,
  410. "set" : 78
  411. }0000000089{
  412. "act" : "access",
  413. "msgtype" : "message",
  414. "op" : "build",
  415. "room" : 37,
  416. "set" : 79
  417. }0000000089{
  418. "act" : "access",
  419. "msgtype" : "message",
  420. "op" : "build",
  421. "room" : 37,
  422. "set" : 80
  423. }0000000089{
  424. "act" : "access",
  425. "msgtype" : "message",
  426. "op" : "build",
  427. "room" : 37,
  428. "set" : 81
  429. }0000000089{
  430. "act" : "access",
  431. "msgtype" : "message",
  432. "op" : "build",
  433. "room" : 37,
  434. "set" : 82
  435. }0000000089{
  436. "act" : "access",
  437. "msgtype" : "message",
  438. "op" : "build",
  439. "room" : 37,
  440. "set" : 83
  441. }0000000089{
  442. "act" : "access",
  443. "msgtype" : "message",
  444. "op" : "build",
  445. "room" : 37,
  446. "set" : 84
  447. }0000000089{
  448. "act" : "access",
  449. "msgtype" : "message",
  450. "op" : "build",
  451. "room" : 37,
  452. "set" : 85
  453. }0000000089{
  454. "act" : "access",
  455. "msgtype" : "message",
  456. "op" : "build",
  457. "room" : 37,
  458. "set" : 86
  459. }0000000089{
  460. "act" : "access",
  461. "msgtype" : "message",
  462. "op" : "build",
  463. "room" : 37,
  464. "set" : 87
  465. }0000000089{
  466. "act" : "access",
  467. "msgt';
  468. }
  469. }