room_processor.php 14 KB

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