Resp.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of Resp
  5. * 服务端消息应答实体
  6. * @author gwang
  7. */
  8. class Resp extends Object_ext {
  9. /**
  10. * 错误码[成功为0]
  11. * @var int
  12. */
  13. public $err;
  14. /**
  15. * @var int 请求的SN带回,更方便对号入座.
  16. */
  17. public $SN;
  18. /**
  19. * 执行结果
  20. * @var Object
  21. */
  22. public $result;
  23. /**
  24. * 时间戳
  25. * @var int
  26. */
  27. public $ts;
  28. /**
  29. * 事件列表
  30. * @var array/list
  31. */
  32. // public $events = array();
  33. /**
  34. * 标签[扩展用,保留]
  35. * @var object
  36. * @deprecated since version 0 弃用
  37. */
  38. // public $tag;
  39. /**
  40. * 附加错误信息, err!=0时赋值
  41. * @var string
  42. */
  43. public $errmsg = "";
  44. /**
  45. * @var int data version on server
  46. */
  47. public $DN = -1;
  48. /**
  49. * 产生服务端成功应答数据
  50. * @param Req $req
  51. * @param assoc Array $ret
  52. * @return \Resp
  53. */
  54. public static function ok($ret = null) {
  55. $resp = new Resp();
  56. $resp->err = ErrCode::ok;
  57. $resp->result = $ret == null ? ObjectInit() : $ret; # 避免出现null
  58. $resp->ts = time();
  59. // $resp->tag = ObjectInit();
  60. $resp->SN = req()->SN;
  61. $resp->DN = isset(req()->game->stVer) ? req()->game->stVer : -1;
  62. return $resp;
  63. }
  64. /**
  65. * 产生服务端错误应答数据
  66. * @param Req $req
  67. * @param int $err
  68. * @param string $msg 【可选】附加信息
  69. * @return \Resp
  70. */
  71. public static function err($err, $msg = "") {
  72. $resp = new Resp();
  73. $resp->err = $err;
  74. $resp->result = ObjectInit();
  75. $resp->ts = time();
  76. $resp->errmsg = $msg;
  77. // $resp->tag = ObjectInit();
  78. // self::addTag('errmsg', $msg);
  79. $resp->SN = req()->SN;
  80. return $resp;
  81. }
  82. /**
  83. * 实例方法, 将静态变量上累积的数据转移到自己身上
  84. */
  85. public function AfterProc() {
  86. // $this->tag = arr2obj(array_merge((array) $this->tag, (array) self::$ext_tag)); # 合并附加tags
  87. // self::$ext_tag = ObjectInit();
  88. $this->events = self::$ext_events; # 合并附加events
  89. self::$ext_events = array();
  90. if (count($this->events) > 0) { # 对于出现event的情况下自动附带store到客户端.
  91. if (is_array($this->result)) {
  92. if (!array_key_exists('task', (array) $this->result)) {
  93. //$this->result['task'] = ctx()->task;
  94. }
  95. } else {
  96. if (!array_key_exists('task', (array) $this->result)) {
  97. //$this->result->task = ctx()->task;
  98. }
  99. }
  100. }
  101. if (count(CRedisUtil::$caller_counter) > 0) {
  102. // echoLine("Redis api called Number: " . count(CRedisUtil::$caller_counter));
  103. // echoLine(implode("<br/>", CRedisUtil::$caller_counter));
  104. }
  105. }
  106. // <editor-fold defaultstate="collapsed" desc="附加event带回">
  107. static $ext_events = array();
  108. public static function AddEvent($name, $arg1, $arg2) {
  109. self::$ext_events[] = array(
  110. 'name' => $name,
  111. 'arg1' => is_null($arg1) ? ObjectInit() : $arg1,
  112. 'arg2' => is_null($arg2) ? ObjectInit() : $arg2
  113. );
  114. }
  115. // </editor-fold>
  116. // <editor-fold defaultstate="collapsed" desc="附加Tags带回">
  117. // static $ext_tag = null;
  118. //
  119. // /**
  120. // * 向返回值添加附加数据
  121. // * @param string $name
  122. // * @param any $value
  123. // */
  124. // public static function addTag($name, $value) {
  125. // if (null == self::$ext_tag) {
  126. // self::$ext_tag = ObjectInit();
  127. // }
  128. // self::$ext_tag->$name = $value;
  129. // }
  130. // </editor-fold>
  131. }