udp

package
v0.0.0-...-bff1185 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package udp provides a connection-oriented listener over a UDP PacketConn

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosedListener      = errors.New("udp: listener closed")
	ErrListenQueueExceeded = errors.New("udp: listen queue exceeded")
	ErrInvalidBatchConfig  = errors.New("udp: invalid batch config")
)

Typed errors

Functions

func Listen

func Listen(network string, laddr *net.UDPAddr) (net.Listener, error)

Listen creates a new listener using default ListenConfig.

Types

type BatchConn

type BatchConn struct {
	net.PacketConn
	// contains filtered or unexported fields
}

BatchConn uses ipv4/v6.NewPacketConn to wrap a net.PacketConn to write/read messages in batch, only available in linux. In other platform, it will use single Write/Read as same as net.Conn.

func NewBatchConn

func NewBatchConn(conn net.PacketConn, batchWriteSize int, batchWriteInterval time.Duration) *BatchConn

NewBatchConn creates a *BatchConn from net.PacketConn with batch configs.

func (*BatchConn) Close

func (c *BatchConn) Close() error

Close batchConn and the underlying PacketConn

func (*BatchConn) ReadBatch

func (c *BatchConn) ReadBatch(msgs []ipv4.Message, flags int) (int, error)

ReadBatch reads messages in batch, return length of message readed and error.

func (*BatchConn) WriteTo

func (c *BatchConn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo write message to an UDPAddr, addr should be nil if it is a connected socket.

type BatchIOConfig

type BatchIOConfig struct {
	Enable bool
	// ReadBatchSize indicates the maximum number of packets to be read in one batch, a batch size less than 2 means
	// disable read batch.
	ReadBatchSize int
	// WriteBatchSize indicates the maximum number of packets to be written in one batch
	WriteBatchSize int
	// WriteBatchInterval indicates the maximum interval to wait before writing packets in one batch
	// small interval will reduce latency/jitter, but increase the io count.
	WriteBatchInterval time.Duration
}

BatchIOConfig indicates config to batch read/write packets, it will use ReadBatch/WriteBatch to improve throughput for UDP.

type BatchPacketConn

type BatchPacketConn interface {
	BatchWriter
	BatchReader
	io.Closer
}

BatchPacketConn represents conn can read/write messages in batch

type BatchReader

type BatchReader interface {
	ReadBatch(msg []ipv4.Message, flags int) (int, error)
}

BatchReader represents conn can read messages in batch

type BatchWriter

type BatchWriter interface {
	WriteBatch(ms []ipv4.Message, flags int) (int, error)
}

BatchWriter represents conn can write messages in batch

type Conn

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

Conn augments a connection-oriented connection over a UDP PacketConn

func (*Conn) Close

func (c *Conn) Close() error

Close closes the conn and releases any Read calls

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr implements net.Conn.LocalAddr

func (*Conn) Read

func (c *Conn) Read(p []byte) (int, error)

Read reads from c into p

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn.RemoteAddr

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.SetDeadline

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.SetDeadline

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.SetDeadline

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the DTLS connection

type ListenConfig

type ListenConfig struct {
	// Backlog defines the maximum length of the queue of pending
	// connections. It is equivalent of the backlog argument of
	// POSIX listen function.
	// If a connection request arrives when the queue is full,
	// the request will be silently discarded, unlike TCP.
	// Set zero to use default value 128 which is same as Linux default.
	Backlog int

	// AcceptFilter determines whether the new conn should be made for
	// the incoming packet. If not set, any packet creates new conn.
	AcceptFilter func([]byte) bool

	// ReadBufferSize sets the size of the operating system's
	// receive buffer associated with the listener.
	ReadBufferSize int

	// WriteBufferSize sets the size of the operating system's
	// send buffer associated with the connection.
	WriteBufferSize int

	Batch BatchIOConfig
}

ListenConfig stores options for listening to an address.

func (*ListenConfig) Listen

func (lc *ListenConfig) Listen(network string, laddr *net.UDPAddr) (net.Listener, error)

Listen creates a new listener based on the ListenConfig.

Jump to

Keyboard shortcuts

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