room_processor.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. use scope_trace;
  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 onConnected($bufid,$stream,$host,$port,$args)
  27. {
  28. }
  29. public function onRequest($bufid, $body)
  30. {
  31. new scope_trace(__METHOD__);
  32. $input = json_decode($body,true);
  33. if($input == false) {
  34. room_server::instance()->close($bufid);
  35. return false;
  36. }
  37. $act = $input['act'];
  38. if(empty($act)) return false;
  39. if($act == proto_type::act_factory) {
  40. $ret = $this->onFactory($bufid,$input);
  41. room_server::instance()->write($bufid,$ret);
  42. return true;
  43. }
  44. elseif($act == proto_type::act_access) {
  45. $ret = $this->onAccess($bufid,$input);
  46. room_server::instance()->write($bufid,$ret);
  47. return $ret;
  48. }
  49. elseif($act == proto_type::act_room) {
  50. $ret = $this->onRoom($bufid,$input);
  51. return $ret;
  52. }
  53. elseif($act == proto_type::act_chatwo) {
  54. $ret = $this->onChatwo($bufid,$input);
  55. return $ret;
  56. }
  57. }
  58. public function onClose($bufid)
  59. {
  60. $this->remove_acc($bufid);
  61. }
  62. ////////////////////////////////////////Factory Message Handler/////////////////////////////////////////////////////
  63. private function onFactory($bufid,$input)
  64. {
  65. $op = $input['op'];
  66. if($op == 'build')
  67. {
  68. $roomid = intval($input['room']);
  69. if($roomid <= 0) return $this->error(errcode::ErrRoomParam);
  70. if(array_key_exists($roomid,$this->mRooms)) {
  71. return $this->success(['room' => $roomid]);
  72. }
  73. $room = $this->mFactory->build($roomid);
  74. if($room == false) {
  75. return $this->error(errcode::ErrRoomBuild);
  76. }
  77. else {
  78. $this->mRooms[$roomid] = $room;
  79. return $this->success(['room' => $room->room_id()]);
  80. }
  81. }
  82. elseif($op == 'list_room')
  83. {
  84. return $this->success(['rooms' => $this->mRooms]);
  85. }
  86. elseif($op == 'invite')
  87. {
  88. $roomid = intval($input['room']);
  89. if($roomid <= 0) {
  90. return $this->error(errcode::ErrRoomParam);
  91. }
  92. if(!array_key_exists($roomid,$this->mRooms))
  93. {
  94. $room = $this->mFactory->build($roomid);
  95. if($room != false) {
  96. $this->mRooms[$roomid] = $room;
  97. }
  98. }
  99. else {
  100. $room = $this->room($roomid);
  101. }
  102. if($room != false)
  103. {
  104. $inviter = intval($input['inviter']);
  105. $invitees = $input['invitees'];
  106. if($inviter > 0 && !empty($invitees))
  107. {
  108. $result = $room->invite($inviter,$invitees);
  109. if($result != false) {
  110. return $this->success($result);
  111. }
  112. }
  113. }
  114. return $this->error(errcode::ErrRoomInvite);
  115. }
  116. elseif($op == 'leave')
  117. {
  118. $roomid = intval($input['room']);
  119. if($roomid <= 0) {
  120. return $this->error(errcode::ErrRoomParam);
  121. }
  122. if(!array_key_exists($roomid,$this->mRooms))
  123. {
  124. $room = $this->mFactory->build($roomid);
  125. if($room != false) {
  126. $this->mRooms[$roomid] = $room;
  127. }
  128. }
  129. else {
  130. $room = $this->room($roomid);
  131. }
  132. if($room != false)
  133. {
  134. $userid = intval($input['user']);
  135. if($userid > 0)
  136. {
  137. $result = $room->leave($userid);
  138. if($result != false) {
  139. return $this->success($result);
  140. }
  141. }
  142. }
  143. return $this->error(errcode::ErrRoomLeave);
  144. }
  145. elseif($op == 'push') {
  146. $content = $input['content'];
  147. $this->write_push($content);
  148. return $this->success(NULL);
  149. }
  150. else
  151. {
  152. return $this->error(errcode::ErrRoomFactoryOp);
  153. }
  154. }
  155. ////////////////////////////////////////Access Message Handler//////////////////////////////////////////////////////
  156. private function onAccess($bufid,$input)
  157. {
  158. $op = $input['op'];
  159. if($op == 'list') {
  160. $this->add_acc($bufid);
  161. $reply = ['act' => 'access','op' => $op,'rooms' => $this->rooms()];
  162. return $this->success($reply);
  163. }
  164. else {
  165. return $this->error(errcode::ErrRoomAccessOp);
  166. }
  167. }
  168. private function rooms()
  169. {
  170. $result = [];
  171. foreach ($this->mRooms as $roomid => $room) {
  172. $result[] = $roomid;
  173. }
  174. return $result;
  175. }
  176. ////////////////////////////////////////Room Message Handler////////////////////////////////////////////////////////
  177. private function onRoom($bufid,$input)
  178. {
  179. $roomid = intval($input['room']);
  180. $room = $this->room($roomid);
  181. if($room != false)
  182. {
  183. $function = $input['op'] . 'Op';
  184. if (method_exists($room,$function))
  185. {
  186. $success = $room->$function($input);
  187. if($success) {
  188. $this->write_roomsg($bufid,$room);
  189. }
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. ////////////////////////////////////////Peer Message Handler////////////////////////////////////////////////////////
  196. private function onChatwo($bufid, $input)
  197. {
  198. $function = $input['op'] . 'Op';
  199. if (method_exists($this->mChatwo,$function))
  200. {
  201. $msg = $this->mChatwo->$function($input);
  202. if($msg != false)
  203. {
  204. $body = $this->success($msg);
  205. foreach ($this->mAccConnes as $conn) {
  206. room_server::instance()->write($conn,$body);
  207. }
  208. }
  209. return true;
  210. }
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  213. private function write_push($content)
  214. {
  215. $msg = [];
  216. $msg['act'] = "room";
  217. $msg['op'] = "relay";
  218. $msg['relay_type'] = "alluser";
  219. $msg['receivers'] = [];
  220. $msg['room'] = 0;
  221. $msg['receiver_type'] = proto_type::sroom_push;
  222. $msg['body']['act'] = proto_type::act_push;
  223. $msg['body']['op'] = 'message';
  224. $msg['body']['content'] = $content;
  225. $body = $this->success($msg);
  226. foreach ($this->mAccConnes as $conn) {
  227. room_server::instance()->write($conn,$body);
  228. }
  229. }
  230. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. protected function write_accmsg($bufid,$op,$content)
  232. {
  233. $msg['act'] = "access";
  234. $msg['op'] = $op;
  235. $msg['body']['act'] = 'access';
  236. $msg['body']['op'] = $op;
  237. $msg['body']['content'] = $content;
  238. $body = $this->success($msg);
  239. room_server::instance()->write($bufid,$body);
  240. }
  241. private function write_roomsg($bufid,$room)
  242. {
  243. $retmsgs = $room->relay_users_msgs();
  244. foreach ($retmsgs as $msg) {
  245. $body = json_encode($msg);
  246. room_server::instance()->write($bufid,$body);
  247. }
  248. $broad_msgs = $room->relay_broadcast_msgs();
  249. foreach ($broad_msgs as $msg)
  250. {
  251. $body = json_encode($msg);
  252. foreach ($this->mAccConnes as $conn) {
  253. room_server::instance()->write($conn,$body);
  254. }
  255. }
  256. }
  257. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  258. private function room($roomid)
  259. {
  260. if($roomid <= 0) return false;
  261. if(array_key_exists($roomid,$this->mRooms)) {
  262. return $this->mRooms[$roomid];
  263. } else {
  264. return false;
  265. }
  266. }
  267. private function add_acc($bufid) {
  268. $connid = intval($bufid);
  269. if($connid <= 0) return false;
  270. if(!algorithm::binary_search($this->mAccConnes,$connid)) {
  271. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  272. algorithm::array_insert($this->mAccConnes,$pos,$connid);
  273. }
  274. return true;
  275. }
  276. private function remove_acc($bufid)
  277. {
  278. $connid = intval($bufid);
  279. if($connid <= 0) return false;
  280. if(algorithm::binary_search($this->mAccConnes,$connid)) {
  281. $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
  282. algorithm::array_erase($this->mAccConnes,$pos);
  283. }
  284. return true;
  285. }
  286. ////////////////////////////////返回包///////////////////////////////////////////////////////////////////////////////
  287. private function success($val)
  288. {
  289. $code = errcode::Success;
  290. $data['code'] = $code;
  291. $data['message'] = errcode::msg($code);
  292. $data['data'] = $val;
  293. $data['msgtype'] = "reply";
  294. return json_encode($data);
  295. }
  296. private function error($code,$message = '')
  297. {
  298. if (empty($message)) {
  299. $message = errcode::msg($code);
  300. }
  301. $data['code'] = $code;
  302. $data['message'] = $message;
  303. $data['data'] = null;
  304. $data['msgtype'] = "reply";
  305. return json_encode($data);
  306. }
  307. }