public function getAsyncData() { //wait all the async result //when timeout all the error will return //这里有个坑,我不知道具体哪个client需要recive while (1) { if (count(self::$asynclist) > 0) { foreach (self::$asynclist as $k => $client) { if ($client->isConnected()) { $data = $client->recv(); if ($data !== false && $data != "") { $data = Packet::packDecode($data); if (isset(self::$asynclist[$data["data"]["guid"]]) && isset($data["data"]["isresult"]) && $data["data"]["isresult"] == 1) { //ok recive an async result //remove the guid on the asynclist unset(self::$asynclist[$data["data"]["guid"]]); //add result to async result self::$asynresult[$data["data"]["guid"]] = $data["data"]; self::$asynresult[$data["data"]["guid"]]["fromwait"] = 0; continue; } else { //not in the asynclist drop this packet continue; } } else { //remove the result unset(self::$asynclist[$k]); self::$asynresult[$k] = Packet::packFormat("the recive wrong or timeout", 100009); continue; } } else { //remove the result unset(self::$asynclist[$k]); self::$asynresult[$k] = Packet::packFormat("Get Async Result Fail: Client Closed.", 100012); continue; } } // foreach the list } else { break; } } //while $result = self::$asynresult; self::$asynresult = array(); return Packet::packFormat("OK", 0, $result); }
private function doRequest($sendData, $group = "group1", $ip = "", $port = "") { //get client obj try { $client = $this->getClientObj($group, $ip, $port); } catch (\Exception $e) { return Packet::packFormat($e->getMessage(), $e->getCode()); } $ret = $client->send($sendData); //ok fail if (!$ret) { $errorcode = $client->errCode; //destroy error client obj to make reconncet unset(self::$client[$this->currentClientKey]); if ($errorcode == 0) { $msg = "connect fail.check host dns."; $errorcode = -1; $Packet = Packet::packFormat($msg, $errorcode); } else { $msg = socket_strerror($errorcode); $Packet = Packet::packFormat($msg, $errorcode); } return $Packet; } //recive the response $result = $client->recv(); //recive error check if ($result !== false) { return Packet::packDecode($result); } else { return Packet::packFormat("the recive wrong or timeout", 100009); } }
public final function onReceive(\swoole_server $serv, $fd, $from_id, $data) { $requestInfo = Packet::packDecode($data); #decode error if ($requestInfo["code"] != 0) { $pack["guid"] = $requestInfo["guid"]; $req = Packet::packEncode($requestInfo); $serv->send($fd, $req); return true; } else { $requestInfo = $requestInfo["data"]; } #api was not set will fail if (!is_array($requestInfo["api"]) && count($requestInfo["api"])) { $pack = Packet::packFormat("param api is empty", 100003); $pack["guid"] = $requestInfo["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; } $guid = $requestInfo["guid"]; //prepare the task parameter $task = array("type" => $requestInfo["type"], "guid" => $requestInfo["guid"], "fd" => $fd, "protocol" => "tcp"); //different task type process switch ($requestInfo["type"]) { case DoraConst::SW_MODE_WAITRESULT_SINGLE: $task["api"] = $requestInfo["api"]["one"]; $taskid = $serv->task($task); //result with task key $this->taskInfo[$fd][$guid]["taskkey"][$taskid] = "one"; return true; break; case DoraConst::SW_MODE_NORESULT_SINGLE: $task["api"] = $requestInfo["api"]["one"]; $serv->task($task); //return success deploy $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; break; case DoraConst::SW_MODE_WAITRESULT_MULTI: foreach ($requestInfo["api"] as $k => $v) { $task["api"] = $requestInfo["api"][$k]; $taskid = $serv->task($task); $this->taskInfo[$fd][$guid]["taskkey"][$taskid] = $k; } return true; break; case DoraConst::SW_MODE_NORESULT_MULTI: foreach ($requestInfo["api"] as $k => $v) { $task["api"] = $requestInfo["api"][$k]; $serv->task($task); } $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; break; case DoraConst::SW_CONTROL_CMD: switch ($requestInfo["api"]["cmd"]["name"]) { case "getStat": $pack = Packet::packFormat("OK", 0, array("server" => $serv->stats())); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; break; case "reloadTask": $pack = Packet::packFormat("OK", 0, array("server" => $serv->stats())); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); $serv->reload(true); return true; break; default: $pack = Packet::packFormat("unknow cmd", 100011); $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); break; } break; case DoraConst::SW_MODE_ASYNCRESULT_SINGLE: $task["api"] = $requestInfo["api"]["one"]; $taskid = $serv->task($task); $this->taskInfo[$fd][$guid]["taskkey"][$taskid] = "one"; //return success $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; break; case DoraConst::SW_MODE_ASYNCRESULT_MULTI: foreach ($requestInfo["api"] as $k => $v) { $task["api"] = $requestInfo["api"][$k]; $taskid = $serv->task($task); $this->taskInfo[$fd][$guid]["taskkey"][$taskid] = $k; } //return success $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); break; default: $pack = Packet::packFormat("unknow task type.未知类型任务", 100002); $pack = Packet::packEncode($pack); $serv->send($fd, $pack); //unset($this->taskInfo[$fd]); return true; } return true; }
public final function onReceive(\swoole_server $serv, $fd, $from_id, $data) { $reqa = Packet::packDecode($data); #decode error if ($reqa["code"] != 0) { $req = Packet::packEncode($reqa); $serv->send($fd, $req); return true; } else { $req = $reqa["data"]; } #api not set if (!is_array($req["api"]) && count($req["api"])) { $pack = Packet::packFormat("param api is empty", 100003); $pack["guid"] = $req["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); return true; } $this->taskInfo[$fd] = $req; $task = array("type" => $this->taskInfo[$fd]["type"], "guid" => $this->taskInfo[$fd]["guid"], "fd" => $fd); switch ($this->taskInfo[$fd]["type"]) { case DoraConst::SW_SYNC_SINGLE: $task["api"] = $this->taskInfo[$fd]["api"]["one"]; $taskid = $serv->task($task); $this->taskInfo[$fd]["task"][$taskid] = "one"; return true; break; case DoraConst::SW_ASYNC_SINGLE: $task["api"] = $this->taskInfo[$fd]["api"]["one"]; $serv->task($task); $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); return true; break; case DoraConst::SW_SYNC_MULTI: foreach ($req["api"] as $k => $v) { $task["api"] = $this->taskInfo[$fd]["api"][$k]; $taskid = $serv->task($task); $this->taskInfo[$fd]["task"][$taskid] = $k; } return true; break; case DoraConst::SW_ASYNC_MULTI: foreach ($req["api"] as $k => $v) { $task["api"] = $this->taskInfo[$fd]["api"][$k]; $serv->task($task); } $pack = Packet::packFormat("transfer success.已经成功投递", 100001); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); return true; break; case DoraConst::SW_CONTROL_CMD: if ($this->taskInfo[$fd]["api"]["cmd"]["name"] == "getStat") { $pack = Packet::packFormat("OK", 0, array("server" => $serv->stats())); $pack["guid"] = $task["guid"]; $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); return true; } //no one process $pack = Packet::packFormat("unknow cmd", 100011); $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); break; default: $pack = Packet::packFormat("unknow task type.未知类型任务", 100002); $pack = Packet::packEncode($pack); $serv->send($fd, $pack); unset($this->taskInfo[$fd]); return true; } return true; }