123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午12:07
- */
- namespace room;
- use search\IProcessor;
- use errcode;
- use algorithm;
- class room_processor implements IProcessor
- {
- private $mAccConnes;
- private $mFactory;
- private $mRooms;
- public function __construct()
- {
- $this->mAccConnes = [];
- $this->mFactory = new factory();
- $this->mRooms = [];
- }
- public function onRequest($bufid, $body)
- {
- $input = json_decode($body,true);
- if($input == false) {
- room_server::instance()->close($bufid);
- return false;
- }
- $act = $input['act'];
- if(!empty($act) && $act == proto_type::act_factory) {
- $ret = $this->onFactory($bufid,$input);
- room_server::instance()->write($bufid,$ret);
- return true;
- }
- elseif(!empty($act) && $act == proto_type::act_access) {
- $ret = $this->onAccess($bufid,$input);
- return $ret;
- }
- else {
- $ret = $this->onRoom($bufid,$input);
- return $ret;
- }
- }
- public function onClose($bufid)
- {
- $this->remove_acc($bufid);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private function onFactory($bufid,$input)
- {
- $op = $input['op'];
- if($op == 'build')
- {
- $roomid = intval($input['room']);
- if($roomid <= 0) return $this->error(errcode::ErrRoomParam);
- if(array_key_exists($roomid,$this->mRooms)) {
- return $this->success(['room' => $roomid]);
- }
- $room = $this->mFactory->build($roomid);
- if($room == false) {
- return $this->error(errcode::ErrRoomBuild);
- }
- else {
- $this->mRooms[$roomid] = $room;
- return $this->success(['room' => $room->room_id()]);
- }
- }
- elseif($op == 'list_room')
- {
- return $this->success(['rooms' => $this->mRooms]);
- }
- elseif($op == 'invite')
- {
- $roomid = intval($input['room']);
- if($roomid <= 0) return $this->error(errcode::ErrRoomParam);
- if(!array_key_exists($roomid,$this->mRooms))
- {
- $room = $this->mFactory->build($roomid);
- if($room != false) {
- $this->mRooms[$roomid] = $room;
- }
- }
- else {
- $room = $this->room($roomid);
- }
- if($room != false)
- {
- $userid = intval($input['user']);
- if($userid > 0)
- {
- $room_key = $room->invite($userid);
- if($room_key != false) {
- return $this->success(['room' => $roomid,'room_key' => $room_key]);
- }
- }
- }
- return $this->error(errcode::ErrRoomInvite);
- }
- else
- {
- return $this->error(errcode::ErrRoomFactoryOp);
- }
- }
- private function onAccess($bufid,$input)
- {
- $op = $input['op'];
- if($op == 'list') {
- $this->add_acc($bufid);
- $this->write_accmsg($bufid,"list",['rooms' => $this->rooms()]);
- return true;
- }
- else {
- return $this->error(errcode::ErrRoomAccessOp);
- }
- }
- private function rooms()
- {
- $result = [];
- foreach ($this->mRooms as $roomid => $room) {
- $result[] = $roomid;
- }
- return $result;
- }
- private function onRoom($bufid,$input)
- {
- $roomid = intval($input['room']);
- $room = $this->room($roomid);
- if($room != false)
- {
- $function = $input['op'] . 'Op';
- $roomkey = $input['room_key'];
- if (method_exists($room,$function))
- {
- $success = $room->$function($input);
- if($success) {
- $this->write_roomsg($bufid,$room);
- }
- else {
- $this->del_user($bufid,$room->room_id(), [$roomkey]);
- }
- return true;
- }
- }
- return false;
- }
- private function del_user($bufid,$room_id,$roomkeys)
- {
- $msg = [];
- $msg['receivers'] = ['type' => 'roomkey', 'users' => $roomkeys];
- $msg['room'] = $room_id;
- $msg['act'] = 'room';
- $msg['op'] = 'deluser';
- $body = $this->success(['msg' => $msg]);
- room_server::instance()->write($bufid,$body);
- }
- protected function write_accmsg($bufid,$op,$content)
- {
- $msg['act'] = "access";
- $msg['op'] = $op;
- $msg['body']['act'] = 'access';
- $msg['body']['op'] = $op;
- $msg['body']['content'] = $content;
- $body = $this->success(['msg' => $msg]);
- room_server::instance()->write($bufid,$body);
- }
- private function write_roomsg($bufid,$room)
- {
- $retmsgs = $room->return_msgs();
- foreach ($retmsgs as $msg) {
- $body = $this->success(['msg' => $msg]);
- room_server::instance()->write($bufid,$body);
- }
- $broad_msgs = $room->broadcast_msgs();
- foreach ($broad_msgs as $msg)
- {
- $body = $this->success(['msg' => $msg]);
- foreach ($this->mAccConnes as $conn) {
- room_server::instance()->write($conn,$body);
- }
- }
- }
- private function room($roomid)
- {
- if($roomid <= 0) return false;
- if(array_key_exists($roomid,$this->mRooms)) {
- return $this->mRooms[$roomid];
- } else {
- return false;
- }
- }
- private function add_acc($bufid) {
- $connid = intval($bufid);
- if($connid <= 0) return false;
- if(!algorithm::binary_search($this->mAccConnes,$connid)) {
- $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
- algorithm::array_insert($this->mAccConnes,$pos,$connid);
- }
- return true;
- }
- private function remove_acc($bufid)
- {
- $connid = intval($bufid);
- if($connid <= 0) return false;
- if(algorithm::binary_search($this->mAccConnes,$connid)) {
- $pos = algorithm::lower_bonud($this->mAccConnes,$connid);
- algorithm::array_erase($this->mAccConnes,$pos);
- }
- return true;
- }
- private function success($datas)
- {
- $code = errcode::Success;
- $data['code'] = $code;
- $data['message'] = errcode::msg($code);
- $data['data'] = $datas;
- return json_encode($data);
- }
- private function error($code,$message = '')
- {
- if (empty($message)) {
- $message = errcode::msg($code);
- }
- $data = array();
- $data['code'] = $code;
- $data['message'] = $message;
- $data['datas'] = null;
- return json_encode($data);
- }
- }
|