<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Object-proxy遠(yuǎn)程對(duì)象代理

          聯(lián)合創(chuàng)作 · 2023-10-01 10:34

          Object-proxy是一個(gè)簡(jiǎn)單的遠(yuǎn)程對(duì)象代理。

          用法

          use ObjectProxy\ObjectProxy;
          use ObjectProxy\ObjectProxyService;
          
          $objectName = \SomeClass::class;
          // 要實(shí)例化的對(duì)象名稱
          $constructArgs = [];
          // 構(gòu)造函數(shù)的參數(shù)
          
          $proxyObject = new ObjectProxy($objectName, $constructArgs);
          // $proxyObject是SomeClass對(duì)象的代理。
          
          $virtualReturnObject = $proxyObject->someObjectMethod();
          // 這個(gè) $virtualReturnObject 可以序列化后給到遠(yuǎn)程服務(wù)器。
          
          $return = new ObjectProxyService($virtualReturnObject);
          // 實(shí)例一個(gè)對(duì)象代理服務(wù)類處理操作代理對(duì)象
          
          $result = $return->getResult();
          
          if ($result instanceof \Exception) {
              echo $result->getMessage();
              // 調(diào)用發(fā)生異常,顯示錯(cuò)誤消息。
          } else {
              var_dump($result);
              // SomeClass::someObjectMethod的返回結(jié)果。
          }

          示例

          通過Swoole做一個(gè)PDO對(duì)象代理。

          bin/swoole_client.php
          use Swoole\Client;
          use ObjectProxy\ObjectProxy;
          use ObjectProxy\Transporter;
          
          require __DIR__.'/../src/Loader.php';
          
          for ($i = 0;$i <= 20000;++$i) {
          
              $pdo = new ObjectProxy(\PDO::class, array('mysql:host=127.0.0.1;dbname=test', 'user', 'password',  [\PDO::ATTR_PERSISTENT => true]));
          
              $stat = $pdo->prepare('SELECT * FROM test WHERE id = ?');
          
              $id = 2;
          
              $stat->execute([$id]);
          
              $virtualObject = $stat->fetch();
          
              $client = new Client(SWOOLE_SOCK_TCP);
              $client->connect('127.0.0.1', 9509);
          
              $client->send(Transporter::encode($virtualObject));
              $data = $client->recv();
              $client->close();
          
              $r = Transporter::decode($data);
          
              if ($r instanceof \Exception) {
                  echo 'Bad: '.$i.' '.$r->getMessage()."\n";
              } else {
                  echo 'OK: '.$i.':'.$r['id']."\n";
              }
          }
          bin/swoole_server.php
          use Swoole\Server;
          use ObjectProxy\Transporter;
          use ObjectProxy\ObjectProxyService;
          
          require __DIR__.'/../src/Loader.php';
          
          class SwooleObjectProxyServer
          {
              public function run()
              {
                  echo 'Server: start.Swoole version is ['.SWOOLE_VERSION."]\n";
          
                  $serv = new Server('127.0.0.1', 9509);
          
                  $serv->set(array(
                      'worker_num' => 20, //worker process num          
                      'task_worker_num' => 10, //Object Pool Size
                      //'backlog' => 128, //listen backlog
                      //'open_tcp_keepalive' => 1,
                      //'log_file' => '/tmp/swoole.log', //swoole error log
                  ));
                  $serv->on('WorkerStart', array($this, 'onStart'));
                  $serv->on('Receive', array($this, 'onReceive'));
                  $serv->on('Close', array($this, 'onClose'));
                  $serv->on('Task', array($this, 'onTask'));
                  $serv->on('Finish', array($this, 'onFinish'));
                  $serv->on('WorkerStop', array($this, 'onShutdown'));
          
                  $serv->start();
              }
          
              public function onTask($serv, $task_id, $from_id, $data)
              {
                  $service = new ObjectProxyService(Transporter::decode($data));
          
                  $serv->finish(Transporter::encode($service->getResult()));
              }
          
              public function onReceive($serv, $fd, $from_id, $data)
              {
                  $result = $serv->taskwait($data);
          
                  $serv->send($fd, $result);
          
                  echo "onReceive\n";
              }
          
              public function onClose($serv, $fd, $from_id)
              {
                  echo "onClose\n";
              }
          
              public function onStart($serv)
              {        
              }
          
              public function onFinish($serv, $data)
              {
              }
          
              public function onShutdown($serv)
              {
              }
          }
          
          $serv = new SwooleObjectProxyServer();
          
          $serv->run();
          瀏覽 20
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天艹天天日天天干 | 欧美亚洲国产高清 | 欧美成人性生活视频 | 欧美成人一级视频 | 啪啪啪啪啪啪AVXXX |