room_processor.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 algorithm;
  13. use QueueClient;
  14. use scope_trace;
  15. use Log;
  16. class room_processor implements IProcessor
  17. {
  18. private $mAccConnes;
  19. private $mFactory;
  20. private $mRooms;
  21. private $mChatwo;
  22. public function __construct()
  23. {
  24. $this->mAccConnes = [];
  25. $this->mFactory = new factory();
  26. $this->mRooms = [];
  27. $this->mChatwo = new chatwo();
  28. }
  29. public function onStart()
  30. {
  31. }
  32. public function onConnected($bufid,$stream,$host,$port,$args)
  33. {
  34. }
  35. public function onClose($bufid)
  36. {
  37. $this->remove_acc($bufid);
  38. }
  39. public function onRequest($bufid, $body)
  40. {
  41. $trace = new scope_trace(__METHOD__);
  42. $input = json_decode($body,true);
  43. if($input == false) {
  44. process_looper::instance()->close($bufid);
  45. return false;
  46. }
  47. $act = $input['act'];
  48. if(empty($act)) return false;
  49. if($act == proto_type::act_factory) {
  50. $ret = $this->onFactory($bufid,$input);
  51. process_looper::instance()->write($bufid,$ret);
  52. return true;
  53. }
  54. elseif($act == proto_type::act_access) {
  55. $ret = $this->onAccess($bufid,$input);
  56. process_looper::instance()->write($bufid,$ret);
  57. return true;
  58. }
  59. elseif($act == proto_type::act_room) {
  60. $ret = $this->onRoom($bufid,$input);
  61. process_looper::instance()->write($bufid,$ret);
  62. return true;
  63. }
  64. elseif($act == proto_type::act_chatwo) {
  65. $ret = $this->onChatwo($bufid,$input);
  66. return true;
  67. }
  68. else {
  69. return false;
  70. }
  71. }
  72. ////////////////////////////////////////Factory Message Handler/////////////////////////////////////////////////////
  73. private function onFactory($bufid,$input)
  74. {
  75. $op = $input['op'];
  76. if($op == 'build') {
  77. return $this->factory_build($bufid,$input);
  78. }
  79. elseif($op == 'list_room') {
  80. return $this->success(['rooms' => $this->mRooms]);
  81. }
  82. elseif($op == 'invite') {
  83. return $this->factory_invite($bufid,$input);
  84. }
  85. elseif($op == 'change') {
  86. return $this->factory_change($bufid,$input);
  87. }
  88. elseif($op == 'notice_room') {
  89. return $this->factory_notice_room($bufid,$input);
  90. }
  91. elseif($op == 'notice_users') {
  92. return $this->factory_notice_users($bufid,$input);
  93. }
  94. elseif($op == 'notice_all') {
  95. return $this->factory_notice_all($bufid,$input);
  96. }
  97. elseif($op == 'leave') {
  98. return $this->factory_leave($bufid,$input);
  99. }
  100. elseif($op == 'kickout') {
  101. return $this->factory_kickout($bufid,$input);
  102. }
  103. else {
  104. return $this->error(errcode::ErrRoomFactoryOp);
  105. }
  106. }
  107. /**
  108. * @param $input
  109. * @return mixed|string
  110. */
  111. private function factory_build($bufid,$input)
  112. {
  113. $roomid = intval($input['room']);
  114. $newroom = boolval($input['newroom']);
  115. if ($roomid <= 0) {
  116. return $this->error(errcode::ErrRoomParam);
  117. }
  118. if (array_key_exists($roomid, $this->mRooms)) {
  119. return $this->success(['room' => $roomid]);
  120. }
  121. $room = $this->mFactory->build($roomid);
  122. if ($room == false) {
  123. return $this->error(errcode::ErrRoomBuild);
  124. }
  125. else {
  126. $this->broad_access(msg_builder::build_message($roomid, $room->room_type()));
  127. $this->mRooms[$roomid] = $room;
  128. if ($newroom) {
  129. $this->broad_access(msg_builder::create_push($room->creator(), $roomid, $room->room_info()));
  130. }
  131. return $this->success(['room' => $room->room_id()]);
  132. }
  133. }
  134. /**
  135. * @param $input
  136. * @return mixed|string
  137. */
  138. private function factory_invite($bufid,$input)
  139. {
  140. $roomid = intval($input['room']);
  141. $inviter = intval($input['inviter']);
  142. if ($roomid <= 0) {
  143. return $this->error(errcode::ErrRoomParam);
  144. }
  145. $room = $this->room($roomid);
  146. if ($room != false)
  147. {
  148. $invitees = $input['invitees'];
  149. if ($inviter > 0 && !empty($invitees))
  150. {
  151. $result = $room->invite($inviter, $invitees,$newusers);
  152. if ($result != false)
  153. {
  154. if (!empty($newusers))
  155. {
  156. $room_info = $room->room_info();
  157. $this->broad_access(msg_builder::invite_message($roomid, $room->room_type(), $newusers));
  158. $inviter = $room->userinfos($inviter);
  159. $this->broad_access(msg_builder::invited_push($roomid, $newusers,$inviter,$room_info));
  160. $this->broadcast_roomsg($room);
  161. $left = $room->usercount(); $right = count($newusers);
  162. $last_count = $left - $right;
  163. Log::record("ready to update -------$last_count||$left||$right|| ",Log::DEBUG);
  164. if($last_count >= 0 && $last_count < 10) {
  165. QueueClient::push("OnUpdateRoom",['room_id' => $roomid]);
  166. } else {
  167. $count = count($newusers);
  168. Model('room')->editRoom(['room_id' => $roomid],['users' => ['exp',"users+{$count}"]]);
  169. }
  170. }
  171. return $this->success($result);
  172. }
  173. }
  174. }
  175. return $this->error(errcode::ErrRoomInvite);
  176. }
  177. private function factory_leave($bufid,$input)
  178. {
  179. $roomid = intval($input['room']);
  180. if ($roomid <= 0) {
  181. return $this->error(errcode::ErrRoomParam);
  182. }
  183. $room = $this->room($roomid);
  184. if ($room != false) {
  185. $userid = intval($input['user']);
  186. if ($userid > 0) {
  187. $result = $room->leave($userid);
  188. if ($result != false)
  189. {
  190. $this->broad_access(msg_builder::leave_message($roomid, $room->room_type(), [$userid]));
  191. $this->reply_roomsg($bufid,$room);
  192. $this->broadcast_roomsg($room);
  193. $user_count = $room->usercount();
  194. if($user_count >= 0 && $user_count < 10) {
  195. QueueClient::push("OnUpdateRoom",['room_id' => $roomid]);
  196. }
  197. return $this->success($result);
  198. }
  199. }
  200. }
  201. return $this->error(errcode::ErrRoomLeave);
  202. }
  203. private function factory_kickout($bufid,$input)
  204. {
  205. $roomid = intval($input['room']);
  206. if ($roomid <= 0) {
  207. return $this->error(errcode::ErrRoomParam);
  208. }
  209. $room = $this->room($roomid);
  210. if ($room != false)
  211. {
  212. $user = intval($input['user']);
  213. $kicks = $input['kicks'];
  214. if ($user > 0)
  215. {
  216. $users = $room->kickout($user,$kicks);
  217. if ($users != false)
  218. {
  219. $this->broad_access(msg_builder::leave_message($roomid, $room->room_type(), $users));
  220. $this->broadcast_roomsg($room);
  221. $user_count = $room->usercount();
  222. if($user_count >= 0 && $user_count < 10) {
  223. QueueClient::push("OnUpdateRoom",['room_id' => $roomid]);
  224. }
  225. return $this->success(['users' => $users]);
  226. }
  227. else {
  228. return $this->success(['users' => []]);
  229. }
  230. }
  231. }
  232. return $this->error(errcode::ErrRoomLeave);
  233. }
  234. private function factory_change($bufid,$input)
  235. {
  236. $roomid = intval($input['room']);
  237. if ($roomid <= 0) {
  238. return $this->error(errcode::ErrRoomParam);
  239. }
  240. $room = $this->room($roomid);
  241. if ($room != false)
  242. {
  243. $result = $room->change();
  244. if ($result != false) {
  245. $this->broad_access(msg_builder::change_push($roomid, $room->room_info()));
  246. return $this->success($result);
  247. }
  248. }
  249. return $this->error(errcode::ErrRoomChange);
  250. }
  251. private function factory_notice_room($bufid, $input)
  252. {
  253. $roomid = intval($input['room']);
  254. $type = $input['type'];
  255. $content = $input['content'];
  256. $user = $input['user'];
  257. if ($roomid <= 0 || empty($type) || empty($content)) {
  258. return $this->error(errcode::ErrRoomParam);
  259. }
  260. $room = $this->room($roomid);
  261. if ($room != false)
  262. {
  263. $result = $room->notice($type,$content,$user);
  264. if($result) {
  265. $this->broadcast_roomsg($room);
  266. return $this->success($result);
  267. } else {
  268. return $this->error(errcode::ErrRoomNotice);
  269. }
  270. }
  271. return $this->error(errcode::ErrRoomChange);
  272. }
  273. ///Push消息处理区/////////////////////////////////////////////////////////////////////////////////////////////////////
  274. private function factory_notice_users($bufid, $input)
  275. {
  276. $content = $input['content'];
  277. $msg = $input['msg'];
  278. $users = $input['users'];
  279. $type = $input['type'];
  280. if($type === proto_type::push_command) {
  281. $pushid = 1;
  282. $msg_type = proto_type::msg_type_command;
  283. $send = $content;
  284. }
  285. elseif($type === proto_type::push_notify) {
  286. $pushid = 2;
  287. $msg_type = proto_type::msg_type_nofity;
  288. $send['content'] = $msg;
  289. }
  290. elseif($type === proto_type::push_apply) {
  291. $pushid = 3;
  292. $msg_type = proto_type::msg_type_apply;
  293. $send['content'] = $msg;
  294. }
  295. else {
  296. return $this->error(errcode::ErrParamter);
  297. }
  298. if($msg_type != proto_type::msg_type_command)
  299. {
  300. $mod_room = Model('room');
  301. $orgmsg = json_encode($content,JSON_UNESCAPED_UNICODE);
  302. foreach ($users as $user) {
  303. $msgid = $mod_room->addRoomMsg(['room_id' => 0,'member_id' => $user, 'type' => $msg_type,'msg' => $msg, 'orgmsg' => $orgmsg, 'add_time' => time(),'msg_type' => 1]);
  304. $send['msgid'] = $msgid;
  305. $send['send_time'] = time();
  306. $this->write_push_users($pushid,[$user],$send);
  307. }
  308. }
  309. else
  310. {
  311. $send['msgid'] = 0;
  312. $send['send_time'] = time();
  313. $this->write_push_users($pushid,$users,$send);
  314. }
  315. return $this->success(NULL);
  316. }
  317. private function factory_notice_all($bufid, $input)
  318. {
  319. $content = $input['content'];
  320. $type = $input['type'];
  321. if($type === proto_type::push_command) {
  322. $pushid = 1;
  323. }
  324. elseif($type === proto_type::push_notify) {
  325. $pushid = 2;
  326. }
  327. elseif($type === proto_type::push_apply) {
  328. $pushid = 3;
  329. }
  330. else {
  331. return $this->error(errcode::ErrParamter);
  332. }
  333. $this->write_push($content);
  334. return $this->success(NULL);
  335. }
  336. ////////////////////////////////////////Access Message Handler//////////////////////////////////////////////////////
  337. private function onAccess($bufid,$input)
  338. {
  339. $op = $input['op'];
  340. if($op == 'list') {
  341. $this->add_acc($bufid);
  342. $reply = ['act' => 'access','op' => $op,'rooms' => $this->rooms()];
  343. return $this->success($reply);
  344. }
  345. else {
  346. return $this->error(errcode::ErrRoomAccessOp);
  347. }
  348. }
  349. private function rooms()
  350. {
  351. $result = [];
  352. foreach ($this->mRooms as $roomid => $room) {
  353. $result[] = $roomid;
  354. }
  355. return $result;
  356. }
  357. ////////////////////////////////////////Room Message Handler////////////////////////////////////////////////////////
  358. private function onRoom($bufid,$input)
  359. {
  360. $roomid = intval($input['room']);
  361. $room = $this->room($roomid);
  362. if($room != false)
  363. {
  364. $function = $input['op'] . 'Op';
  365. if (method_exists($room,$function))
  366. {
  367. $ret = $room->$function($input);
  368. if($ret != false) {
  369. $this->reply_roomsg($bufid,$room);
  370. $this->broadcast_roomsg($room);
  371. }
  372. return $this->success($ret);
  373. }
  374. }
  375. return $this->error(errcode::ErrRoom);
  376. }
  377. ////////////////////////////////////////Peer Message Handler////////////////////////////////////////////////////////
  378. private function onChatwo($bufid, $input)
  379. {
  380. $function = $input['op'] . 'Op';
  381. if (method_exists($this->mChatwo,$function))
  382. {
  383. $msg = $this->mChatwo->$function($input);
  384. if($msg != false)
  385. {
  386. $body = json_encode($msg);
  387. foreach ($this->mAccConnes as $conn) {
  388. process_looper::instance()->write($conn,$body);
  389. }
  390. }
  391. return true;
  392. }
  393. }
  394. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  395. private function write_push_users($pushid,$users,$content)
  396. {
  397. $msg = [];
  398. $msg['act'] = "room";
  399. $msg['op'] = "relay";
  400. $msg['relay_type'] = "users";
  401. $msg['receivers'] = $users;
  402. $msg['room'] = 0;
  403. $msg['receiver_type'] = proto_type::sroom_push;
  404. $msg['msgtype'] = "message";
  405. $msg['body']['act'] = proto_type::act_push;
  406. $msg['body']['op'] = 'message';
  407. $msg['body']['push'] = $pushid;
  408. $msg['body']['content'] = $content;
  409. $msg['body']['msgtype'] = "message";
  410. $body = json_encode($msg);
  411. foreach ($this->mAccConnes as $conn) {
  412. process_looper::instance()->write($conn,$body);
  413. }
  414. }
  415. private function write_push($content)
  416. {
  417. $msg = [];
  418. $msg['act'] = "room";
  419. $msg['op'] = "relay";
  420. $msg['relay_type'] = "alluser";
  421. $msg['receivers'] = [];
  422. $msg['room'] = 0;
  423. $msg['receiver_type'] = proto_type::sroom_push;
  424. $msg['msgtype'] = "message";
  425. $msg['body']['act'] = proto_type::act_push;
  426. $msg['body']['op'] = 'message';
  427. $msg['body']['push'] = 0;
  428. $msg['body']['content'] = $content;
  429. $msg['body']['msgtype'] = "message";
  430. $body = $this->success($msg);
  431. foreach ($this->mAccConnes as $conn) {
  432. process_looper::instance()->write($conn,$body);
  433. }
  434. }
  435. ////////////////////////////////返回包///////////////////////////////////////////////////////////////////////////////
  436. private function success($val)
  437. {
  438. $code = errcode::Success;
  439. $data['code'] = $code;
  440. $data['message'] = errcode::msg($code);
  441. $data['data'] = $val;
  442. $data['msgtype'] = "reply";
  443. return json_encode($data);
  444. }
  445. private function error($code,$message = '')
  446. {
  447. if (empty($message)) {
  448. $message = errcode::msg($code);
  449. }
  450. $data['code'] = $code;
  451. $data['message'] = $message;
  452. $data['data'] = null;
  453. $data['msgtype'] = "reply";
  454. return json_encode($data);
  455. }
  456. private function reply_roomsg($bufid,base_room $room)
  457. {
  458. $retmsgs = $room->relay_reply_msgs();
  459. foreach ($retmsgs as $msg) {
  460. $body = json_encode($msg);
  461. process_looper::instance()->write($bufid,$body);
  462. }
  463. }
  464. private function broad_access($messages)
  465. {
  466. $body = json_encode($messages);
  467. foreach ($this->mAccConnes as $bufid) {
  468. process_looper::instance()->write($bufid,$body);
  469. }
  470. }
  471. private function broadcast_roomsg(base_room $room)
  472. {
  473. $broad_msgs = $room->relay_broadcast_msgs();
  474. foreach ($broad_msgs as $msg)
  475. {
  476. $body = json_encode($msg);
  477. foreach ($this->mAccConnes as $bufid) {
  478. process_looper::instance()->write($bufid,$body);
  479. }
  480. }
  481. }
  482. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  483. private function room($roomid)
  484. {
  485. if($roomid <= 0) return false;
  486. if(array_key_exists($roomid,$this->mRooms)) {
  487. return $this->mRooms[$roomid];
  488. } else {
  489. return false;
  490. }
  491. }
  492. private function add_acc($bufid) {
  493. $connid = intval($bufid);
  494. if($connid <= 0) return false;
  495. if(!algorithm::binary_search($this->mAccConnes,$connid)) {
  496. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  497. algorithm::array_insert($this->mAccConnes,$pos,$connid);
  498. }
  499. return true;
  500. }
  501. private function remove_acc($bufid)
  502. {
  503. $connid = intval($bufid);
  504. if($connid <= 0) return false;
  505. if(algorithm::binary_search($this->mAccConnes,$connid)) {
  506. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  507. algorithm::array_erase($this->mAccConnes,$pos);
  508. }
  509. return true;
  510. }
  511. }