示例#1
0
 /**
  * 连接到服务器
  * @param SOA_Result $retObj
  * @return bool
  * @throws \Exception
  */
 protected function connectToServer(SOA_Result $retObj)
 {
     $ret = false;
     //循环连接
     while (count($this->servers) > 0) {
         $svr = $this->getServer();
         if (!$this->useSwoole) {
             $socket = new TCP();
             $socket->try_reconnect = false;
             $ret = $socket->connect($svr['host'], $svr['port'], $this->timeout);
             $socketFd = intval($socket->get_socket());
         } else {
             $key = $svr['host'] . ':' . $svr['port'] . '-' . $retObj->index;
             $socket = new \swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP, SWOOLE_SOCK_SYNC, $key);
             $socket->set(array('open_length_check' => true, 'package_max_length' => $this->packet_maxlen, 'package_length_type' => 'N', 'package_body_offset' => SOAServer::HEADER_SIZE, 'package_length_offset' => 0));
             $ret = $socket->connect($svr['host'], $svr['port'], $this->timeout);
             $socketFd = $socket->sock;
         }
         //连接被拒绝,证明服务器已经挂了
         //TODO 如果连接失败,需要上报机器存活状态
         if ($ret === false and $socket->errCode == 111) {
             $this->onConnectServerFailed($svr);
             unset($socket);
         } else {
             $retObj->socket = $socket;
             $retObj->server_host = $svr['host'];
             $retObj->server_port = $svr['port'];
             //使用SOCKET的编号作为ID
             $retObj->id = $socketFd;
             break;
         }
     }
     return $ret;
 }
示例#2
0
文件: SOA.php 项目: viile/swoole_test
 /**
  * 发送请求
  * @param $type
  * @param $send
  * @param SOA_result $retObj
  * @return bool
  */
 protected function request($type, $send, $retObj)
 {
     $svr = $this->getServer();
     $socket = new TCP();
     $retObj->socket = $socket;
     $retObj->type = $type;
     $retObj->send = $send;
     $retObj->server_host = $svr['host'];
     $retObj->server_port = $svr['port'];
     $this->beforeRequest($retObj);
     //异步connect
     //TODO 如果连接失败,需要上报机器存活状态
     $ret = $socket->connect($svr['host'], $svr['port'], $this->timeout);
     //使用SOCKET的编号作为ID
     $retObj->id = (int) $socket->get_socket();
     if ($ret === false) {
         $retObj->code = SOA_Result::ERR_CONNECT;
         unset($retObj->socket);
         return false;
     }
     //请求串号
     $retObj->requestId = self::getRequestId();
     //发送失败了
     if ($retObj->socket->send(SOAServer::encode($retObj->send, SOAServer::DECODE_PHP, 0, $retObj->requestId)) === false) {
         $retObj->code = SOA_Result::ERR_SEND;
         unset($retObj->socket);
         return false;
     }
     //加入wait_list
     if ($type != self::TYPE_ASYNC) {
         $this->wait_list[$retObj->id] = $retObj;
     }
     return true;
 }
示例#3
0
文件: SOA.php 项目: shakeng/framework
 /**
  * 发送请求
  * @param $type
  * @param $send
  * @param SOA_result $retObj
  * @return bool
  */
 protected function request($type, $send, $retObj)
 {
     $this->beforeRequest($retObj);
     $ret = false;
     $socket = null;
     $svr = null;
     //循环连接
     while (count($this->servers) > 0) {
         $svr = $this->getServer();
         $socket = new TCP();
         $socket->try_reconnect = false;
         $ret = $socket->connect($svr['host'], $svr['port'], $this->timeout);
         //连接被拒绝,证明服务器已经挂了
         //TODO 如果连接失败,需要上报机器存活状态
         if ($ret === false and $socket->errCode == 111) {
             $this->onConnectServerFailed($svr);
         } else {
             break;
         }
     }
     $retObj->socket = $socket;
     $retObj->type = $type;
     $retObj->send = $send;
     $retObj->server_host = $svr['host'];
     $retObj->server_port = $svr['port'];
     //使用SOCKET的编号作为ID
     $retObj->id = (int) $socket->get_socket();
     if ($ret === false) {
         $retObj->code = SOA_Result::ERR_CONNECT;
         unset($retObj->socket);
         return false;
     }
     //请求串号
     $retObj->requestId = self::getRequestId();
     //发送失败了
     if ($retObj->socket->send(SOAServer::encode($retObj->send, SOAServer::DECODE_PHP, 0, $retObj->requestId)) === false) {
         $retObj->code = SOA_Result::ERR_SEND;
         unset($retObj->socket);
         return false;
     }
     //加入wait_list
     if ($type != self::TYPE_ASYNC) {
         $this->wait_list[$retObj->id] = $retObj;
     }
     return true;
 }