一个简易的数据传输协议,保证在流传输协议下,每次Write的数据对方能完整的接收和处理
Go to file
Akvicor 1c9129c1e3
Test / testing (1.19.13, ubuntu-latest) (push) Successful in 3m10s Details
Test / testing (>=1.20, ubuntu-latest) (push) Successful in 4m5s Details
test
2024-03-12 00:18:06 +08:00
.gitea/workflows test 2024-02-03 10:43:41 +08:00
.gitignore 0.0.1 2024-01-16 03:01:44 +08:00
LICENSE LICENSE 2024-01-22 08:50:12 +08:00
README.md log 2024-03-11 23:13:45 +08:00
go.mod heartbeat 2024-02-27 23:38:46 +08:00
go.sum error message 2024-03-12 00:02:13 +08:00
log.go log 2024-03-11 23:13:45 +08:00
package.go check header 2024-03-12 00:16:47 +08:00
protocol.go func after read/write 2024-03-11 23:34:17 +08:00
protocol_test.go test 2024-03-12 00:18:06 +08:00
queue.go heartbeat 2024-02-28 02:49:54 +08:00
queue_test.go heartbeat 2024-02-28 02:49:54 +08:00

README.md

Protocol

一个简易的数据传输协议保证在流传输协议下每次Write的数据对方能完整(不多&不少)的接收和处理

GoReport Gitea Release License: GPL v2 Go.Dev reference

import "git.viry.cc/gomod/protocol"

样例请参照protocol_test.go中的示例示例中包含了对tcp的封装

// 创建protocol封装
New(tag string, r io.Reader, w io.Writer, writeQueueSize int, readCallback func(data []byte), heartbeatTimeoutCallback func(p *Protocol) bool, setFuncBeforeRead, setFuncBeforeWrite, killCallback func()) *Protocol
// 启动传输,通过参数确定是否是心跳服务端(主动发出心跳信号一方)
//    如果传输双方均为发出方,不会影响正常服务,但会产生不必要的心跳
Connect(bool)
// 发送数据(注意数据长度有限制
Write([]byte{0x11, 0x22, 0x33})
// 设置心跳请求间隔(单位秒),用于主动发出心跳的一方
SetHeartbeatInterval(interval uint8)
GetHeartbeatInterval() uint8
// 设置心跳超时时间(单位秒)
SetHeartbeatTimeout(timeout uint8)
GetHeartbeatTimeout() uint8
// 获取上一次收到心跳的时间
GetHeartbeatLastReceived()
// 获取上一次发送心跳的时间
GetHeartbeatLastSend()
// 获取正在运行的协程的数量
GetRunningRoutine() int32
// 等待Protocol关闭
WaitKilled(timeout int) error
// 获取tag
GetTag() string
SetTag(tag string)
// 关闭Protocol
Kill()

// 设置log模式
SetLogProd(isProd bool)
SetLogMask(mask uint32)
SetLogFlag(f uint32)

// Protocol版本号不同版本存在不兼容的可能性
protocol.VERSION
// 获取每次Write的最大数据长度
protocol.GetDataMaxSize() int
// 计算传入的size需要多少次Write才能发送
protocol.CalculateTheNumberOfPackages(size int64) int64