/** * @param TcpConnection $connection * @param $data */ function onMessage($connection, $data) { //记录进入数据 MyLog::debug("[IN] [LEN] %s [USER_ID] %s [CMD] %x [body] %s ", $data['pack_len'], $data['user_id'], $data['cmd'], json_encode($data['body'])); //根据cmd进行路由 \Workerman\route\CrouteFunc::deal($data, $response); //记录发出数据 MyLog::debug("[OUT] [USER_ID] %s [CMD] 0x%08x [RET] %s [body] %s ", $response['user_id'], $response['cmd'], $response['return_code'], json_encode($response['body'])); //返回数据 $connection->send($response); }
public static function deal(&$in, &$out) { try { //设置out的cmd $out['cmd'] = Helper::getIncomeParam($in, 'cmd'); $out['user_id'] = Helper::getIncomeParam($in, 'user_id'); $out['body'] = ''; $out['return_code'] = Errors::SUCCESS; //获取cmd映射的函数 $func = self::getFunc($in['cmd']); //验证协议是否合法 Verify::validProtocol($in); //调用router 处理协议 $body = call_user_func(array(self::getRouter(), $func), $in); $out['body'] = $body; } catch (UserException $ue) { MyLog::warn("Exception: Line %s Message ", $ue->getLine(), $ue->getMessage()); //出错了,获取异常的code $out['return_code'] = $ue->getCode(); } }
/** * 执行 * @param string $query * @param string $parameters */ protected function execute($query, $parameters = "") { try { MyLog::debug("[SQL] %s [DATA] %s", $this->lastSql, json_encode($parameters)); $this->sQuery = $this->pdo->prepare($query); $this->bindMore($parameters); if (!empty($this->parameters)) { foreach ($this->parameters as $param) { $parameters = explode("", $param); $this->sQuery->bindParam($parameters[0], $parameters[1]); } } $this->succes = $this->sQuery->execute(); } catch (\PDOException $e) { // 服务端断开时重连一次 if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) { $this->closeConnection(); $this->connect(); try { $this->sQuery = $this->pdo->prepare($query); $this->bindMore($parameters); if (!empty($this->parameters)) { foreach ($this->parameters as $param) { $parameters = explode("", $param); $this->sQuery->bindParam($parameters[0], $parameters[1]); } } $this->succes = $this->sQuery->execute(); } catch (\PDOException $ex) { $this->rollBackTrans(); throw $ex; } } else { $this->rollBackTrans(); throw $e; } } $this->parameters = array(); }