代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。