Пример #1
0
 /**
  * @param string $func
  * @param mixed  $args
  *
  * @return mixed
  * @throws Exception\RequestErrorException
  */
 public function send($func, $args)
 {
     $host = $this->host;
     $port = $this->port;
     $code = 0;
     $call = $this->back->clientCallObject($code, $func, $args);
     $send = $this->back->clientConnection($host, $port, $call, $this->socketTimeout, $this->socketReadTimeout);
     $future = $this->back->clientRecvObject($send);
     $result = $future->getResult();
     $errors = $future->getErrors();
     if ($errors !== null) {
         if (is_array($errors)) {
             $errors = '[' . implode(', ', $errors) . ']';
         } else {
             if (is_object($errors)) {
                 if (method_exists($errors, '__toString')) {
                     $errors = $errors->__toString();
                 } else {
                     $errors = print_r($errors, true);
                 }
             }
         }
         throw new RequestErrorException("{$errors}");
     }
     return $result;
 }
Пример #2
0
 /**
  * @throws Exception\NetworkErrorException
  */
 public function recv()
 {
     try {
         ErrorHandler::start();
         $this->listenSocket = socket_create_listen($this->port);
         $error = ErrorHandler::stop();
         $sockList = array($this->listenSocket);
         if ($this->listenSocket === false) {
             throw new NetworkErrorException("Cannot listen on port " . $this->port, 0, $error);
         }
         while (true) {
             $moveList = $sockList;
             $moveNums = socket_select($moveList, $w = null, $e = null, null);
             foreach ($moveList as $moveItem) {
                 if ($moveItem == $this->listenSocket) {
                     $acptItem = socket_accept($this->listenSocket);
                     $sockList[] = $acptItem;
                 } else {
                     $data = socket_read($moveItem, $this->back->getSize());
                     list($code, $func, $args) = $this->back->serverRecvObject($data);
                     $hand = $this->hand;
                     $error = null;
                     try {
                         $ret = call_user_func_array(array($hand, $func), $args);
                     } catch (\Exception $e) {
                         $ret = null;
                         $error = $e->__toString();
                     }
                     $send = $this->back->serverSendObject($code, $ret, $error);
                     socket_write($moveItem, $send);
                     unset($sockList[array_search($moveItem, $sockList)]);
                     socket_close($moveItem);
                 }
             }
         }
     } catch (\Exception $e) {
         throw new NetworkErrorException("Server error", 0, $e);
     }
 }