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 onHttpFinished($serv, $task_id, $data, $response) { $fd = $data["fd"]; $guid = $data["guid"]; //if the guid not exists .it's mean the api no need return result if (!isset($this->taskInfo[$fd][$guid])) { return true; } //get the api key $key = $this->taskInfo[$fd][$guid]["taskkey"][$task_id]; //save the result $this->taskInfo[$fd][$guid]["result"][$key] = $data["result"]; //remove the used taskid unset($this->taskInfo[$fd][$guid]["taskkey"][$task_id]); switch ($data["type"]) { case DoraConst::SW_MODE_WAITRESULT_MULTI: //all task finished if (count($this->taskInfo[$fd][$guid]["taskkey"]) == 0) { $packet = Packet::packFormat("OK", 0, $this->taskInfo[$fd][$guid]["result"]); $packet["guid"] = $guid; $packet = Packet::packEncode($packet, $data["protocol"]); unset($this->taskInfo[$fd][$guid]); $response->end($packet); return true; } else { //multi call task //not finished //waiting other result return true; } break; default: return true; break; } }
public final function onFinish($serv, $task_id, $data) { //fixed the result more than 8k timeout bug if (strpos($data, '$$$$$$$$') === 0) { $tmp_path = substr($data, 8); $data = file_get_contents($tmp_path); unlink($tmp_path); } $data = unserialize($data); $fd = $data["fd"]; if (!isset($this->taskInfo[$fd]) || !$data["result"]) { unset($this->taskInfo[$fd]); return true; } $key = $this->taskInfo[$fd]["task"][$task_id]; $this->taskInfo[$fd]["result"][$key] = $data["result"]; unset($this->taskInfo[$fd]["task"][$task_id]); switch ($data["type"]) { case DoraConst::SW_SYNC_SINGLE: $Packet = Packet::packFormat("OK", 0, $data["result"]); $Packet["guid"] = $this->taskInfo[$fd]["guid"]; $Packet = Packet::packEncode($Packet); //sys_get_temp_dir $serv->send($fd, $Packet); unset($this->taskInfo[$fd]); return true; break; case DoraConst::SW_SYNC_MULTI: if (count($this->taskInfo[$fd]["task"]) == 0) { $Packet = Packet::packFormat("OK", 0, $this->taskInfo[$fd]["result"]); $Packet["guid"] = $this->taskInfo[$fd]["guid"]; $Packet = Packet::packEncode($Packet); $serv->send($fd, $Packet); unset($this->taskInfo[$fd]); return true; } else { return true; } break; default: unset($this->taskInfo[$fd]); return true; break; } }