udp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

Golang high-performance asynchronous UDP (only supports Linux)

Example

package main

import (
	"fmt"
	"syscall"
	"time"

	"golang.org/x/sys/unix"

	"github.com/gotcp/udp"
)

var u *udp.EP

// Asynchronous event
func OnReceive(from unix.Sockaddr, msg []byte, n int) {
	var err = u.WriteWithTimeout(from, msg, 3*time.Second)
	if err != nil {
		fmt.Printf("write error -> %v\n", err)
	}
}

// Asynchronous event
func OnError(code udp.ErrorCode, err error) {
	fmt.Printf("OnError -> %d, %v\n", code, err)
}

func main() {
	var err error

	var rLimit syscall.Rlimit
	if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		panic(err)
	}
	rLimit.Cur = rLimit.Max
	if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		panic(err)
	}

	// parameters: readBuffer, threads, queueLength
	u, err = udp.New(2048, 3500, 4096)
	if err != nil {
		panic(err)
	}
	defer u.Stop()

	u.OnReceive = OnReceive // must have
	u.OnError = OnError     // optional

	u.Start("0.0.0.0", 8003)
}

Documentation

Index

Constants

View Source
const (
	DEFAULT_EPOLL_EVENTS        = 4096
	DEFAULT_EPOLL_READ_TIMEOUT  = 5
	DEFAULT_EPOLL_WRITE_TIMEOUT = 5
	DEFAULT_POOL_MULTIPLE       = 5
	DEFAULT_DELAY               = 200
)

Variables

View Source
var (
	ErrorBufferPool = errors.New("error getting buffer pool item")
)

Functions

func GetAddr

func GetAddr(addr unix.Sockaddr) (string, int)

func GetAddrBytes

func GetAddrBytes(addr unix.Sockaddr) ([]byte, int)

Types

type EP

type EP struct {
	Host        string
	Port        int
	Fd          int
	Threads     int
	QueueLength int
	ReadBuffer  int
	ReuseAddr   int
	ReusePort   int

	OnReceive OnReceiveEvent
	OnError   OnErrorEvent
	// contains filtered or unexported fields
}

func New

func New(readBuffer int, threads int, queueLength int) (*EP, error)

func (*EP) GetBufferPoolItem

func (ep *EP) GetBufferPoolItem() (*[]byte, error)

func (*EP) InvokeError

func (ep *EP) InvokeError(code ErrorCode, err error)

func (*EP) InvokeReceive

func (ep *EP) InvokeReceive(from unix.Sockaddr, msg *[]byte, n int)

func (*EP) PutBufferPoolItem

func (ep *EP) PutBufferPoolItem(buffer *[]byte)

func (*EP) SetDelay

func (ep *EP) SetDelay(t time.Duration)

func (*EP) SetReuseAddr

func (ep *EP) SetReuseAddr(n int)

func (*EP) SetReusePort

func (ep *EP) SetReusePort(n int)

func (*EP) Start

func (ep *EP) Start(host string, port int)

func (*EP) Stop

func (ep *EP) Stop()

func (*EP) Write

func (ep *EP) Write(to unix.Sockaddr, msg []byte) error

func (*EP) WriteWithTimeout

func (ep *EP) WriteWithTimeout(to unix.Sockaddr, msg []byte, timeout time.Duration) error

type ErrorCode

type ErrorCode int
const (
	ERROR_RECEIVE     ErrorCode = 1
	ERROR_BUFFER_POOL ErrorCode = 2
)

type OnErrorEvent

type OnErrorEvent func(code ErrorCode, err error)

type OnReceiveEvent

type OnReceiveEvent func(from unix.Sockaddr, msg []byte, n int)

type OpCode

type OpCode int
const (
	OP_RECEIVE OpCode = 1
	OP_ERROR   OpCode = 2
)

type Request

type Request struct {
	Id      uint64
	Op      OpCode
	From    unix.Sockaddr
	Msg     []byte
	N       int
	ErrCode ErrorCode
	Err     error
}

Jump to

Keyboard shortcuts

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