1 Star 6 Fork 4

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

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SpliceSymbolReceiver.go 1.11 KB
一键复制 编辑 原始数据 按行查看 历史
package Receivers
import (
"gitee.com/ling-bin/network/netConn"
)
/// 按照开始结束符来粘包(单字节版)
type SpliceSymbolReceiver struct {
StartSymbol byte
FinishSymbol byte
}
//按照开始结束符来粘包
func NewSpliceSymbolReceiver(startSymbol byte,finishSymbol byte) *SpliceSymbolReceiver {
return &SpliceSymbolReceiver{
StartSymbol: startSymbol,
FinishSymbol: finishSymbol,
}
}
//分包逻辑
func (s *SpliceSymbolReceiver) Receiver(conn netConn.IConnection, buffer []byte) ([]byte,int,bool) {
bfLen := len(buffer)
startIndex := 0
isOk := false
for i := 0; i < bfLen; i++ {
if buffer[i] == s.StartSymbol {
startIndex = i
} else if buffer[i] == s.FinishSymbol {
cacheBf := conn.GetBytesCache()
cacheBf.Write(buffer[startIndex:(i + 1)])
data := cacheBf.Bytes()
//fmt.Printf( "[分包结果]=======:%p\n", data)
conn.OnReceiveCompleted(data)
//fmt.Printf( "[分包缓存]=======:%p\n", cacheBf)
cacheBf.Reset()
startIndex = i + 1
isOk = true
break
}
}
if startIndex < bfLen {
return buffer[startIndex:], startIndex, isOk
}
return nil, startIndex, isOk
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ling-bin/network.git
git@gitee.com:ling-bin/network.git
ling-bin
network
TCP-UDP网络组件
v1.2.1

搜索帮助