heartbeat

package module
v0.0.0-...-285a020 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 10 Imported by: 0

README

heartbeat

Heartbeat is helper library for reliable.

Features

Hertbeat

Send heartbeat packets to be asked by a receiver. A receiver ack every 32 packets.
heartbeat packets sending start when WritePacket method is called. Sending continue until deadline come. The deadline is reset whenever WritePacket is called. The deadline and heartbeat period can be set through option.

Fragmentation

Fragment packets to send large size of packet.
Any packets which exceeds fragment size is divided into smaller ones. A receiver reassemble fragmented packets. The fragment size and max fragments can be set through option.

Notes

  • Send packet via WriteReliablePacket of reliable which mean that stop sending if a receiver don't ack.
  • reliable.WithEndpointPacketHandler is overwritten.
  • Allowed max packet size can be sent is MaxUint32(=4294967295).
  • Allowed max fragmentation size is MaxUint8(=255).

Example

func logPacket(packetData []byte) {
  log.Printf("recv (packetData size=%d)", len(packetData))
}

func logErr(err error) {
  log.Panic(err)
}

ca, _ := net.ListenPacket("udp", "127.0.0.1:0")
cb, _ := net.ListenPacket("udp", "127.0.0.1:0")

e, _ := heartbeat.NewEndpoint(ca, logPacket, logErr, nil)

e.WritePacket([]byte("hello world"), cb.LocalAddr())

Documentation

Index

Constants

View Source
const (
	FragmentHeaderSize uint8  = 5
	ReassemblyBufSize  uint16 = 512
)
View Source
const (
	RegularPacketPrefix byte = iota
	FragmentPacketPrefix
)
View Source
const (
	// heartbeat options
	DefaulthbPeriod                    = 20 * time.Millisecond
	DefaultHearbeatDeadline            = 1000 * time.Millisecond
	DefaultMaxHearbeatReceivers uint16 = 32

	// fragment options
	DefaultFragmentSize uint32 = 4096
	DefaultMaxFragments uint8  = 64
)

Variables

View Source
var HeartbeatMsg = []byte(".h")

Functions

This section is empty.

Types

type Endpoint

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

func NewEndpoint

func NewEndpoint(conn net.PacketConn, rh receiveHandler, eh errorHandler, opts []Option, reOpts ...reliable.EndpointOption) (*Endpoint, error)

Note: about reOpts, reliable.WithEndpointPacketHandler is overwritten

func (*Endpoint) Addr

func (e *Endpoint) Addr() net.Addr

func (*Endpoint) Close

func (e *Endpoint) Close() error

func (*Endpoint) Listen

func (e *Endpoint) Listen()

func (*Endpoint) ReadPacket

func (e *Endpoint) ReadPacket(packetData []byte) error

func (*Endpoint) WritePacket

func (e *Endpoint) WritePacket(packetData []byte, addr net.Addr) error

type Heartbeat

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

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithFragmentSize

func WithFragmentSize(fragmentSize uint32) Option

func WithHearbeatTimeout

func WithHearbeatTimeout(hbDeadline time.Duration) Option

func WithMaxFragments

func WithMaxFragments(maxFragments uint8) Option

func WithMaxHearbeatReceivers

func WithMaxHearbeatReceivers(maxHbReceivers uint16) Option

func WithhbPeriod

func WithhbPeriod(hbPeriod time.Duration) Option

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

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