TCP 服務(wù)
TCP 基礎(chǔ) Demo
EasySwoole
創(chuàng)建 TCP
服務(wù)器,有兩種以下方式:
1.將 TCP 服務(wù)作為 EasySwoole 的主服務(wù)。
首先修改配置文件中 MAIN_SERVER.SERVER_TYPE
配置項(xiàng)為 EASYSWOOLE_SERVER
。
然后在 EasySwooleEvent
的 mainServerCreate 事件中注冊(cè)回調(diào),注冊(cè)參考示例如下:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use Swoole\Server as SwooleServer;
class EasySwooleEvent implements Event
{
// ...
public static function mainServerCreate(EventRegister $register)
{
$register->add($register::onConnect, function (SwooleServer $server, int $fd, int $reactorId) {
echo "fd{$fd} connected\n";
});
$register->add($register::onReceive, function (SwooleServer $server, int $fd, int $reactorId, string $data) {
echo "fd:{$fd} receive_data:{$data}\n";
});
$register->add($register::onClose, function (SwooleServer $server, int $fd, int $reactorId) {
echo "fd {$fd} closed\n";
});
}
}
2.將 TCP 服務(wù)作為 EasySwoole 的子服務(wù)。顧名思義:另外開(kāi)一個(gè)端口進(jìn)行
tcp
監(jiān)聽(tīng)。
在 EasySwooleEvent
中的 mainServerCreate 事件中進(jìn)行子服務(wù)監(jiān)聽(tīng),參考代碼如下:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use Swoole\Server as SwooleServer;
class EasySwooleEvent implements Event
{
// ...
public static function mainServerCreate(EventRegister $register)
{
// ....
$server = ServerManager::getInstance()->getSwooleServer();
$subPort = $server->addlistener('0.0.0.0', 9502, SWOOLE_TCP);
$subPort->set([
// swoole 相關(guān)配置
'open_length_check' => false,
]);
$subPort->on($register::onConnect, function (SwooleServer $server, int $fd, int $reactorId) {
echo "fd {$fd} connected\n";
});
$subPort->on($register::onReceive, function (SwooleServer $server, int $fd, int $reactorId, string $data) {
echo "fd:{$fd} received_data:{$data}\n";
});
$subPort->on($register::onClose, function (SwooleServer $server, int $fd, int $reactorId) {
echo "fd {$fd} closed\n";
});
}
}
如何處理粘包
1.解決思路
-
方法1:通過(guò)標(biāo)識(shí)
EOF
,例如http
協(xié)議,通過(guò)\r\n\r\n
的方式去表示該數(shù)據(jù)已經(jīng)完結(jié),我們可以自定義一個(gè)協(xié)議。例如當(dāng)接收到 "結(jié)尾666" 字符串時(shí),代表該字符串已經(jīng)結(jié)束,如果沒(méi)有獲取到,則存入緩沖區(qū),等待結(jié)尾字符串,或者如果獲取到多條,則通過(guò)該字符串剪切出其他數(shù)據(jù)。 -
方法2:定義消息頭,通過(guò)特定長(zhǎng)度的消息頭進(jìn)行獲取。例如我們定義一個(gè)協(xié)議,前面 10 位字符串都代表著之后數(shù)據(jù)主體的長(zhǎng)度,那么我們傳輸數(shù)據(jù)時(shí),只需要
000000000512346
(前10位為協(xié)議頭,表示了這條數(shù)據(jù)的大小,后面的為數(shù)據(jù)),每次我們讀取只先讀取10位,獲取到消息長(zhǎng)度,再讀取消息長(zhǎng)度那么多的數(shù)據(jù),這樣就可以保證數(shù)據(jù)的完整性了。(但是為了不被混淆,協(xié)議頭也得像EOF
一樣標(biāo)識(shí)) -
方法3:通過(guò)
pack
二進(jìn)制處理,相當(dāng)于于方法2,將數(shù)據(jù)通過(guò)二進(jìn)制封裝拼接進(jìn)消息中,通過(guò)驗(yàn)證二進(jìn)制數(shù)據(jù)去讀取信息,swoole
采用的就是這種方式。
可查看 swoole 官方文檔: https://wiki.swoole.com/zh-cn/#/learn?id=tcp數(shù)據(jù)包邊界問(wèn)題