1 Star 6 Fork 4

夏季的风/TCP-UDP网络组件

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SpliceSymbolReceiver.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
package Receivers
import (
"gitee.com/ling-bin/network/netInterface"
)
//SpliceSymbolReceiver 按照开始结束符来粘包(单字节版)
type SpliceSymbolReceiver struct {
startSymbol byte
finishSymbol byte
}
// NewSpliceSymbolReceiver 按照开始结束符来粘包
func NewSpliceSymbolReceiver(startSymbol byte, finishSymbol byte) *SpliceSymbolReceiver {
return &SpliceSymbolReceiver{
startSymbol: startSymbol,
finishSymbol: finishSymbol,
}
}
//GetHeadLen 包头必须要的长度
func (s *SpliceSymbolReceiver) GetHeadLen() int {
return 1
}
//CanHandle 是否能处理
func (s *SpliceSymbolReceiver) CanHandle(conn netInterface.IConnection, buffer []byte) bool {
return buffer[0] == s.startSymbol
}
//Reset 重置分包状态为初始状态
func (s *SpliceSymbolReceiver) Reset() {
}
//Receiver 分包逻辑
func (s *SpliceSymbolReceiver) Receiver(conn netInterface.IConnection, buffer []byte) ([]byte, int) {
var (
bfLen = len(buffer)
startIndex = -1
isStart = false
)
for i := 0; i < bfLen; i++ {
if !isStart && buffer[i] == s.startSymbol {
startIndex = i
isStart = true
} else if buffer[i] == s.finishSymbol {
hData := buffer[startIndex : i+1]
startIndex = i + 1
return hData, i + 1
}
}
return nil, startIndex
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ling-bin/network.git
git@gitee.com:ling-bin/network.git
ling-bin
network
TCP-UDP网络组件
v1.8.25

搜索帮助

0d507c66 1850385 C8b1a773 1850385