room_processor.php 14 KB

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