protected function get_score($res) { $ip_distance = AClientUtils::ip_xor($res['ip'], $this->get_local_ip()); if ($ip_distance == 0) { return 0; } elseif ($ip_distance <= 0xff) { return 1; } elseif ($ip_distance <= 0xffff) { return 2; } elseif ($ip_distance <= 0xffffff) { return 3; } else { return 4; } //return $this->get_score_from_param($ip_distance, $time_passed); }
public function process($ip, $port) { $ret = $this->connect($ip, $port); if ($ret != true) { AClientUtils::add_error("connect ip[{$ip}] port[{$port}] failed"); return self::$CONNECT_FAILED; } $ret = $this->send(); if ($ret != true) { AClientUtils::add_error("send to ip[{$ip}] port[{$port}] failed"); return self::$OTHER_FAILED; } $ret = $this->receive(); if ($ret != true) { AClientUtils::add_error("receive from ip[{$ip}] port[{$port}] failed"); return self::$OTHER_FAILED; } return self::$SUCCESS; }
public function SetConf($conf) { AClientUtils::clear_error(); $ret = $this->set_conf($conf); $this->error = AClientUtils::get_error(); return $ret; }
public function process($ip, $port) { $url = 'http://' . $ip . ':' . $port . '/' . $this->url; $conn_timeout = $this->connect_timeout; $conn_timeout = $conn_timeout < 1 ? 1 : (int) $conn_timeout; $timeout = $this->connect_timeout + $this->write_timeout + $this->read_timeout; $timeout = $timeout < 1 ? 1 : (int) $timeout; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header); if ($this->is_post) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); } curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $conn_timeout); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); $output = curl_exec($curl); if (!is_string($output)) { if (curl_errno($curl) == CURLE_COULDNT_CONNECT) { AClientUtils::add_error("connect ip[{$ip}] port[{$port}] failed"); return AClientProtocol::$CONNECT_FAILED; } else { AClientUtils::add_error(curl_error($curl)); return AClientProtocol::$OTHER_FAILED; } } $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $this->output = array('code' => $code, 'data' => $output); return AClientProtocol::$SUCCESS; }
public function process($protocol, $resources) { if (empty($resources)) { AClientUtils::add_error("no resource"); return false; } $this->res = array(); $this->add_resources($resources); if (empty($this->res)) { // AClientUtils::add_error("all resources are disabled"); $this->res = $resources; } $this->before_choose(); $count = 0; ///<连接成功的个数 $success = false; $retry = -1; $res_count = count($this->res); $delta_count = $this->service_retry + 1 - $res_count; for ($i = 0; $i < $delta_count; ++$i) { $this->res[] = $this->res[$i % $res_count]; } foreach ($this->res as $res) { $ret = $protocol->process($res['ip'], $res['port']); if ($ret == AClientProtocol::$SUCCESS) { $success = true; $this->after_chosen_resource($res); $count++; } else { //still couldn't connect, mark this resource as unusable $this->mark_unusable($res); if ($ret != AClientProtocol::$CONNECT_FAILED && !$this->always_retry) { $count++; $retry++; } } if ($this->CHOOSE_NUM > 0 && $count >= $this->CHOOSE_NUM) { $output = $protocol->get_output(); // http协议返回码小于400时不进行重试 if (!isset($output['code']) || intval($output['code']) < 400) { break; } if ($retry >= $this->service_retry) { break; } } } return $success; }
public function receive() { $sock =& $this->sock; $head = $sock->receive(self::$head_len); if (!is_string($head) || strlen($head) != self::$head_len) { return false; } $output =& $this->output; $output = $this->unpack($head); $len = $output['body_len']; if (!is_integer($len) || $len < 0 || $output['magic_num'] != $this->input['magic_num']) { AClientUtils::add_error("receive head[{$head}] error"); return false; } $body = $sock->receive($len); if (!is_string($body) || strlen($body) != $len) { return false; } $output['body'] = $body; return true; }
public static function clear_error() { self::$err_info = array(); self::$err_info['message'] = ''; }