protocol

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: GPL-2.0 Imports: 8 Imported by: 0

README

Protocol

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

import "git.viry.cc/gomod/protocol"
// 创建Protocol(只能通过New方法创建才能保证Protocol得到正确初始化)
prot = protocol.New(r io.Reader, w io.Writer)
// Reader方法为阻塞方式,监听接收数据,每次接收到完整Package后会调用callback来处理
go func() {
	err := prot.Reader(callback func(data []byte))
	if err != nil {
		glog.Fatal("failed to enable reader %v", err)
	}
}()
// Writer方法为阻塞方法,从队列中取出数据并发送,需要传入队列大小来初始化写入队列
go func() {
	err := prot.Writer(writeQueueSize int)
	if err != nil {
		glog.Fatal("failed to enable writer %v", err)
	}
}()
// Heartbeat方法为阻塞方法,
go func() {
	err := prot.Heartbeat(sendInterval int, receiveTimeout int, failedCallback func() bool)
	if err != nil {
		glog.Fatal("failed to enable heartbeat %v", err)
	}
}()
// 发送数据(注意数据长度有限制
prot.Write([]byte{0x11, 0x22, 0x33})
// 获取上一次收到心跳的时间
prot.GetHeartbeatLastReceived()
// 获取上一次发送心跳的时间
prot.GetHeartbeatLastSend()
// 关闭Protocol
prot.Kill()

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

Documentation

Index

Constants

View Source
const VERSION uint8 = 1

Variables

View Source
var ErrorBrokenData = errors.New("data crc32 checksum does not match")
View Source
var ErrorBrokenHead = errors.New("head crc32 checksum does not match")
View Source
var ErrorDataSizeExceedsLimit = errors.New("data size exceeds limit")
View Source
var ErrorHeartbeatCallbackIsNil = errors.New("heartbeat callback is nil")
View Source
var ErrorHeartbeatIsKilled = errors.New("heartbeat is killed")
View Source
var ErrorPackageEncrypted = errors.New("package is encrypted")
View Source
var ErrorPackageIncomplete = errors.New("package incomplete")
View Source
var ErrorReadCallbackIsNil = errors.New("read callback is nil")
View Source
var ErrorReaderIsKilled = errors.New("reader is killed")
View Source
var ErrorReaderIsNil = errors.New("reader is nil")
View Source
var ErrorUnsupportedVersion = errors.New("unsupported version")
View Source
var ErrorWriterIsKilled = errors.New("writer is killed")
View Source
var ErrorWriterIsNil = errors.New("writer is nil")
View Source
var ErrorWriterQueueIsNil = errors.New("writer queue is nil")
View Source
var ErrorWrongPrefix = errors.New("prefix does not match")

Functions

func CalculateTheNumberOfPackages

func CalculateTheNumberOfPackages(size int64) int64

func GetDataMaxSize

func GetDataMaxSize() int

Types

type Protocol

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

func New

func New(r io.Reader, w io.Writer) *Protocol

func (*Protocol) GetHeartbeatLastReceived

func (p *Protocol) GetHeartbeatLastReceived() int64

func (*Protocol) GetHeartbeatLastSend

func (p *Protocol) GetHeartbeatLastSend() int64

func (*Protocol) Heartbeat

func (p *Protocol) Heartbeat(sendInterval int, receiveTimeout int, failedCallback func() bool) error

Heartbeat 心跳服务

sendInterval: 主动发送心跳信号的间隔时间(s),最小为3s,,传入参数小于3时使用默认值3
receiveTimeout: 被动接收心跳信号的超时时间(s),最小为3s,传入参数小于3时使用默认值3
failedCallback: 没有按时收到心跳信号时调用,返回true继续等待,返回false退出

func (*Protocol) Kill

func (p *Protocol) Kill()

func (*Protocol) Reader

func (p *Protocol) Reader(callback func(data []byte)) error

Reader 阻塞接收数据并提交给readCallback

func (*Protocol) Write

func (p *Protocol) Write(data []byte) error

Write 发送数据

func (*Protocol) Writer

func (p *Protocol) Writer(writeQueueSize int) error

Writer 创建发送队列并监听待发送数据

Jump to

Keyboard shortcuts

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