<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # 封裝器-Pack ```php interface IPack { function encode($buffer); function decode($buffer); function pack($data, $topic = null); function unPack($data); function getProbufSet(); function errorHandle($e, $fd); } ``` 需要實現6個方法 encode和decode分別是協議頭的封裝和解析工作,pack和unPack是協議體的序列化和反序列化工作,getProbufSet將返回swoole的set中關于協議頭解析相關的配置,errorHandle是協議解析出錯后的處理函數。 默認提供了幾組簡易配置 ```php class LenJsonPack implements IPack { protected $package_length_type = 'N'; protected $package_length_type_length = 4; protected $package_length_offset = 0; protected $package_body_offset = 0; protected $last_data = null; protected $last_data_result = null; /** * 數據包編碼 * @param $buffer * @return string * @throws SwooleException */ public function encode($buffer) { $total_length = $this->package_length_type_length + strlen($buffer) - $this->package_body_offset; return pack($this->package_length_type, $total_length) . $buffer; } /** * @param $buffer * @return string */ public function decode($buffer) { return substr($buffer, $this->package_length_type_length); } public function pack($data, $topic = null) { if ($this->last_data != null && $this->last_data == $data) { return $this->last_data_result; } $this->last_data = $data; $this->last_data_result = $this->encode(json_encode($data, JSON_UNESCAPED_UNICODE)); return $this->last_data_result; } public function unPack($data) { $value = json_decode($this->decode($data)); if (empty($value)) { throw new SwooleException('json unPack 失敗'); } return $value; } public function getProbufSet() { return [ 'open_length_check' => true, 'package_length_type' => $this->package_length_type, 'package_length_offset' => $this->package_length_offset, //第N個字節是包長度的值 'package_body_offset' => $this->package_body_offset, //第幾個字節開始計算長度 'package_max_length' => 2000000, //協議最大長度) ]; } public function errorHandle(\Throwable $e, $fd) { get_instance()->close($fd); } } ``` ## 自定義協議舉例 協議 = 包頭(4字節 `msg_type` + 4字節包體長) + 包體(protobuf) ```php <?php namespace app\Pack; use Server\Pack\IPack; use Server\CoreBase\SwooleException; use Game\Protobuf\Ping; use Game\Protobuf\Pong; use stdClass; class GamePack implements IPack { /** * 數據包編碼 * @param $buffer * @return string * @throws SwooleException */ public function encode($buffer) { return substr($buffer, 0, 4) . pack('N', strlen($buffer) - 4) . substr($buffer, 4); } /** * @param $buffer * @return string */ public function decode($buffer) { return substr($buffer, 0, 4) . substr($buffer, 8); } // 包長還是會傳遞過來 public function unPack($data) { $msgType = unpack('N', substr($data, 0, 4))[1]; $body = substr($data, 8); $dataMsg = new stdClass(); $dataMsg->msg_type = $msgType; // protobuf Exception // try { // $to->mergeFromString($data); // } catch (Exception $e) { // // Handle parsing error from invalid data. // ... // } if ($msgType == 1) { $msg = new Ping(); $msg->mergeFromString($body); $dataMsg->uid = $msg->getUid(); } else if ($msgType == 2) { $msg = new Pong(); $dataMsg->uid = $msg->getUid(); } return $dataMsg; } public function Pack($data, $topic = null) { $msgType = $data->msg_type; if ($msgType == 1) { $msg = new Ping(); $msg->setUid($data->uid); } else if ($msgType == 2) { $msg = new Pong(); $msg->setUid($data->uid); } $data = $msg->serializeToString(); return pack('N', $msgType) . pack('N', strlen($data)). $data; } public function getProbufSet() { return [ 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 4, //第N個字節是包長度的值 'package_body_offset' => 8, //第幾個字節開始計算長度 'package_max_length' => 2000000, //協議最大長度 ]; } public function errorHandle(\Throwable $e, $fd) { get_instance()->close($fd); } } ``` protobuf 定義如下: ``` syntax = "proto3"; package game.protobuf; enum MsgType { PING = 1; PONG = 2; }; message Ping { uint64 uid = 1; }; message Pong { uint64 uid = 1; }; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看