room_processor.php 12 KB

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