protocol

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2024 License: GPL-2.0 Imports: 11 Imported by: 0

README

Protocol

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

GoReport Gitea Release Go.Dev reference

TODO

  • 心跳服务发送单独的Package, 如果value不为0, 则修改心跳超时时间
  • 完全关闭心跳, 即双方都不发送心跳信号
  • 单/双方间隔发送HeartbeatResponse
  • 单/双方间隔发送HeartbeatRequest, 接收方收到Request需要响应Response
  • 心跳信号不管是Request还是Response, 都可以携带Confirm信息
  • Confirm信息的键由Heartbeat携带
  • 更新glog包,使得glog能够接受外部File的Paper, 保证使用glog的mod和使用mod的项目公用一个log文件
  • 可变header长度
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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDataSizeExceedsLimit = errors.New("body size exceeds limit")
View Source
var ErrEncryptorIsNil = errors.New("encryptor is nil")
View Source
var ErrReaderIsNil = errors.New("reader is nil")
View Source
var ErrWriterIsKilled = errors.New("writer is killed")
View Source
var ErrWriterIsNil = errors.New("writer is nil")

Functions

This section is empty.

Types

type HeartbeatTimeoutCallback added in v0.3.0

type HeartbeatTimeoutCallback func(p *Protocol) bool

type KillCallback added in v0.3.0

type KillCallback func()

type Protocol

type Protocol struct {
	// contains filtered or unexported fields
}

func New

func New(tag string, r io.Reader, w io.Writer, passwordAes, passwordXor string, heartbeatInterval, heartbeatTimeout uint32, sendQueueSize, receiveQueueSize int, readCallback ReadCallback, heartbeatTimeoutCallback HeartbeatTimeoutCallback, killCallback KillCallback, readPrepare ReadPrepare, writePrepare WritePrepare) (prot *Protocol, err error)

New 返回一个protocol实例

tag: 标签,用于区分protocol实例
r: 数据流的reader
w: 数据流的writer
writeQueueSize: 发送等待队列长度
readCallback: 用于处理获取到的数据,每个package中的数据都会完整的保存在data中
heartbeatTimeoutCallback: 心跳请求超时后的处理函数
setFuncBeforeRead: 在reader读取数据前,设置reader的读取截止时间
setFuncBeforeWrite: 在writer发送数据前,设置writer的发送截止时间
killCallback: status被标记为statusKilled时执行,可以用于关闭reader和writer

func (*Protocol) Connect added in v0.1.0

func (p *Protocol) Connect(activeHeartbeatSignalSender bool)

func (*Protocol) Wait added in v0.3.0

func (p *Protocol) Wait()

func (*Protocol) Write

func (p *Protocol) Write(flag, encryptor uint8, data []byte) error

type ReadCallback added in v0.3.0

type ReadCallback func(data []byte)

type ReadPrepare added in v0.3.0

type ReadPrepare func()

type WritePrepare added in v0.3.0

type WritePrepare func()

Directories

Path Synopsis
pkg
xor

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL