# 封裝器-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;
};
```
- SD3.X簡介
- 捐贈SD項目
- VIP服務
- 基礎篇
- 搭建環境
- 使用Composer安裝/更新SD框架
- 啟動命令
- 開發注意事項
- 框架配置
- 配置文件夾
- server.php
- ports.php
- business.php
- mysql.php
- redis.php
- timerTask.php
- log.php
- consul.php
- catCache.php
- client.php
- 自定義配置
- 框架入口
- MVC架構
- 加載器-Loader
- 控制器-Controller
- 模型-Model
- 視圖-View
- 同步任務-Task
- 封裝器
- Swoole編程指南-EOF協議
- Swoole編程指南-固定包頭協議
- 封裝器-Pack
- 路由器
- TCP相關
- 綁定UID
- Send系列
- Sub/Pub
- 獲取服務器信息
- Http相關
- HttpInput
- HttpOutput
- 默認路由規則
- WebSocket相關
- 使用SSL
- 公共函數
- 進階篇
- 內核優化
- 封裝器路由器原理剖析
- 對象池
- 上下文-Context
- 中間件
- 進程管理
- 創建自定義進程
- 進程間RPC
- 自定義進程如何使用連接池
- 異步連接池
- Redis
- Mysql
- Mqtt
- HttpClient
- Client
- AMQP
- RPC
- 日志工具-GrayLog
- 微服務-Consul
- Consul基礎
- 搭建Consul服務器
- SD中Consul配置
- 微服務
- 選舉-Leader
- Consul動態配置定時任務
- 熔斷與降級
- 集群-Cluster
- 高速緩存-CatCache
- 萬物-Actor
- Actor原型
- Actor的創建
- Actor間的通訊
- 消息派發-EventDispatcher
- 延遲隊列-TimerCallBack
- 協程
- 訂閱與發布
- MQTT簡易服務器
- AMQP異步任務調度
- 自定義命令-Console
- 調試工具Channel
- 特別注意事項
- 日常問題總結
- 實踐案例
- 物聯網自定義協議
- Actor在游戲的應用
- Mongodb以及一些同步擴展的使用
- 自定義進程使用MQTT客戶端
- 開發者工具
- SDHelper