znet

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 18 Imported by: 1

README

znet

Documentation

Index

Constants

View Source
const (
	SO_ZEROCOPY       = 60
	SO_ZEROBLOCKTIMEO = 69
	MSG_ZEROCOPY      = 0x4000000
)
View Source
const BinaryInplaceThreshold = block4k

BinaryInplaceThreshold marks the minimum value of the nocopy slice length, which is the threshold to use copy to minimize overhead.

View Source
const EPOLLET = -syscall.EPOLLET

Variables

View Source
var LinkBufferCap = block4k

LinkBufferCap that can be modified marks the minimum value of each node of LinkBuffer.

Functions

func EpollCtl

func EpollCtl(epfd int, op int, fd int, event *epollevent) (err error)

EpollCtl implements epoll_ctl.

func EpollWait

func EpollWait(epfd int, events []epollevent, msec int) (n int, err error)

EpollWait implements epoll_wait. 等待事件的产生

func GetSysFdPairs

func GetSysFdPairs() (r, w int)

GetSysFdPairs creates and returns the fds of a pair of sockets.

func Init added in v0.0.16

func Init(numLoops int)

func SetKeepAlive

func SetKeepAlive(fd, secs int) error

Types

type CloseCallback

type CloseCallback func(c *connection) error

type Conn

type Conn interface {
	FDConn
	ReadLine() ([]byte, bool, error)
	WriteString(s string) (n int, err error)
	Flush() error
	LoadValue() any
	StoreValue(v any)
}

type EpollEvent

type EpollEvent int8
const (
	EpollRead    EpollEvent = 0x1
	EpollWrite   EpollEvent = 0x2
	EpollDetach  EpollEvent = 0x3
	EpollModRead EpollEvent = 0x4
	EpollR2RW    EpollEvent = 0x5
	EpollRW2R    EpollEvent = 0x6
)

type EventHandler

type EventHandler interface {
	OnConnect(ctx context.Context, conn Conn) error
	OnRead(ctx context.Context, conn Conn) error
}

type EventLoop

type EventLoop interface {
	Serve(listener net.Listener) error
	Shutdown(ctx context.Context) error
}

func NewEventLoop

func NewEventLoop(eh EventHandler, opts ...Option) EventLoop

type FDConn

type FDConn interface {
	net.Conn
	Fd() int
}

type FDOperator

type FDOperator struct {
	FD int

	OnAccept func() error
	OnHup    func(Poller) error

	// The following is the required fn, which must exist when used, or directly panic.
	// Fns are only called by the poll when handles connection events.
	Inputs   func(vs [][]byte) (rs [][]byte)
	InputAck func(n int) (err error)

	// Outputs will locked if len(rs) > 0, which need unlocked by OutputAck.
	Outputs   func(vs [][]byte) (rs [][]byte, supportZeroCopy bool)
	OutputAck func(n int) (err error)
	// contains filtered or unexported fields
}

func (*FDOperator) Control

func (o *FDOperator) Control(event EpollEvent) error

type LinkBuffer

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

LinkBuffer implements ReadWriter.

func NewLinkBuffer

func NewLinkBuffer(size ...int) *LinkBuffer

NewLinkBuffer size defines the initial capacity, but there is no readable data.

func (*LinkBuffer) Append

func (b *LinkBuffer) Append(w Writer) (err error)

Append implements Writer.

func (*LinkBuffer) Bytes

func (b *LinkBuffer) Bytes() []byte

Bytes returns all the readable bytes of this LinkBuffer.

func (*LinkBuffer) Close

func (b *LinkBuffer) Close() (err error)

Close will recycle all buffer.

func (*LinkBuffer) Flush

func (b *LinkBuffer) Flush() (err error)

Flush will submit all malloc data and must confirm that the allocated bytes have been correctly assigned.

func (*LinkBuffer) GetBytes

func (b *LinkBuffer) GetBytes(p [][]byte) (vs [][]byte)

GetBytes will read and fill the slice p as much as possible.

func (*LinkBuffer) IsEmpty

func (b *LinkBuffer) IsEmpty() (ok bool)

IsEmpty check if this LinkBuffer is empty.

func (*LinkBuffer) Len

func (b *LinkBuffer) Len() int

Len implements Reader.

func (*LinkBuffer) Malloc

func (b *LinkBuffer) Malloc(n int) (buf []byte, err error)

Malloc pre-allocates memory, which is not readable, and becomes readable data after submission(e.g. Flush).

func (*LinkBuffer) MallocAck

func (b *LinkBuffer) MallocAck(n int) (err error)

MallocAck will keep the first n malloc bytes and discard the rest.

func (*LinkBuffer) MallocLen

func (b *LinkBuffer) MallocLen() (length int)

MallocLen implements Writer.

func (*LinkBuffer) Next

func (b *LinkBuffer) Next(n int) (p []byte, err error)

Next implements Reader.

func (*LinkBuffer) Peek

func (b *LinkBuffer) Peek(n int) (p []byte, err error)

Peek does not have an independent lifecycle, and there is no signal to indicate that Peek content can be released, so Peek will not introduce mcache for now.

func (*LinkBuffer) ReadBinary

func (b *LinkBuffer) ReadBinary(n int) (p []byte, err error)

ReadBinary implements Reader.

func (*LinkBuffer) ReadByte

func (b *LinkBuffer) ReadByte() (p byte, err error)

ReadByte implements Reader.

func (*LinkBuffer) ReadBytes

func (b *LinkBuffer) ReadBytes(n int) (p []byte, err error)

func (*LinkBuffer) ReadSlice

func (b *LinkBuffer) ReadSlice(delim byte) (line []byte, err error)

func (*LinkBuffer) ReadString

func (b *LinkBuffer) ReadString(n int) (s string, err error)

ReadString implements Reader.

func (*LinkBuffer) Release

func (b *LinkBuffer) Release() (err error)

Release the node that has been read. b.flush == nil indicates that this LinkBuffer is created by LinkBuffer.Slice

func (*LinkBuffer) Skip

func (b *LinkBuffer) Skip(n int) (err error)

Skip implements Reader.

func (*LinkBuffer) Slice

func (b *LinkBuffer) Slice(n int) (r Reader, err error)

Slice returns a new LinkBuffer, which is a zero-copy slice of this LinkBuffer, and only holds the ability of Reader.

Slice will automatically execute a Release.

func (*LinkBuffer) Until

func (b *LinkBuffer) Until(delim byte) (line []byte, err error)

Until returns a slice ends with the delim in the buffer.

func (*LinkBuffer) WriteBinary

func (b *LinkBuffer) WriteBinary(p []byte) (n int, err error)

WriteBinary implements Writer.

func (*LinkBuffer) WriteBuffer

func (b *LinkBuffer) WriteBuffer(buf *LinkBuffer) (err error)

WriteBuffer will not submit(e.g. Flush) data to ensure normal use of MallocLen. you must actively submit before read the data. The argument buf can't be used after calling WriteBuffer. (set it to nil)

func (*LinkBuffer) WriteByte

func (b *LinkBuffer) WriteByte(p byte) (err error)

WriteByte implements Writer.

func (*LinkBuffer) WriteBytes

func (b *LinkBuffer) WriteBytes(p []byte) (n int, err error)

func (*LinkBuffer) WriteDirect

func (b *LinkBuffer) WriteDirect(p []byte, remainLen int) error

WriteDirect cannot be mixed with WriteString or WriteBinary functions.

func (*LinkBuffer) WriteString

func (b *LinkBuffer) WriteString(s string) (n int, err error)

WriteString implements Writer.

type Listener

type Listener interface {
	net.Listener
	Fd() int
}

func ConvertListener

func ConvertListener(netListener net.Listener) (Listener, error)

type LoadBalance

type LoadBalance int8
const (
	RoundRobin LoadBalance = iota
)

type OnConnect

type OnConnect func(ctx context.Context, conn Conn) error

type OnRead

type OnRead func(ctx context.Context, conn Conn) error

type Option

type Option func(o *options)

type Poller

type Poller interface {
	Poll() error

	Close() error

	Control(operator *FDOperator, event EpollEvent) error
}

type Reader

type Reader interface {
	Next(n int) (p []byte, err error)
	Peek(n int) (buf []byte, err error)
	Skip(n int) (err error)
	ReadSlice(delim byte) (line []byte, err error)
	ReadString(n int) (s string, err error)
	ReadBytes(n int) (p []byte, err error)
	ReadByte() (b byte, err error)
	Slice(n int) (r Reader, err error)
	Release() (err error)
	Len() (length int)
}

type Writer

type Writer interface {
	Malloc(n int) (buf []byte, err error)
	WriteString(s string) (n int, err error)
	WriteBytes(b []byte) (n int, err error)
	WriteByte(b byte) (err error)
	WriteDirect(p []byte, remainCap int) error
	MallocAck(n int) (err error)
	Append(w Writer) (err error)
	Flush() (err error)
	MallocLen() (length int)
}

Jump to

Keyboard shortcuts

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