123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace loyalsoft;
- /**
- * Description of Resp
- * 服务端消息应答实体
- * @author gwang
- */
- class Resp extends Object_ext {
- /**
- * 错误码[成功为0]
- * @var int
- */
- public $err;
- /**
- * @var int 请求的SN带回,更方便对号入座.
- */
- public $SN;
- /**
- * 执行结果
- * @var Object
- */
- public $result;
- /**
- * 时间戳
- * @var int
- */
- public $ts;
- /**
- * 事件列表
- * @var array/list
- */
- // public $events = array();
- /**
- * 标签[扩展用,保留]
- * @var object
- * @deprecated since version 0 弃用
- */
- // public $tag;
- /**
- * 附加错误信息, err!=0时赋值
- * @var string
- */
- public $errmsg = "";
- /**
- * @var int data version on server
- */
- public $DN = -1;
- /**
- * 产生服务端成功应答数据
- * @param Req $req
- * @param assoc Array $ret
- * @return \Resp
- */
- public static function ok($ret = null) {
- $resp = new Resp();
- $resp->err = ErrCode::ok;
- $resp->result = $ret == null ? ObjectInit() : $ret; # 避免出现null
- $resp->ts = time();
- // $resp->tag = ObjectInit();
- $resp->SN = req()->SN;
- $resp->DN = isset(req()->game->stVer) ? req()->game->stVer : -1;
- return $resp;
- }
- /**
- * 产生服务端错误应答数据
- * @param Req $req
- * @param int $err
- * @param string $msg 【可选】附加信息
- * @return \Resp
- */
- public static function err($err, $msg = "") {
- $resp = new Resp();
- $resp->err = $err;
- $resp->result = ObjectInit();
- $resp->ts = time();
- $resp->errmsg = $msg;
- // $resp->tag = ObjectInit();
- // self::addTag('errmsg', $msg);
- $resp->SN = req()->SN;
- return $resp;
- }
- /**
- * 实例方法, 将静态变量上累积的数据转移到自己身上
- */
- public function AfterProc() {
- // $this->tag = arr2obj(array_merge((array) $this->tag, (array) self::$ext_tag)); # 合并附加tags
- // self::$ext_tag = ObjectInit();
- $this->events = self::$ext_events; # 合并附加events
- self::$ext_events = array();
- if (count($this->events) > 0) { # 对于出现event的情况下自动附带store到客户端.
- if (is_array($this->result)) {
- if (!array_key_exists('task', (array) $this->result)) {
- //$this->result['task'] = ctx()->task;
- }
- } else {
- if (!array_key_exists('task', (array) $this->result)) {
- //$this->result->task = ctx()->task;
- }
- }
- }
- if (count(CRedisUtil::$caller_counter) > 0) {
- // echoLine("Redis api called Number: " . count(CRedisUtil::$caller_counter));
- // echoLine(implode("<br/>", CRedisUtil::$caller_counter));
- }
- }
- // <editor-fold defaultstate="collapsed" desc="附加event带回">
- static $ext_events = array();
- public static function AddEvent($name, $arg1, $arg2) {
- self::$ext_events[] = array(
- 'name' => $name,
- 'arg1' => is_null($arg1) ? ObjectInit() : $arg1,
- 'arg2' => is_null($arg2) ? ObjectInit() : $arg2
- );
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="附加Tags带回">
- // static $ext_tag = null;
- //
- // /**
- // * 向返回值添加附加数据
- // * @param string $name
- // * @param any $value
- // */
- // public static function addTag($name, $value) {
- // if (null == self::$ext_tag) {
- // self::$ext_tag = ObjectInit();
- // }
- // self::$ext_tag->$name = $value;
- // }
- // </editor-fold>
- }
|