room_processor.php 13 KB

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