room_processor.php 13 KB

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