|
@@ -10,64 +10,92 @@ namespace room;
|
|
|
|
|
|
use search\IProcessor;
|
|
|
use errcode;
|
|
|
+use algorithm;
|
|
|
|
|
|
class room_processor implements IProcessor
|
|
|
{
|
|
|
private $mAccConnes;
|
|
|
- private $mFcgiConnes;
|
|
|
private $mFactory;
|
|
|
private $mRooms;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->mAccConnes = [];
|
|
|
- $this->mFcgiConnes = [];
|
|
|
$this->mFactory = new factory();
|
|
|
$this->mRooms = [];
|
|
|
}
|
|
|
|
|
|
- public function handle_input($bufid,$body)
|
|
|
+ public function onRequest($bufid, $body)
|
|
|
{
|
|
|
$input = json_decode($body,true);
|
|
|
if($input == false) {
|
|
|
- return $this->error(['result' => false]);
|
|
|
+ room_server::instance()->close($bufid);
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
$act = $input['act'];
|
|
|
if(!empty($act) && $act == proto_type::act_factory) {
|
|
|
- return $this->onFactory($input);
|
|
|
+ $ret = $this->onFactory($bufid,$input);
|
|
|
+ room_server::instance()->write($bufid,$ret);
|
|
|
+ return true;
|
|
|
}
|
|
|
elseif(!empty($act) && $act == proto_type::act_access) {
|
|
|
- return $this->onAccess($input);
|
|
|
+ $ret = $this->onAccess($bufid,$input);
|
|
|
+ return $ret;
|
|
|
}
|
|
|
else {
|
|
|
- return $this->onRoom($input);
|
|
|
+ $ret = $this->onRoom($bufid,$input);
|
|
|
+ return $ret;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- ob_clean();
|
|
|
+ public function onClose($bufid)
|
|
|
+ {
|
|
|
+ $this->remove_acc($bufid);
|
|
|
}
|
|
|
|
|
|
- private function onFactory($input)
|
|
|
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
+ private function onFactory($bufid,$input)
|
|
|
{
|
|
|
- $err = errcode::ErrRoom;
|
|
|
$op = $input['op'];
|
|
|
- if($op == 'create')
|
|
|
+ if($op == 'build')
|
|
|
{
|
|
|
- $room = $this->mFactory->create($input);
|
|
|
- if($room != false) {
|
|
|
- $user = intval($input['creator']);
|
|
|
- $this->mRooms[$room->room_id()] = $room;
|
|
|
- $user_roomKey = $room->invite($user);
|
|
|
- if($user_roomKey != false) {
|
|
|
- $ret = ['room' => $room->room_id(),'room_key' => $user_roomKey];
|
|
|
- return $this->success($ret);
|
|
|
- }
|
|
|
+ $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()]);
|
|
|
}
|
|
|
- $err = errcode::ErrRoomCreate;
|
|
|
+ }
|
|
|
+ elseif($op == 'list_room')
|
|
|
+ {
|
|
|
+ return $this->success(['rooms' => $this->mRooms]);
|
|
|
}
|
|
|
elseif($op == 'invite')
|
|
|
{
|
|
|
$roomid = intval($input['room']);
|
|
|
- $room = $this->room($roomid);
|
|
|
+ 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']);
|
|
@@ -79,27 +107,25 @@ class room_processor implements IProcessor
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- $err = errcode::ErrRoomInvite;
|
|
|
+ return $this->error(errcode::ErrRoomInvite);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return $this->error(errcode::ErrRoomFactoryOp);
|
|
|
}
|
|
|
-
|
|
|
- return $this->error($err);
|
|
|
}
|
|
|
|
|
|
- private function onAccess($input)
|
|
|
+ private function onAccess($bufid,$input)
|
|
|
{
|
|
|
- $result['act'] = 'access';
|
|
|
-
|
|
|
$op = $input['op'];
|
|
|
- if($op == 'list')
|
|
|
- {
|
|
|
- $result['op'] = 'list';
|
|
|
- $result['rooms'] = $this->rooms();
|
|
|
+ if($op == 'list') {
|
|
|
+ $this->add_acc($bufid);
|
|
|
+ $this->write_accmsg($bufid,"list",['rooms' => $this->rooms()]);
|
|
|
+ return true;
|
|
|
}
|
|
|
else {
|
|
|
-
|
|
|
+ return $this->error(errcode::ErrRoomAccessOp);
|
|
|
}
|
|
|
-
|
|
|
- return $this->success($result);
|
|
|
}
|
|
|
|
|
|
private function rooms()
|
|
@@ -111,34 +137,72 @@ class room_processor implements IProcessor
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
- private function onRoom($input)
|
|
|
+ private function onRoom($bufid,$input)
|
|
|
{
|
|
|
- $result['act'] = 'room';
|
|
|
-
|
|
|
$roomid = intval($input['room']);
|
|
|
$room = $this->room($roomid);
|
|
|
+
|
|
|
if($room != false)
|
|
|
{
|
|
|
$function = $input['op'] . 'Op';
|
|
|
$roomkey = $input['room_key'];
|
|
|
|
|
|
- $result['room_key'] = $roomkey;
|
|
|
if (method_exists($room,$function))
|
|
|
{
|
|
|
- $msgs = $room->$function($input);
|
|
|
- if($msgs === false) {
|
|
|
- $result['op'] = 'deluser';
|
|
|
- return $this->success($result);
|
|
|
+ $success = $room->$function($input);
|
|
|
+ if($success) {
|
|
|
+ $this->write_roomsg($bufid,$room);
|
|
|
}
|
|
|
else {
|
|
|
- $result['msgs'] = $msgs;
|
|
|
- $result['op'] = 'broadcast';
|
|
|
- return $this->success($result);
|
|
|
+ $this->del_user($bufid,$room->room_id(), [$roomkey]);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
- else {
|
|
|
|
|
|
+ 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);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -152,7 +216,29 @@ class room_processor implements IProcessor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function success($datas) {
|
|
|
+ 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);
|