room_processor.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 search\IProcessor;
  10. use errcode;
  11. use algorithm;
  12. class room_processor implements IProcessor
  13. {
  14. private $mAccConnes;
  15. private $mFactory;
  16. private $mRooms;
  17. public function __construct()
  18. {
  19. $this->mAccConnes = [];
  20. $this->mFactory = new factory();
  21. $this->mRooms = [];
  22. }
  23. public function onRequest($bufid, $body)
  24. {
  25. $input = json_decode($body,true);
  26. if($input == false) {
  27. room_server::instance()->close($bufid);
  28. return false;
  29. }
  30. $act = $input['act'];
  31. if(!empty($act) && $act == proto_type::act_factory) {
  32. $ret = $this->onFactory($bufid,$input);
  33. room_server::instance()->write($bufid,$ret);
  34. return true;
  35. }
  36. elseif(!empty($act) && $act == proto_type::act_access) {
  37. $ret = $this->onAccess($bufid,$input);
  38. return $ret;
  39. }
  40. else {
  41. $ret = $this->onRoom($bufid,$input);
  42. return $ret;
  43. }
  44. }
  45. public function onClose($bufid)
  46. {
  47. $this->remove_acc($bufid);
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. private function onFactory($bufid,$input)
  51. {
  52. $op = $input['op'];
  53. if($op == 'build')
  54. {
  55. $roomid = intval($input['room']);
  56. if($roomid <= 0) return $this->error(errcode::ErrRoomParam);
  57. if(array_key_exists($roomid,$this->mRooms)) {
  58. return $this->success(['room' => $roomid]);
  59. }
  60. $room = $this->mFactory->build($roomid);
  61. if($room == false) {
  62. return $this->error(errcode::ErrRoomBuild);
  63. }
  64. else {
  65. $this->mRooms[$roomid] = $room;
  66. return $this->success(['room' => $room->room_id()]);
  67. }
  68. }
  69. elseif($op == 'list_room')
  70. {
  71. return $this->success(['rooms' => $this->mRooms]);
  72. }
  73. elseif($op == 'invite')
  74. {
  75. $roomid = intval($input['room']);
  76. if($roomid <= 0) return $this->error(errcode::ErrRoomParam);
  77. if(!array_key_exists($roomid,$this->mRooms))
  78. {
  79. $room = $this->mFactory->build($roomid);
  80. if($room != false) {
  81. $this->mRooms[$roomid] = $room;
  82. }
  83. }
  84. else {
  85. $room = $this->room($roomid);
  86. }
  87. if($room != false)
  88. {
  89. $userid = intval($input['user']);
  90. if($userid > 0)
  91. {
  92. $room_key = $room->invite($userid);
  93. if($room_key != false) {
  94. return $this->success(['room' => $roomid,'room_key' => $room_key]);
  95. }
  96. }
  97. }
  98. return $this->error(errcode::ErrRoomInvite);
  99. }
  100. else
  101. {
  102. return $this->error(errcode::ErrRoomFactoryOp);
  103. }
  104. }
  105. private function onAccess($bufid,$input)
  106. {
  107. $op = $input['op'];
  108. if($op == 'list') {
  109. $this->add_acc($bufid);
  110. $this->write_accmsg($bufid,"list",['rooms' => $this->rooms()]);
  111. return true;
  112. }
  113. else {
  114. return $this->error(errcode::ErrRoomAccessOp);
  115. }
  116. }
  117. private function rooms()
  118. {
  119. $result = [];
  120. foreach ($this->mRooms as $roomid => $room) {
  121. $result[] = $roomid;
  122. }
  123. return $result;
  124. }
  125. private function onRoom($bufid,$input)
  126. {
  127. $roomid = intval($input['room']);
  128. $room = $this->room($roomid);
  129. if($room != false)
  130. {
  131. $function = $input['op'] . 'Op';
  132. $roomkey = $input['room_key'];
  133. if (method_exists($room,$function))
  134. {
  135. $success = $room->$function($input);
  136. if($success) {
  137. $this->write_roomsg($bufid,$room);
  138. }
  139. else {
  140. $this->del_user($bufid,$room->room_id(), [$roomkey]);
  141. }
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. private function del_user($bufid,$room_id,$roomkeys)
  148. {
  149. $msg = [];
  150. $msg['receivers'] = ['type' => 'roomkey', 'users' => $roomkeys];
  151. $msg['room'] = $room_id;
  152. $msg['act'] = 'room';
  153. $msg['op'] = 'deluser';
  154. $body = $this->success(['msg' => $msg]);
  155. room_server::instance()->write($bufid,$body);
  156. }
  157. protected function write_accmsg($bufid,$op,$content)
  158. {
  159. $msg['act'] = "access";
  160. $msg['op'] = $op;
  161. $msg['body']['act'] = 'access';
  162. $msg['body']['op'] = $op;
  163. $msg['body']['content'] = $content;
  164. $body = $this->success(['msg' => $msg]);
  165. room_server::instance()->write($bufid,$body);
  166. }
  167. private function write_roomsg($bufid,$room)
  168. {
  169. $retmsgs = $room->return_msgs();
  170. foreach ($retmsgs as $msg) {
  171. $body = $this->success(['msg' => $msg]);
  172. room_server::instance()->write($bufid,$body);
  173. }
  174. $broad_msgs = $room->broadcast_msgs();
  175. foreach ($broad_msgs as $msg)
  176. {
  177. $body = $this->success(['msg' => $msg]);
  178. foreach ($this->mAccConnes as $conn) {
  179. room_server::instance()->write($conn,$body);
  180. }
  181. }
  182. }
  183. private function room($roomid)
  184. {
  185. if($roomid <= 0) return false;
  186. if(array_key_exists($roomid,$this->mRooms)) {
  187. return $this->mRooms[$roomid];
  188. } else {
  189. return false;
  190. }
  191. }
  192. private function add_acc($bufid) {
  193. $connid = intval($bufid);
  194. if($connid <= 0) return false;
  195. if(!algorithm::binary_search($this->mAccConnes,$connid)) {
  196. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  197. algorithm::array_insert($this->mAccConnes,$pos,$connid);
  198. }
  199. return true;
  200. }
  201. private function remove_acc($bufid)
  202. {
  203. $connid = intval($bufid);
  204. if($connid <= 0) return false;
  205. if(algorithm::binary_search($this->mAccConnes,$connid)) {
  206. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  207. algorithm::array_erase($this->mAccConnes,$pos);
  208. }
  209. return true;
  210. }
  211. private function success($datas)
  212. {
  213. $code = errcode::Success;
  214. $data['code'] = $code;
  215. $data['message'] = errcode::msg($code);
  216. $data['data'] = $datas;
  217. return json_encode($data);
  218. }
  219. private function error($code,$message = '')
  220. {
  221. if (empty($message)) {
  222. $message = errcode::msg($code);
  223. }
  224. $data = array();
  225. $data['code'] = $code;
  226. $data['message'] = $message;
  227. $data['datas'] = null;
  228. return json_encode($data);
  229. }
  230. }