execute() public method

public execute ( string $str = null, integer &$offset = null )
$str string
$offset integer
Beispiel #1
0
 private function nextRpcMsg()
 {
     while (socket_recv($this->socket, $buf, 1024, 0)) {
         $this->unpacker->feed($buf);
         while ($this->unpacker->execute()) {
             $unserialized = $this->unpacker->data();
             $this->unpacker->reset();
             (yield $unserialized);
         }
     }
 }
Beispiel #2
0
function test($type, $variable, $test = null)
{
    $msgpack = new MessagePack();
    if (version_compare(PHP_VERSION, '5.1.0') < 0) {
        $msgpack->setOption(MESSAGEPACK_OPT_PHPONLY, false);
    } else {
        $msgpack->setOption(MessagePack::OPT_PHPONLY, false);
    }
    $serialized = $msgpack->pack($variable);
    $unpacker = new MessagePackUnpacker();
    if (version_compare(PHP_VERSION, '5.1.0') < 0) {
        $unpacker->setOption(MESSAGEPACK_OPT_PHPONLY, false);
    } else {
        $unpacker->setOption(MessagePack::OPT_PHPONLY, false);
    }
    $length = strlen($serialized);
    if (rand(0, 1)) {
        for ($i = 0; $i < $length;) {
            $len = rand(1, 10);
            $str = substr($serialized, $i, $len);
            $unpacker->feed($str);
            if ($unpacker->execute()) {
                $unserialized = $unpacker->data();
                var_dump($unserialized);
                $unpacker->reset();
            }
            $i += $len;
        }
    } else {
        $str = "";
        $offset = 0;
        for ($i = 0; $i < $length;) {
            $len = rand(1, 10);
            $str .= substr($serialized, $i, $len);
            if ($unpacker->execute($str, $offset)) {
                $unserialized = $unpacker->data();
                var_dump($unserialized);
                $unpacker->reset();
                $str = "";
                $offset = 0;
            }
            $i += $len;
        }
    }
    if (!is_bool($test)) {
        echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL;
    } else {
        echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL;
    }
}
Beispiel #3
0
function test($type, $variable, $test = null)
{
    $serialized = msgpack_serialize($variable);
    $unpacker = new MessagePackUnpacker();
    $length = strlen($serialized);
    $str = "";
    $offset = 0;
    for ($i = 0; $i < $length;) {
        $len = rand(1, 10);
        $str .= substr($serialized, $i, $len);
        if ($unpacker->execute($str, $offset)) {
            $unserialized = $unpacker->data();
            var_dump($unserialized);
            $unpacker->reset();
            $str = "";
            $offset = 0;
        }
        $i += $len;
    }
    if (!is_bool($test)) {
        echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL;
    } else {
        echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL;
    }
}
Beispiel #4
0
function test()
{
    $serialized = msgpack_serialize(null);
    $serialized = substr($serialized, 0, -1);
    $length = mt_rand(1, 255);
    for ($i = 0; $i < $length; ++$i) {
        $serialized .= chr(mt_rand(0, 255));
    }
    // if returned null everything is OK
    $unpacker = new MessagePackUnpacker();
    $unpacker->feed($serialized);
    if ($unpacker->execute()) {
        if (($unserialized = $unpacker->data()) === null) {
            return true;
        }
        $unpacker->reset();
    } else {
        return true;
    }
    // whole data is read?
    if ($serialized !== msgpack_serialize($unserialized)) {
        return true;
    }
    echo bin2hex($serialized), "\n";
    var_dump($unserialized);
    return false;
}
Beispiel #5
0
function test($type, $variable, $test = null)
{
    $unpacker = new MessagePackUnpacker();
    $str = "";
    $offset = 0;
    foreach ($variable as $var) {
        $serialized = pack('H*', $var);
        $length = strlen($serialized);
        for ($i = 0; $i < $length;) {
            $len = rand(1, 10);
            $str .= substr($serialized, $i, $len);
            while (true) {
                if ($unpacker->execute($str, $offset)) {
                    $unserialized = $unpacker->data();
                    var_dump($unserialized);
                    $unpacker->reset();
                    $str = substr($str, $offset);
                    $offset = 0;
                } else {
                    break;
                }
            }
            $i += $len;
        }
    }
}
Beispiel #6
0
 /**
  * @param $host
  * @param $port
  * @param $call
  *
  * @param float    $socketTimeout
  * @param int|null $socketReadTimeout
  *
  * @return string
  * @throws Exception\NetworkErrorException
  * @throws Exception\TimeoutException
  */
 public function clientConnection($host, $port, $call, $socketTimeout, $socketReadTimeout = null)
 {
     $send = $this->serializer->serialize($call);
     $sock = $this->connect($host, $port, $socketTimeout);
     if ($sock === false) {
         throw new NetworkErrorException("Cannot open socket");
     }
     ErrorHandler::start();
     $puts = fwrite($sock, $send);
     $error = ErrorHandler::stop();
     if ($puts === false) {
         throw new NetworkErrorException("Cannot write to socket", 0, $error);
     }
     ErrorHandler::start();
     stream_set_blocking($sock, 0);
     $result = "";
     while (!feof($sock)) {
         $r = [$sock];
         $n = null;
         $return_code = stream_select($r, $n, $n, $socketReadTimeout);
         if ($return_code === 0) {
             throw new TimeoutException('Timeout exceeded when read from socket');
         }
         $read = fread($sock, $this->size);
         if ($read === false) {
             throw new NetworkErrorException("Cannot read from socket", 0, $error);
         }
         $this->unpacker->feed($read);
         if ($this->unpacker->execute()) {
             $result = $this->unpacker->data();
             break;
         }
     }
     if (!$this->reuseConnection) {
         ErrorHandler::start();
         fclose($sock);
         ErrorHandler::stop();
     }
     return $result;
 }
 protected function unpackImpl(TreasureData_API_Stream_InputStream $stream, $callback = null)
 {
     $unpacker = new MessagePackUnpacker();
     $result = array();
     $offset = 0;
     $flag = true;
     $call = false;
     if (is_callable($callback)) {
         $call = true;
         $result = true;
     }
     while (true) {
         if ($flag) {
             $buffer = $stream->read();
             $flag = false;
         }
         if (empty($buffer)) {
             break;
         }
         if ($unpacker->execute($buffer, $offset)) {
             if ($call) {
                 call_user_func_array($callback, array($unpacker->data()));
             } else {
                 $result[] = $unpacker->data();
             }
             $unpacker->reset();
             $buffer = substr($buffer, $offset);
             $offset = 0;
             if (empty($buffer)) {
                 $flag = true;
                 continue;
             }
         }
     }
     return $result;
 }
 /**
  * Sends data to the socket and receives the response
  * 
  * @throws WriteErrorException
  * @throws ReadErrorException
  * @throws ResponseErrorException
  * @throws TypeErrorException
  * @throws ServerErrorException
  * @param  string &$method
  * @param  array &$params
  * @param  integer &$msgid
  * @return mixed
  */
 public function call(&$method, &$params = array(), &$msgid = 0)
 {
     if (!$this->conn) {
         $this->connect();
     }
     $buffer = '';
     $buffer_length = 0;
     $data = array(0 => 0, 1 => $msgid, 2 => $method, 3 => $params);
     @stream_set_blocking($this->sock, 1);
     if ($this->debug) {
         $this->log('Set option to socket: blocking', $this->sock);
     }
     $packed_data = $this->pack($data);
     $send = @fwrite($this->sock, $packed_data);
     if ($this->debug) {
         $this->log('Write to socket: ' . json_encode($data), $send);
     }
     if ($send != strlen($packed_data)) {
         throw \Novutec\MessagePackRpcClient\AbstractException::factory('WriteError', 'Could not write to the socket. (tried/send): ' . '(' . $send . '/' . strlen($packed_data) . ')');
     }
     @stream_set_blocking($this->sock, 0);
     if ($this->debug) {
         $this->log('Set option to socket: non-blocking', $this->sock);
     }
     $read = $write = array($this->sock);
     $except = null;
     $unpacker = new \MessagePackUnpacker();
     do {
         if (stream_select($read, $write, $except, $this->timeout) === false) {
             break;
         }
         $recv = @fread($this->sock, $this->length);
         if ($this->debug && $recv != '') {
             $this->log('Read from socket - length: ' . strlen($recv), $recv);
         }
         $unpacker->feed($recv);
         if ($recv === false) {
             throw \Novutec\MessagePackRpcClient\AbstractException::factory('ReadError', 'Could not read from socket.');
         }
     } while (!$unpacker->execute());
     $data = $unpacker->data();
     $unpacker->reset();
     if ($this->debug) {
         $this->log('Unpack received data: ' . json_encode($data), $data);
     }
     if (sizeof($data) != 4) {
         throw \Novutec\MessagePackRpcClient\AbstractException::factory('ResponseError', 'Number of elements within received data is wrong.');
     }
     if ($data[0] != 1) {
         throw \Novutec\MessagePackRpcClient\AbstractException::factory('TypeError', 'Received data has the wrong type.');
     }
     if ($data[2] != '') {
         throw \Novutec\MessagePackRpcClient\AbstractException::factory('ServerError', isset($data[2][0]) ? $data[2][0] : 'Got an error from server.');
     }
     return $data[3];
 }
<?php

// serialized data
$msgs = array(pack("C*", 0x93, 0x1, 0x2, 0x3, 0x92), pack("C*", 0x3, 0x9, 0x4));
// streaming deserialize
$unpacker = new MessagePackUnpacker();
$buffer = "";
$nread = 0;
foreach ($msgs as $msg) {
    $buffer = $buffer . $msg;
    while (true) {
        if ($unpacker->execute($buffer, $nread)) {
            $msg = $unpacker->data();
            var_dump($msg);
            $unpacker->reset();
            $buffer = substr($buffer, $nread);
            $nread = 0;
            if (!empty($buffer)) {
                continue;
            }
        }
        break;
    }
}
?>