room_processor.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. if ($roomid <= 0 || empty($type) || empty($content)) {
  257. return $this->error(errcode::ErrRoomParam);
  258. }
  259. $room = $this->room($roomid);
  260. if ($room != false)
  261. {
  262. $result = $room->notice($type,$content);
  263. if($result) {
  264. $this->broadcast_roomsg($room);
  265. return $this->success($result);
  266. } else {
  267. return $this->error(errcode::ErrRoomNotice);
  268. }
  269. }
  270. return $this->error(errcode::ErrRoomChange);
  271. }
  272. ///Push消息处理区/////////////////////////////////////////////////////////////////////////////////////////////////////
  273. private function factory_notice_users($bufid, $input)
  274. {
  275. $content = $input['content'];
  276. $msg = $input['msg'];
  277. $users = $input['users'];
  278. $type = $input['type'];
  279. if($type === proto_type::push_command) {
  280. $pushid = 1;
  281. $msg_type = proto_type::msg_type_command;
  282. $send = $content;
  283. }
  284. elseif($type === proto_type::push_notify) {
  285. $pushid = 2;
  286. $msg_type = proto_type::msg_type_nofity;
  287. $send[] = $msg;
  288. }
  289. elseif($type === proto_type::push_apply) {
  290. $pushid = 3;
  291. $msg_type = proto_type::msg_type_apply;
  292. $send[] = $msg;
  293. }
  294. else {
  295. return $this->error(errcode::ErrParamter);
  296. }
  297. if($msg_type != proto_type::msg_type_command)
  298. {
  299. $mod_room = Model('room');
  300. $orgmsg = json_encode($content);
  301. foreach ($users as $user) {
  302. $msgid = $mod_room->addRoomMsg(['room_id' => 0,'member_id' => $user, 'type' => $msg_type,'msg' => $msg, 'orgmsg' => $orgmsg, 'add_time' => time(),'msg_type' => 1]);
  303. $send['msgid'] = $msgid;
  304. $send['send_time'] = time();
  305. $this->write_push_users($pushid,$users,$send);
  306. }
  307. }
  308. return $this->success(NULL);
  309. }
  310. private function factory_notice_all($bufid, $input)
  311. {
  312. $content = $input['content'];
  313. $type = $input['type'];
  314. if($type === proto_type::push_command) {
  315. $pushid = 1;
  316. }
  317. elseif($type === proto_type::push_notify) {
  318. $pushid = 2;
  319. }
  320. elseif($type === proto_type::push_apply) {
  321. $pushid = 3;
  322. }
  323. else {
  324. return $this->error(errcode::ErrParamter);
  325. }
  326. $this->write_push($content);
  327. return $this->success(NULL);
  328. }
  329. ////////////////////////////////////////Access Message Handler//////////////////////////////////////////////////////
  330. private function onAccess($bufid,$input)
  331. {
  332. $op = $input['op'];
  333. if($op == 'list') {
  334. $this->add_acc($bufid);
  335. $reply = ['act' => 'access','op' => $op,'rooms' => $this->rooms()];
  336. return $this->success($reply);
  337. }
  338. else {
  339. return $this->error(errcode::ErrRoomAccessOp);
  340. }
  341. }
  342. private function rooms()
  343. {
  344. $result = [];
  345. foreach ($this->mRooms as $roomid => $room) {
  346. $result[] = $roomid;
  347. }
  348. return $result;
  349. }
  350. ////////////////////////////////////////Room Message Handler////////////////////////////////////////////////////////
  351. private function onRoom($bufid,$input)
  352. {
  353. $roomid = intval($input['room']);
  354. $room = $this->room($roomid);
  355. if($room != false)
  356. {
  357. $function = $input['op'] . 'Op';
  358. if (method_exists($room,$function))
  359. {
  360. $ret = $room->$function($input);
  361. if($ret != false) {
  362. $this->reply_roomsg($bufid,$room);
  363. $this->broadcast_roomsg($room);
  364. }
  365. return $this->success($ret);
  366. }
  367. }
  368. return $this->error(errcode::ErrRoom);
  369. }
  370. ////////////////////////////////////////Peer Message Handler////////////////////////////////////////////////////////
  371. private function onChatwo($bufid, $input)
  372. {
  373. $function = $input['op'] . 'Op';
  374. if (method_exists($this->mChatwo,$function))
  375. {
  376. $msg = $this->mChatwo->$function($input);
  377. if($msg != false)
  378. {
  379. $body = json_encode($msg);
  380. foreach ($this->mAccConnes as $conn) {
  381. process_looper::instance()->write($conn,$body);
  382. }
  383. }
  384. return true;
  385. }
  386. }
  387. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  388. private function write_push_users($pushid,$users,$content)
  389. {
  390. $msg = [];
  391. $msg['act'] = "room";
  392. $msg['op'] = "relay";
  393. $msg['relay_type'] = "users";
  394. $msg['receivers'] = $users;
  395. $msg['room'] = 0;
  396. $msg['receiver_type'] = proto_type::sroom_push;
  397. $msg['msgtype'] = "message";
  398. $msg['body']['act'] = proto_type::act_push;
  399. $msg['body']['op'] = 'message';
  400. $msg['body']['push'] = $pushid;
  401. $msg['body']['content'] = $content;
  402. $msg['body']['msgtype'] = "message";
  403. $body = json_encode($msg);
  404. foreach ($this->mAccConnes as $conn) {
  405. process_looper::instance()->write($conn,$body);
  406. }
  407. }
  408. private function write_push($content)
  409. {
  410. $msg = [];
  411. $msg['act'] = "room";
  412. $msg['op'] = "relay";
  413. $msg['relay_type'] = "alluser";
  414. $msg['receivers'] = [];
  415. $msg['room'] = 0;
  416. $msg['receiver_type'] = proto_type::sroom_push;
  417. $msg['msgtype'] = "message";
  418. $msg['body']['act'] = proto_type::act_push;
  419. $msg['body']['op'] = 'message';
  420. $msg['body']['push'] = 0;
  421. $msg['body']['content'] = $content;
  422. $msg['body']['msgtype'] = "message";
  423. $body = $this->success($msg);
  424. foreach ($this->mAccConnes as $conn) {
  425. process_looper::instance()->write($conn,$body);
  426. }
  427. }
  428. ////////////////////////////////返回包///////////////////////////////////////////////////////////////////////////////
  429. private function success($val)
  430. {
  431. $code = errcode::Success;
  432. $data['code'] = $code;
  433. $data['message'] = errcode::msg($code);
  434. $data['data'] = $val;
  435. $data['msgtype'] = "reply";
  436. return json_encode($data);
  437. }
  438. private function error($code,$message = '')
  439. {
  440. if (empty($message)) {
  441. $message = errcode::msg($code);
  442. }
  443. $data['code'] = $code;
  444. $data['message'] = $message;
  445. $data['data'] = null;
  446. $data['msgtype'] = "reply";
  447. return json_encode($data);
  448. }
  449. private function reply_roomsg($bufid,base_room $room)
  450. {
  451. $retmsgs = $room->relay_reply_msgs();
  452. foreach ($retmsgs as $msg) {
  453. $body = json_encode($msg);
  454. process_looper::instance()->write($bufid,$body);
  455. }
  456. }
  457. private function broad_access($messages)
  458. {
  459. $body = json_encode($messages);
  460. foreach ($this->mAccConnes as $bufid) {
  461. process_looper::instance()->write($bufid,$body);
  462. }
  463. }
  464. private function broadcast_roomsg(base_room $room)
  465. {
  466. $broad_msgs = $room->relay_broadcast_msgs();
  467. foreach ($broad_msgs as $msg)
  468. {
  469. $body = json_encode($msg);
  470. foreach ($this->mAccConnes as $bufid) {
  471. process_looper::instance()->write($bufid,$body);
  472. }
  473. }
  474. }
  475. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  476. private function room($roomid)
  477. {
  478. if($roomid <= 0) return false;
  479. if(array_key_exists($roomid,$this->mRooms)) {
  480. return $this->mRooms[$roomid];
  481. } else {
  482. return false;
  483. }
  484. }
  485. private function add_acc($bufid) {
  486. $connid = intval($bufid);
  487. if($connid <= 0) return false;
  488. if(!algorithm::binary_search($this->mAccConnes,$connid)) {
  489. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  490. algorithm::array_insert($this->mAccConnes,$pos,$connid);
  491. }
  492. return true;
  493. }
  494. private function remove_acc($bufid)
  495. {
  496. $connid = intval($bufid);
  497. if($connid <= 0) return false;
  498. if(algorithm::binary_search($this->mAccConnes,$connid)) {
  499. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  500. algorithm::array_erase($this->mAccConnes,$pos);
  501. }
  502. return true;
  503. }
  504. }