TestEvent.php 11 KB

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