Ejemplo n.º 1
0
 static function test1($str)
 {
     var_dump(SOAServer::getClientEnv());
     var_dump(SOAServer::getRequestHeader());
     return "hello-soa-finish: {$str}";
 }
Ejemplo n.º 2
0
 /**
  * 并发请求
  * @param float $timeout
  * @return int
  */
 function wait($timeout = 0.5)
 {
     $st = microtime(true);
     $t_sec = (int) $timeout;
     $t_usec = (int) (($timeout - $t_sec) * 1000 * 1000);
     $buffer = $header = array();
     $success_num = 0;
     while (true) {
         $write = $error = $read = array();
         if (empty($this->wait_list)) {
             break;
         }
         foreach ($this->wait_list as $obj) {
             if ($obj->socket !== null) {
                 $read[] = $obj->socket->get_socket();
             }
         }
         if (empty($read)) {
             break;
         }
         $n = socket_select($read, $write, $error, $t_sec, $t_usec);
         if ($n > 0) {
             //可读
             foreach ($read as $sock) {
                 $id = (int) $sock;
                 /**
                  * @var $retObj SOA_Result
                  */
                 $retObj = $this->wait_list[$id];
                 $data = $retObj->socket->recv();
                 //socket被关闭了
                 if (empty($data)) {
                     $retObj->code = SOA_Result::ERR_CLOSED;
                     unset($this->wait_list[$id], $retObj->socket);
                     continue;
                 }
                 //一个新的请求,缓存区中没有数据
                 if (!isset($buffer[$id])) {
                     //这里仅使用了length和type,uid,serid未使用
                     $header[$id] = unpack(SOAServer::HEADER_STRUCT, substr($data, 0, SOAServer::HEADER_SIZE));
                     //错误的包头
                     if ($header[$id] === false or $header[$id]['length'] <= 0) {
                         $retObj->code = SOA_Result::ERR_HEADER;
                         unset($this->wait_list[$id]);
                         continue;
                     } elseif ($header[$id]['length'] > $this->packet_maxlen) {
                         $retObj->code = SOA_Result::ERR_TOOBIG;
                         unset($this->wait_list[$id]);
                         continue;
                     }
                     $buffer[$id] = substr($data, SOAServer::HEADER_SIZE);
                 } else {
                     $buffer[$id] .= $data;
                 }
                 //达到规定的长度
                 if (strlen($buffer[$id]) == $header[$id]['length']) {
                     $retObj->responseId = $header[$id]['serid'];
                     //成功处理
                     $this->finish(SOAServer::decode($buffer[$id], $header[$id]['type']), $retObj);
                     $success_num++;
                 }
                 //继续等待数据
             }
         }
         //发生超时
         if (microtime(true) - $st > $timeout) {
             foreach ($this->wait_list as $obj) {
                 //TODO 如果请求超时了,需要上报服务器负载
                 $obj->code = $obj->socket->connected ? SOA_Result::ERR_TIMEOUT : SOA_Result::ERR_CONNECT;
                 //执行after钩子函数
                 $this->afterRequest($obj);
             }
             //清空当前列表
             $this->wait_list = array();
             return $success_num;
         }
     }
     //未发生任何超时
     $this->wait_list = array();
     return $success_num;
 }
Ejemplo n.º 3
0
 static function hello()
 {
     return var_dump(SOAServer::getRequestHeader(), true);
 }