room_processor.php 15 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 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_user') {
  92. return $this->factory_notice_user($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. private function factory_notice_user($bufid, $input)
  273. {
  274. }
  275. private function factory_notice_all($bufid, $input)
  276. {
  277. $content = $input['content'];
  278. $type = $input['type'];
  279. $this->write_push($content);
  280. return $this->success(NULL);
  281. }
  282. ////////////////////////////////////////Access Message Handler//////////////////////////////////////////////////////
  283. private function onAccess($bufid,$input)
  284. {
  285. $op = $input['op'];
  286. if($op == 'list') {
  287. $this->add_acc($bufid);
  288. $reply = ['act' => 'access','op' => $op,'rooms' => $this->rooms()];
  289. return $this->success($reply);
  290. }
  291. else {
  292. return $this->error(errcode::ErrRoomAccessOp);
  293. }
  294. }
  295. private function rooms()
  296. {
  297. $result = [];
  298. foreach ($this->mRooms as $roomid => $room) {
  299. $result[] = $roomid;
  300. }
  301. return $result;
  302. }
  303. ////////////////////////////////////////Room Message Handler////////////////////////////////////////////////////////
  304. private function onRoom($bufid,$input)
  305. {
  306. $roomid = intval($input['room']);
  307. $room = $this->room($roomid);
  308. if($room != false)
  309. {
  310. $function = $input['op'] . 'Op';
  311. if (method_exists($room,$function))
  312. {
  313. $ret = $room->$function($input);
  314. if($ret != false) {
  315. $this->reply_roomsg($bufid,$room);
  316. $this->broadcast_roomsg($room);
  317. }
  318. return $this->success($ret);
  319. }
  320. }
  321. return $this->error(errcode::ErrRoom);
  322. }
  323. ////////////////////////////////////////Peer Message Handler////////////////////////////////////////////////////////
  324. private function onChatwo($bufid, $input)
  325. {
  326. $function = $input['op'] . 'Op';
  327. if (method_exists($this->mChatwo,$function))
  328. {
  329. $msg = $this->mChatwo->$function($input);
  330. if($msg != false)
  331. {
  332. $body = json_encode($msg);
  333. foreach ($this->mAccConnes as $conn) {
  334. process_looper::instance()->write($conn,$body);
  335. }
  336. }
  337. return true;
  338. }
  339. }
  340. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  341. private function write_push($content)
  342. {
  343. $msg = [];
  344. $msg['act'] = "room";
  345. $msg['op'] = "relay";
  346. $msg['relay_type'] = "alluser";
  347. $msg['receivers'] = [];
  348. $msg['room'] = 0;
  349. $msg['receiver_type'] = proto_type::sroom_push;
  350. $msg['body']['act'] = proto_type::act_push;
  351. $msg['body']['op'] = 'message';
  352. $msg['body']['content'] = $content;
  353. $body = $this->success($msg);
  354. foreach ($this->mAccConnes as $conn) {
  355. process_looper::instance()->write($conn,$body);
  356. }
  357. }
  358. ////////////////////////////////返回包///////////////////////////////////////////////////////////////////////////////
  359. private function success($val)
  360. {
  361. $code = errcode::Success;
  362. $data['code'] = $code;
  363. $data['message'] = errcode::msg($code);
  364. $data['data'] = $val;
  365. $data['msgtype'] = "reply";
  366. return json_encode($data);
  367. }
  368. private function error($code,$message = '')
  369. {
  370. if (empty($message)) {
  371. $message = errcode::msg($code);
  372. }
  373. $data['code'] = $code;
  374. $data['message'] = $message;
  375. $data['data'] = null;
  376. $data['msgtype'] = "reply";
  377. return json_encode($data);
  378. }
  379. private function reply_roomsg($bufid,base_room $room)
  380. {
  381. $retmsgs = $room->relay_reply_msgs();
  382. foreach ($retmsgs as $msg) {
  383. $body = json_encode($msg);
  384. process_looper::instance()->write($bufid,$body);
  385. }
  386. }
  387. private function broad_access($messages)
  388. {
  389. $body = json_encode($messages);
  390. foreach ($this->mAccConnes as $bufid) {
  391. process_looper::instance()->write($bufid,$body);
  392. }
  393. }
  394. private function broadcast_roomsg(base_room $room)
  395. {
  396. $broad_msgs = $room->relay_broadcast_msgs();
  397. foreach ($broad_msgs as $msg)
  398. {
  399. $body = json_encode($msg);
  400. foreach ($this->mAccConnes as $bufid) {
  401. process_looper::instance()->write($bufid,$body);
  402. }
  403. }
  404. }
  405. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  406. private function room($roomid)
  407. {
  408. if($roomid <= 0) return false;
  409. if(array_key_exists($roomid,$this->mRooms)) {
  410. return $this->mRooms[$roomid];
  411. } else {
  412. return false;
  413. }
  414. }
  415. private function add_acc($bufid) {
  416. $connid = intval($bufid);
  417. if($connid <= 0) return false;
  418. if(!algorithm::binary_search($this->mAccConnes,$connid)) {
  419. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  420. algorithm::array_insert($this->mAccConnes,$pos,$connid);
  421. }
  422. return true;
  423. }
  424. private function remove_acc($bufid)
  425. {
  426. $connid = intval($bufid);
  427. if($connid <= 0) return false;
  428. if(algorithm::binary_search($this->mAccConnes,$connid)) {
  429. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  430. algorithm::array_erase($this->mAccConnes,$pos);
  431. }
  432. return true;
  433. }
  434. }