factory_processor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 uniquer;
  13. use Log;
  14. class factory_processor implements IProcessor
  15. {
  16. const room_connection = "room_connection";
  17. private $mFactory;
  18. private $mRoomPos;
  19. private $mAccUniquer;
  20. private $mBufidRooms;
  21. public function __construct()
  22. {
  23. $this->mFactory = new factory();
  24. $this->mRoomPos = 0;
  25. $this->mAccUniquer = new uniquer();
  26. $this->mBufidRooms = [];
  27. }
  28. public function onStart()
  29. {
  30. global $config;
  31. $room_addrs = $config['room_factory']['rooms_addr'];
  32. foreach ($room_addrs as $addr) {
  33. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  34. }
  35. }
  36. public function onConnected($bufid,$stream,$host,$port,$args)
  37. {
  38. if($args == self::room_connection) {
  39. Log::record("room srv bufid={$bufid}",Log::DEBUG);
  40. $client = new room_client($host,$port,$stream,false);
  41. $this->mBufidRooms[$bufid] = $client;
  42. $this->block($bufid);
  43. $client->init_rooms($bufid);
  44. $this->unblock($bufid);
  45. }
  46. }
  47. public function onClose($bufid)
  48. {
  49. //if access connection
  50. if($this->mAccUniquer->remove_value($bufid)) return;
  51. //if room connection
  52. if(array_key_exists($bufid,$this->mBufidRooms)) {
  53. $client = $this->mBufidRooms[$bufid];
  54. $addr = $client->host_port();
  55. unset($this->mBufidRooms[$bufid]);
  56. process_looper::instance()->connect($addr['host'],$addr['port'],self::room_connection);
  57. }
  58. }
  59. private function block($bufid) {
  60. process_looper::instance()->block($bufid);
  61. }
  62. private function unblock($bufid) {
  63. process_looper::instance()->unblock($bufid);
  64. }
  65. public function onRequest($bufid, $body)
  66. {
  67. $input = json_decode($body,true);
  68. if($input == false) {
  69. return false;
  70. }
  71. $act = $input['act'];
  72. if(empty($act)) return false;
  73. if($act == proto_type::act_fcgi) {
  74. $ret = $this->onFcgi($bufid,$input);
  75. process_looper::instance()->write($bufid,$ret);
  76. return true;
  77. }
  78. elseif($act == proto_type::act_access) {
  79. $ret = $this->onAccess($bufid,$input);
  80. process_looper::instance()->write($bufid,$ret);
  81. return true;
  82. }
  83. else {
  84. return false;
  85. }
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. private function onFcgi($bufid, $input)
  89. {
  90. $op = $input['op'];
  91. if($op == 'create')
  92. {
  93. $ret = $this->mFactory->create($input,$fnew);
  94. if($ret != false)
  95. {
  96. $invite = $input['invite'];
  97. if($invite)
  98. {
  99. $roomid = $ret['room'];
  100. $inviter = $ret['creator'];
  101. if($roomid <= 0 || $inviter <= 0) {
  102. return $this->error(errcode::ErrRoomCreate);
  103. }
  104. if($this->build($roomid,$fnew) == false) {
  105. return $this->error(errcode::ErrRoomBuild);
  106. }
  107. $result = $this->invite($roomid,$inviter,[$inviter]);
  108. if($result != false) {
  109. $ret = array_merge($ret,$result);
  110. return $this->success($ret);
  111. } else {
  112. return $this->error(errcode::ErrRoomInvite);
  113. }
  114. }
  115. else {
  116. return $this->error(errcode::ErrRoomCreate);
  117. }
  118. }
  119. return $this->error(errcode::ErrRoomCreate);
  120. }
  121. elseif($op == 'invite')
  122. {
  123. $roomid = intval($input['room']);
  124. $inviter = intval($input['inviter']);
  125. $invitees = $input['invitees'];
  126. if($roomid <= 0 || $inviter <= 0 || empty($invitees)) {
  127. return $this->error(errcode::ErrRoomInvite);
  128. }
  129. $client = $this->find_room($roomid,$bufid);
  130. if($client == false) {
  131. $this->build($roomid);
  132. }
  133. $ret = $this->invite($roomid,$inviter,$invitees);
  134. if($ret != false) {
  135. return $this->success($ret);
  136. } else {
  137. return $this->error(errcode::ErrRoomInvite);
  138. }
  139. }
  140. elseif($op == 'change')
  141. {
  142. $roomid = intval($input['room']);
  143. if($roomid <= 0) {
  144. return $this->error(errcode::ErrRoomChange);
  145. }
  146. $client = $this->find_room($roomid,$bufid);
  147. if($client == false) {
  148. $this->build($roomid);
  149. }
  150. $ret = $this->change($roomid);
  151. if($ret != false) {
  152. return $this->success($ret);
  153. } else {
  154. return $this->error(errcode::ErrRoomChange);
  155. }
  156. }
  157. elseif($op == 'leave')
  158. {
  159. $roomid = intval($input['room']);
  160. $user = intval($input['user']);
  161. if($roomid <= 0 || $user <= 0) {
  162. return $this->error(errcode::ErrRoomLeave);
  163. }
  164. $client = $this->find_room($roomid,$bufid);
  165. if($client == false) {
  166. $this->build($roomid);
  167. }
  168. $ret = $this->leave($roomid,$user);
  169. if($ret != false) {
  170. return $this->success($ret);
  171. } else {
  172. return $this->error(errcode::ErrRoomLeave);
  173. }
  174. }
  175. elseif($op == 'kickout')
  176. {
  177. $roomid = intval($input['room']);
  178. $user = intval($input['user']);
  179. $kicks = $input['kicks'];
  180. if($roomid <= 0 || $user <= 0) {
  181. return $this->error(errcode::ErrRoomKickout);
  182. }
  183. $client = $this->find_room($roomid,$bufid);
  184. if($client == false) {
  185. $this->build($roomid);
  186. }
  187. $ret = $this->kickout($roomid,$user,$kicks);
  188. if($ret != false) {
  189. return $this->success($ret);
  190. } else {
  191. return $this->error(errcode::ErrRoomKickout);
  192. }
  193. }
  194. elseif($op == 'push')
  195. {
  196. $content = $input['content'];
  197. $ret = $this->push($content);
  198. if($ret != false) {
  199. return $this->success(NULL);
  200. } else {
  201. return $this->error(errcode::ErrRoomPush);
  202. }
  203. }
  204. else {
  205. return $this->error(errcode::ErrRoomFactoryOp);
  206. }
  207. }
  208. private function onAccess($bufid,$input)
  209. {
  210. $op = $input['op'];
  211. if($op == 'who')
  212. {
  213. $this->mAccUniquer->add_value($bufid);
  214. return $this->success(['act' => proto_type::act_access,'op' => 'who']);
  215. }
  216. elseif($op == 'build')
  217. {
  218. $roomid = intval($input['room']);
  219. $client = $this->find_room($roomid,$bufid);
  220. if($client == false) {
  221. $this->build($roomid);
  222. }
  223. return $this->success(['act' => proto_type::act_access,'op' => $op,'room' => $roomid]);
  224. }
  225. else {
  226. return $this->error(errcode::ErrParamter);
  227. }
  228. }
  229. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  230. private function find_room($roomid,&$outbufid)
  231. {
  232. foreach ($this->mBufidRooms as $bufid => $client)
  233. {
  234. if($client->contain_room($roomid)) {
  235. $outbufid = $bufid;
  236. return $client;
  237. }
  238. }
  239. return false;
  240. }
  241. private function build($roomid,$newroom = false)
  242. {
  243. $client = $this->find_room($roomid,$bufid);
  244. if($client != false) {
  245. $this->block($bufid);
  246. $ret = $client->build($roomid,$newroom);
  247. $this->unblock($bufid);
  248. return $ret;
  249. }
  250. $bufid = $this->room_bufid();
  251. if($bufid != false)
  252. {
  253. $client = $this->mBufidRooms[$bufid];
  254. $this->block($bufid);
  255. $ret = $client->build($roomid,$newroom);
  256. $this->unblock($bufid);
  257. if($ret != false) {
  258. $client->add_room($roomid);
  259. }
  260. return $ret;
  261. }
  262. else {
  263. return false;
  264. }
  265. }
  266. private function invite($roomid, $inviter,$invitees)
  267. {
  268. $client = $this->find_room($roomid,$bufid);
  269. if($client != false) {
  270. $this->block($bufid);
  271. $ret = $client->invite($roomid,$inviter,$invitees);
  272. $this->unblock($bufid);
  273. return $ret;
  274. }
  275. return false;
  276. }
  277. private function change($roomid)
  278. {
  279. $client = $this->find_room($roomid,$bufid);
  280. if($client != false) {
  281. $this->block($bufid);
  282. $ret = $client->change($roomid);
  283. $this->unblock($bufid);
  284. return $ret;
  285. }
  286. return false;
  287. }
  288. private function leave($roomid, $user)
  289. {
  290. foreach ($this->mBufidRooms as $bufid => $client)
  291. {
  292. if($client->contain_room($roomid))
  293. {
  294. $this->block($bufid);
  295. $ret = $client->leave($roomid,$user);
  296. $this->unblock($bufid);
  297. return $ret;
  298. }
  299. }
  300. }
  301. private function kickout($roomid, $user,$kicks)
  302. {
  303. foreach ($this->mBufidRooms as $bufid => $client)
  304. {
  305. if($client->contain_room($roomid))
  306. {
  307. $this->block($bufid);
  308. $ret = $client->kickout($roomid,$user,$kicks);
  309. $this->unblock($bufid);
  310. return $ret;
  311. }
  312. }
  313. }
  314. private function push($content)
  315. {
  316. $bufid = $this->room_bufid();
  317. if($bufid != false)
  318. {
  319. $client = $this->mBufidRooms[$bufid];
  320. $this->block($bufid);
  321. $ret = $client->push($content);
  322. $this->unblock($bufid);
  323. return true;
  324. }
  325. return false;
  326. }
  327. private function room_bufid()
  328. {
  329. $count = count($this->mBufidRooms);
  330. if($count <= 0) return false;
  331. $pos = $this->mRoomPos % $count;
  332. $i = 0;
  333. foreach ($this->mBufidRooms as $bufid => $client)
  334. {
  335. if($i == $pos) break;
  336. $pos++;
  337. }
  338. $this->mRoomPos++;
  339. Log::record("select bufid = {$bufid}",Log::DEBUG);
  340. return $bufid;
  341. }
  342. private function success($datas)
  343. {
  344. $code = errcode::Success;
  345. $data['code'] = $code;
  346. $data['message'] = errcode::msg($code);
  347. $data['data'] = $datas;
  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['datas'] = null;
  359. $data['msgtype'] = "reply";
  360. return json_encode($data);
  361. }
  362. }