factory_processor.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午12:07
  7. */
  8. namespace room;
  9. use event\IProcessor;
  10. use process_looper;
  11. use errcode;
  12. use uniquer;
  13. use Log;
  14. use scope_trace;
  15. class factory_processor implements IProcessor
  16. {
  17. const room_connection = "room_connection";
  18. private $mFactory;
  19. private $mRoomPos;
  20. private $mAccUniquer;
  21. private $mBufidRooms;
  22. public function __construct()
  23. {
  24. $this->mFactory = new factory();
  25. $this->mRoomPos = 0;
  26. $this->mAccUniquer = new uniquer();
  27. $this->mBufidRooms = [];
  28. }
  29. public function onStart()
  30. {
  31. global $config;
  32. $room_addrs = $config['room_factory']['rooms_addr'];
  33. foreach ($room_addrs as $addr) {
  34. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  35. }
  36. }
  37. public function onConnected($bufid,$stream,$host,$port,$args)
  38. {
  39. if($args == self::room_connection) {
  40. Log::record("room srv bufid={$bufid}",Log::DEBUG);
  41. $client = new room_client($host,$port,$stream,false);
  42. $this->mBufidRooms[$bufid] = $client;
  43. $this->block($bufid);
  44. $client->init_rooms($bufid);
  45. $this->unblock($bufid);
  46. }
  47. }
  48. public function onClose($bufid)
  49. {
  50. //if access connection
  51. if($this->mAccUniquer->remove_value($bufid)) return;
  52. //if room connection
  53. if(array_key_exists($bufid,$this->mBufidRooms)) {
  54. $client = $this->mBufidRooms[$bufid];
  55. $addr = $client->host_port();
  56. unset($this->mBufidRooms[$bufid]);
  57. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  58. }
  59. }
  60. private function block($bufid) {
  61. process_looper::instance()->block($bufid);
  62. }
  63. private function unblock($bufid) {
  64. process_looper::instance()->unblock($bufid);
  65. }
  66. public function onRequest($bufid, $body)
  67. {
  68. $trace = new scope_trace(__METHOD__);
  69. $input = json_decode($body,true);
  70. if($input == false) {
  71. return false;
  72. }
  73. $act = $input['act'];
  74. if(empty($act)) return false;
  75. if($act == proto_type::act_fcgi) {
  76. $ret = $this->onFcgi($bufid,$input);
  77. process_looper::instance()->write($bufid,$ret);
  78. return true;
  79. }
  80. elseif($act == proto_type::act_access) {
  81. $ret = $this->onAccess($bufid,$input);
  82. process_looper::instance()->write($bufid,$ret);
  83. return true;
  84. }
  85. else {
  86. return false;
  87. }
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. private function onFcgi($bufid, $input)
  91. {
  92. $op = $input['op'];
  93. if($op == 'create')
  94. {
  95. $ret = $this->mFactory->create($input,$fnew);
  96. if($ret != false)
  97. {
  98. $invite = $input['invite'];
  99. if($invite)
  100. {
  101. $roomid = $ret['room'];
  102. $inviter = $ret['creator'];
  103. if($roomid <= 0 || $inviter <= 0) {
  104. return $this->error(errcode::ErrRoomCreate);
  105. }
  106. if($this->build($roomid,$fnew) == false) {
  107. return $this->error(errcode::ErrRoomBuild);
  108. }
  109. $result = $this->invite($roomid,$inviter,[$inviter]);
  110. if($result != false) {
  111. $ret = array_merge($ret,$result);
  112. return $this->success($ret);
  113. } else {
  114. return $this->error(errcode::ErrRoomInvite);
  115. }
  116. }
  117. else {
  118. return $this->error(errcode::ErrRoomCreate);
  119. }
  120. }
  121. return $this->error(errcode::ErrRoomCreate);
  122. }
  123. elseif($op == 'invite')
  124. {
  125. $roomid = intval($input['room']);
  126. $inviter = intval($input['inviter']);
  127. $invitees = $input['invitees'];
  128. if($roomid <= 0 || $inviter <= 0 || empty($invitees)) {
  129. return $this->error(errcode::ErrRoomInvite);
  130. }
  131. $client = $this->find_room($roomid,$bufid);
  132. if($client == false) {
  133. $this->build($roomid);
  134. }
  135. $ret = $this->invite($roomid,$inviter,$invitees);
  136. if($ret != false) {
  137. return $this->success($ret);
  138. } else {
  139. return $this->error(errcode::ErrRoomInvite);
  140. }
  141. }
  142. elseif($op == 'change')
  143. {
  144. $roomid = intval($input['room']);
  145. if($roomid <= 0) {
  146. return $this->error(errcode::ErrRoomChange);
  147. }
  148. $client = $this->find_room($roomid,$bufid);
  149. if($client == false) {
  150. $this->build($roomid);
  151. }
  152. $ret = $this->change($roomid);
  153. if($ret != false) {
  154. return $this->success($ret);
  155. } else {
  156. return $this->error(errcode::ErrRoomChange);
  157. }
  158. }
  159. elseif($op == 'leave')
  160. {
  161. $roomid = intval($input['room']);
  162. $user = intval($input['user']);
  163. if($roomid <= 0 || $user <= 0) {
  164. return $this->error(errcode::ErrRoomLeave);
  165. }
  166. $client = $this->find_room($roomid,$bufid);
  167. if($client == false) {
  168. $this->build($roomid);
  169. }
  170. $ret = $this->leave($roomid,$user);
  171. if($ret != false) {
  172. return $this->success($ret);
  173. } else {
  174. return $this->error(errcode::ErrRoomLeave);
  175. }
  176. }
  177. elseif($op == 'kickout')
  178. {
  179. $roomid = intval($input['room']);
  180. $user = intval($input['user']);
  181. $kicks = $input['kicks'];
  182. if($roomid <= 0 || $user <= 0) {
  183. return $this->error(errcode::ErrRoomKickout);
  184. }
  185. $client = $this->find_room($roomid,$bufid);
  186. if($client == false) {
  187. $this->build($roomid);
  188. }
  189. $ret = $this->kickout($roomid,$user,$kicks);
  190. if($ret != false) {
  191. return $this->success($ret);
  192. } else {
  193. return $this->error(errcode::ErrRoomKickout);
  194. }
  195. }
  196. elseif ($op == 'notice_room')
  197. {
  198. $roomid = intval($input['room']);
  199. $type = $input['type'];
  200. $content = $input['content'];
  201. $user = $input['user'];
  202. if($roomid <= 0 || empty($content)) {
  203. return $this->error(errcode::ErrRoomNotice);
  204. }
  205. $client = $this->find_room($roomid,$bufid);
  206. if($client == false) {
  207. $this->build($roomid);
  208. }
  209. $ret = $this->notice_room($roomid,$type,$content,$user);
  210. if($ret != false) {
  211. return $this->success($ret);
  212. } else {
  213. return $this->error(errcode::ErrRoomKickout);
  214. }
  215. }
  216. elseif($op == 'notice_all')
  217. {
  218. $content = $input['content'];
  219. $type = $input['type'];
  220. $msg = $input['msg'];
  221. $ret = $this->notice_all($type,$content,$msg);
  222. if($ret != false) {
  223. return $this->success(NULL);
  224. } else {
  225. return $this->error(errcode::ErrRoomPush);
  226. }
  227. }
  228. elseif($op == 'notice_users')
  229. {
  230. $content = $input['content'];
  231. $type = $input['type'];
  232. $users = $input['users'];
  233. $msg = $input['msg'];
  234. $ret = $this->notice_users($users,$type,$content,$msg);
  235. if($ret != false) {
  236. return $this->success(NULL);
  237. } else {
  238. return $this->error(errcode::ErrRoomPush);
  239. }
  240. }
  241. else {
  242. return $this->error(errcode::ErrRoomFactoryOp);
  243. }
  244. }
  245. private function onAccess($bufid,$input)
  246. {
  247. $op = $input['op'];
  248. if($op == 'who')
  249. {
  250. $this->mAccUniquer->add_value($bufid);
  251. return $this->success(['act' => proto_type::act_access,'op' => 'who']);
  252. }
  253. elseif($op == 'build')
  254. {
  255. $roomid = intval($input['room']);
  256. $client = $this->find_room($roomid,$bufid);
  257. if($client == false) {
  258. $this->build($roomid);
  259. }
  260. return $this->success(['act' => proto_type::act_access,'op' => $op,'room' => $roomid]);
  261. }
  262. else {
  263. return $this->error(errcode::ErrParamter);
  264. }
  265. }
  266. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  267. private function find_room($roomid,&$outbufid)
  268. {
  269. foreach ($this->mBufidRooms as $bufid => $client)
  270. {
  271. if($client->contain_room($roomid)) {
  272. $outbufid = $bufid;
  273. return $client;
  274. }
  275. }
  276. return false;
  277. }
  278. private function build($roomid,$newroom = false)
  279. {
  280. $client = $this->find_room($roomid,$bufid);
  281. if($client != false) {
  282. $this->block($bufid);
  283. $ret = $client->build($roomid,$newroom);
  284. $this->unblock($bufid);
  285. return $ret;
  286. }
  287. $bufid = $this->room_bufid();
  288. if($bufid != false)
  289. {
  290. $client = $this->mBufidRooms[$bufid];
  291. $this->block($bufid);
  292. $ret = $client->build($roomid,$newroom);
  293. $this->unblock($bufid);
  294. if($ret != false) {
  295. $client->add_room($roomid);
  296. }
  297. return $ret;
  298. }
  299. else {
  300. return false;
  301. }
  302. }
  303. private function invite($roomid, $inviter,$invitees)
  304. {
  305. $client = $this->find_room($roomid,$bufid);
  306. if($client != false) {
  307. $this->block($bufid);
  308. $ret = $client->invite($roomid,$inviter,$invitees);
  309. $this->unblock($bufid);
  310. return $ret;
  311. }
  312. return false;
  313. }
  314. private function change($roomid)
  315. {
  316. $client = $this->find_room($roomid,$bufid);
  317. if($client != false) {
  318. $this->block($bufid);
  319. $ret = $client->change($roomid);
  320. $this->unblock($bufid);
  321. return $ret;
  322. }
  323. return false;
  324. }
  325. private function leave($roomid, $user)
  326. {
  327. foreach ($this->mBufidRooms as $bufid => $client)
  328. {
  329. if($client->contain_room($roomid))
  330. {
  331. $this->block($bufid);
  332. $ret = $client->leave($roomid,$user);
  333. $this->unblock($bufid);
  334. return $ret;
  335. }
  336. }
  337. }
  338. private function kickout($roomid, $user,$kicks)
  339. {
  340. foreach ($this->mBufidRooms as $bufid => $client)
  341. {
  342. if($client->contain_room($roomid))
  343. {
  344. $this->block($bufid);
  345. $ret = $client->kickout($roomid,$user,$kicks);
  346. $this->unblock($bufid);
  347. return $ret;
  348. }
  349. }
  350. }
  351. private function notice_room($roomid,$type,$content,$user)
  352. {
  353. foreach ($this->mBufidRooms as $bufid => $client)
  354. {
  355. if($client->contain_room($roomid))
  356. {
  357. $this->block($bufid);
  358. $ret = $client->notice_room($roomid,$type,$content,$user);
  359. $this->unblock($bufid);
  360. return $ret;
  361. }
  362. }
  363. }
  364. private function notice_all($type,$content,$msg)
  365. {
  366. $bufid = $this->room_bufid();
  367. if($bufid != false)
  368. {
  369. $client = $this->mBufidRooms[$bufid];
  370. $this->block($bufid);
  371. $client->notice_all($type,$content,$msg);
  372. $this->unblock($bufid);
  373. return true;
  374. }
  375. return false;
  376. }
  377. private function notice_users($users,$type,$content,$msg)
  378. {
  379. $bufid = $this->room_bufid();
  380. if($bufid != false)
  381. {
  382. $client = $this->mBufidRooms[$bufid];
  383. $this->block($bufid);
  384. $client->notice_users($users,$type,$content,$msg);
  385. $this->unblock($bufid);
  386. return true;
  387. }
  388. return false;
  389. }
  390. private function room_bufid()
  391. {
  392. $count = count($this->mBufidRooms);
  393. if($count <= 0) return false;
  394. $pos = $this->mRoomPos % $count;
  395. $i = 0;
  396. foreach ($this->mBufidRooms as $bufid => $client)
  397. {
  398. if($i == $pos) break;
  399. $pos++;
  400. }
  401. $this->mRoomPos++;
  402. Log::record("select bufid = {$bufid}",Log::DEBUG);
  403. return $bufid;
  404. }
  405. private function success($datas)
  406. {
  407. $code = errcode::Success;
  408. $data['code'] = $code;
  409. $data['message'] = errcode::msg($code);
  410. $data['data'] = $datas;
  411. $data['msgtype'] = "reply";
  412. return json_encode($data);
  413. }
  414. private function error($code,$message = '')
  415. {
  416. if (empty($message)) {
  417. $message = errcode::msg($code);
  418. }
  419. $data['code'] = $code;
  420. $data['message'] = $message;
  421. $data['datas'] = null;
  422. $data['msgtype'] = "reply";
  423. return json_encode($data);
  424. }
  425. }