public function onRequest($request, $response) { //统一进行路由和数据的预处理 $req = HttpHelper::httpReqHandle($request); SysLog::info(__METHOD__ . print_r($req, true), __CLASS__); if ($req['r'] === HttpHelper::HTTP_ERROR_URI) { $response->status(404); //todo:log $response->end("not found"); return; } SysLog::info(__METHOD__ . ' ' . __LINE__ . " REQUEST IS " . print_r($req, true), __CLASS__); $class = $req['route']['controller'] . 'Controller'; $fun = 'action' . $req['route']['action']; //判断类是否存在 if (!class_exists($class) || !method_exists($class, $fun)) { $response->status(404); SysLog::error(__METHOD__ . " class or fun not found class == {$class} fun == {$fun}", __CLASS__); $response->end("uri not found"); return; } $obj = new $class($this->server, array('request' => $req['request'], 'response' => $response), $request->fd); //代入参数 $request->scheduler->newTask($obj->{$fun}()); $request->scheduler->run(); }
public function onReceive($server, $clientId, $fromId, $data) { $data = Yaaf::unpackAll($data); SysLog::notice(__METHOD__ . " fd = {$clientId} fromId = {$fromId} data = " . print_r($data, true), __CLASS__); $info = $server->connection_info($clientId, $fromId); //yaaf 协议 路由 $req = YaafHelper::yaafReqHandle($data); SysLog::info(__METHOD__ . print_r($req, true), __CLASS__); //路由失败 直接返回错误 if ($req['r'] === YaafHelper::YAAF_ERROR_CMD) { //todo 协议搞成yaaf $yaaf_data = Yaaf::packHeader() . Yaaf::packBody(array('errCode' => -1, 'errMsg' => 'not found class')); $server->sendto($info['remote_ip'], $info['remote_port'], $yaaf_data); return; } $class = $req['route']['controller'] . 'Controller'; $fun = 'action' . $req['route']['action']; //判断类是否存在 if (!class_exists($class) || !method_exists($class, $fun)) { SysLog::error(__METHOD__ . print_r($req, true), __CLASS__); $yaaf_data = Yaaf::packHeader() . Yaaf::packBody(array('errCode' => -1, 'errMsg' => 'not found class')); $server->sendto($info['remote_ip'], $info['remote_port'], $yaaf_data); return; } $obj = new $class($this->server, array('request' => $data, 'info' => $info), $clientId); //代入参数 $server->scheduler->newTask($obj->doFun($fun)); $server->scheduler->run(); }
/** * [init 启动定时器] * @return [type] [description] */ public static function init() { if (!isset(self::$tickKey)) { self::$tickKey = swoole_timer_tick(1000 * self::LOOPTIME, function () { //循环数组,踢出超时情况 self::loop(); }); \SysLog::info(__METHOD__ . " init timer tick key == " . self::$tickKey, __CLASS__); } }
/** * [packRsp 回调函数,收包,合包,回调] * @param [type] $r [description] * @param [type] $client_key [description] * @param [type] $data [description] * @return [type] [description] */ public function recv($r, $client_key, $calltime, $data) { \SysLog::info(__METHOD__ . " r = {$r} client_key = {$client_key} callList = " . count($this->callRsp), __CLASS__); $this->callRsp[$client_key] = array('r' => $r, 'calltime' => $calltime, 'data' => $data); //收包完成 if (count($this->callRsp) == count($this->callList)) { \SysLog::info(__METHOD__ . " get all the rsp ==== " . print_r($this->callRsp, true), __CLASS__); $this->calltime = microtime(true) - $this->calltime; call_user_func_array($this->callback, array('r' => $r, 'key' => '', 'calltime' => $calltime, 'data' => $this->callRsp)); } }
/** * 处理request对象 * @param req swoole http server 获得的request对象 */ public static function httpReqHandle($req) { $method = $req->server['request_method']; $uri = $req->server['request_uri']; //路由 $appRoute = HttpRoute::urlrouter_rewrite($uri, $method); SysLog::info(__METHOD__ . print_r($appRoute, true), __CLASS__); if (!$appRoute) { return array('r' => self::HTTP_ERROR_URI); } return array('r' => self::HTTP_OK, 'route' => $appRoute, 'request' => array('uri' => $uri, 'header' => $req->header, 'get' => array_merge((array) (isset($req->get) ? $req->get : array()), (array) $appRoute['get']), 'post' => isset($req->post) ? $req->post : '', 'files' => isset($req->files) ? $req->files : '', 'cookie' => isset($req->cookie) ? $req->cookie : '', 'rawcontent' => $req->rawContent(), 'method' => $method)); }
/** * 处理request对象 * @param req swoole yaaf server 获得的request对象 */ public static function yaafReqHandle($req) { $cmd = $req['reqHead']['cmd']; //路由 $appRoute = YaafRoute::getRoute($cmd); SysLog::info(__METHOD__ . print_r($appRoute, true), __CLASS__); if (!$appRoute) { return array('r' => self::YAAF_ERROR_CMD); } //这里考虑以后根据appName路由到后端svr yaafUdpServ作为代理 分发流量 return array('r' => self::YAAF_OK, 'route' => $appRoute, 'request' => array('reqHead' => $req['reqHead'], 'reqBody' => $req['reqBody'])); }
private function test() { $test = new TestModel(); $res = (yield $test->MysqlMuticallTest()); SysLog::info(__METHOD__ . " res == " . print_r($res, true), __CLASS__); if ($res['r'] == 0) { //yield success SysLog::info(__METHOD__ . " yield success data == " . print_r($res['data'], true), __CLASS__); (yield $res); } else { //yield failed SysLog::error(__METHOD__ . " yield failed res == " . print_r($res, true), __CLASS__); (yield array('r' => 1, 'error_msg' => 'yield failed')); } }
/** * 处理request对象 * @param req swoole http server 获得的request对象 */ public static function httpReqHandle($req) { $method = $req->server['request_method']; $uri = $req->server['request_uri']; //正则匹配的路由 支持restful 提供给深度用户使用 // $appRoute = HttpRoute::urlrouter_rewrite($uri,$method); // explode 解析类似于 controller/action类型的url //默认会解析到default/index $mvcArr = explode('/', $uri); $appRoute['controller'] = isset($mvcArr[1]) ? $mvcArr[1] : 'default'; $appRoute['action'] = isset($mvcArr[2]) ? $mvcArr[2] : 'index'; SysLog::info(__METHOD__ . print_r($appRoute, true), __CLASS__); if (!$appRoute) { return array('r' => self::HTTP_ERROR_URI); } return array('r' => self::HTTP_OK, 'route' => $appRoute, 'request' => array('uri' => $uri, 'header' => $req->header, 'get' => array_merge((array) (isset($req->get) ? $req->get : array()), (array) $appRoute['get']), 'post' => isset($req->post) ? $req->post : '', 'files' => isset($req->files) ? $req->files : '', 'cookie' => isset($req->cookie) ? $req->cookie : '', 'rawcontent' => $req->rawContent(), 'method' => $method)); }
public static function loop() { \SysLog::info(__METHOD__, __CLASS__); /* 遍历自己的数组,发现时间超过预定时间段,且该IO的状态依然是未回包状态,则走超时逻辑 */ foreach (self::$event as $socket => $e) { $now = microtime(true); \SysLog::debug(__METHOD__ . " key == {$socket} now == {$now} timeout == " . $e['timeout'], __CLASS__); if ($now > $e['timeout']) { self::del($socket); $cli = $e['cli']; $cli->close(); call_user_func_array($e['callback'], $e['params']); } } }
/** * [unPackRsp 反解回包数据] * @param [type] $r [description] * @param [type] $k [description] * @param [type] $data [description] * @return [type] [description] */ public function unPackRsp($r, $k, $calltime, $data) { /* 根据yeaf格式解析回包数据 回调给协程的callback函数,反回数据到调用端 */ \SysLog::info(__METHOD__ . " udp test client rsp ==== " . print_r($data, true), __CLASS__); if ($r != 0) { $result = array('r' => self::ERROR_SENDRECV); } else { $rspHeadBuf = substr($data, 0, self::HEADLEN); //echo "rspHeadBuf === $rspHeadBuf \n"; $rspBodyBuf = substr($data, self::HEADLEN); //echo "rspBodyBuf === $rspBodyBuf \n"; $result = $this->getResult($rspHeadBuf, $rspBodyBuf); //echo " unPackRsp result == ". print_r($result,true) . PHP_EOL; } call_user_func_array($this->callback, array('r' => 0, 'key' => $this->key, 'calltime' => $calltime, 'data' => $result)); }
private function parseHeader($data) { $parts = explode("\r\n\r\n", $data, 2); $headerLines = explode("\r\n", $parts[0]); list($this->rspHeaders['method'], $this->rspHeaders['uri'], $this->rspHeaders['protocol']) = explode(' ', $headerLines[0], 3); $this->respHeader = \Swoole\Http\Parser::parseHeaderLine($headerLines); if (isset($parts[1])) { $this->content = $parts[1]; } //print_r($this ->respHeader); \SysLog::info(__METHOD__ . " header == " . print_r($this->respHeader, true), __CLASS__); }
/** * [callback description] * @param [type] $r [description] * @param [type] $key [description] * @param [type] $calltime [description] * @param [type] $res [description] * @return function [description] */ public function callback($r, $key, $calltime, $res) { /* 继续run的函数实现 ,栈结构得到保存 */ $gen = $this->corStack->pop(); $this->callbackData = array('r' => $r, 'calltime' => $calltime, 'data' => $res); \SysLog::info(__METHOD__ . " corStack pop and data == " . print_r($this->callbackData, true), __CLASS__); $value = $gen->send($this->callbackData); $this->run($gen); }