util

package
v4.11.13 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 25 Imported by: 38

Documentation

Index

Constants

View Source
const (
	AMF0_NUMBER = iota // 浮点数
	AMF0_BOOLEAN
	AMF0_STRING
	AMF0_OBJECT
	AMF0_MOVIECLIP
	AMF0_NULL
	AMF0_UNDEFINED
	AMF0_REFERENCE
	AMF0_ECMA_ARRAY
	AMF0_END_OBJECT
	AMF0_STRICT_ARRAY
	AMF0_DATE
	AMF0_LONG_STRING
	AMF0_UNSUPPORTED
	AMF0_RECORDSET
	AMF0_XML_DOCUMENT
	AMF0_TYPED_OBJECT
	AMF0_AVMPLUS_OBJECT
)
View Source
const (
	AMF3_UNDEFINED = iota
	AMF3_NULL
	AMF3_FALSE
	AMF3_TRUE
	AMF3_INTEGER
	AMF3_DOUBLE
	AMF3_STRING
	AMF3_XML_DOC
	AMF3_DATE
	AMF3_ARRAY
	AMF3_OBJECT
	AMF3_XML
	AMF3_BYTE_ARRAY
	AMF3_VECTOR_INT
	AMF3_VECTOR_UINT
	AMF3_VECTOR_DOUBLE
	AMF3_VECTOR_OBJECT
	AMF3_DICTIONARY
)

Variables

View Source
var (
	END_OBJ   = []byte{0, 0, AMF0_END_OBJECT}
	ObjectEnd = &struct{}{}
	Undefined = &struct{}{}
)
View Source
var BigEndian bigEndian

BigEndian is the big-endian implementation of ByteOrder.

View Source
var Crc32_Table = []uint32{}/* 256 elements not displayed */
View Source
var LittleEndian littleEndian
View Source
var Null = struct{}{}
View Source
var RTPReorderBufferLen uint16 = 50

Functions

func BasicAuth

func BasicAuth(u, p string, next http.Handler) http.Handler

func BigLittleSwap

func BigLittleSwap(v uint) uint

func Bit1

func Bit1(b byte, index int) bool

Bit1 检查字节中的某一位是否为1 |0|1|2|3|4|5|6|7|

func ByteToUint32

func ByteToUint32(data []byte, bigEndian bool) (ret uint32, err error)

千万注意大小端,RTMP是大端

func ByteToUint32N

func ByteToUint32N(data []byte) (ret uint32, err error)

// 千万注意大小端,RTMP是大端

func ByteToUint64N

func ByteToUint64N(data []byte) (ret uint64, err error)

// 千万注意大小端,RTMP是大端

func CORS

func CORS(next http.Handler) http.Handler

CORS 加入跨域策略头包含CORP

func Clone

func Clone[T any](x T) *T

func ConcatBuffers

func ConcatBuffers[T ~[]byte](input []T) (out []byte)

ConcatBuffers 合并碎片内存为一个完整内存

func ConvertNum

func ConvertNum[F Integer, T Integer](from F, to T) T

func CreateShutdownScript

func CreateShutdownScript() error

func CurrentDir

func CurrentDir(path ...string) string

func Exist

func Exist(filename string) bool

检查文件或目录是否存在 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false

func GetBE

func GetBE[T Integer](b []byte, num *T) T

func GetFillBytes

func GetFillBytes(data byte, n int) []byte

func GetPCR

func GetPCR(v uint64) uint64

func GetPtsDts

func GetPtsDts(v uint64) uint64

func GetUev

func GetUev(buff []byte, start int) (value int, pos int)

哥伦布解码

func LastElement

func LastElement[T any](s []T) T

func ListenUDP

func ListenUDP(address string, networkBuffer int) (*net.UDPConn, error)

func MallocSlice added in v4.9.9

func MallocSlice[T any](slice *[]T) *T

MallocSlice 用来对容量够的slice进行长度扩展+1,并返回新的位置的指针,用于写入

func MapList added in v4.5.1

func MapList[K comparable, V any, R any](m *Map[K, V], f func(K, V) R) (r []R)

func MarshalAMFs added in v4.11.0

func MarshalAMFs(v ...any) []byte

func PutBE

func PutBE[T Integer](b []byte, num T) []byte

func PutPCR

func PutPCR(pcr uint64) uint64

func PutPtsDts

func PutPtsDts(v uint64) uint64

func ReadAMF added in v4.11.0

func ReadAMF[T string | float64 | bool | map[string]any](amf *AMF) (result T)

func ReadBE

func ReadBE[T Integer](b []byte) (num T)

func ReadByteToUint16

func ReadByteToUint16(r io.Reader, bigEndian bool) (data uint16, err error)

func ReadByteToUint24

func ReadByteToUint24(r io.Reader, bigEndian bool) (data uint32, err error)

func ReadByteToUint32

func ReadByteToUint32(r io.Reader, bigEndian bool) (data uint32, err error)

func ReadByteToUint40

func ReadByteToUint40(r io.Reader, bigEndian bool) (data uint64, err error)

func ReadByteToUint48

func ReadByteToUint48(r io.Reader, bigEndian bool) (data uint64, err error)

func ReadByteToUint64

func ReadByteToUint64(r io.Reader, bigEndian bool) (data uint64, err error)

func ReadByteToUint8

func ReadByteToUint8(r io.Reader) (data uint8, err error)

func Retry

func Retry(attempts int, sleep time.Duration, f func() error) error

func RetryStopErr

func RetryStopErr(err error) retryStop

func ReturnJson

func ReturnJson[T any](fetch func() T, tickDur time.Duration, rw http.ResponseWriter, r *http.Request)

func SizeOfBuffers

func SizeOfBuffers[T ~[]byte](buf []T) (size int)

SizeOfBuffers 计算Buffers的内容长度

func SplitBuffers

func SplitBuffers[T ~[]byte](buf []T, size int) (result [][]T)

SplitBuffers 按照一定大小分割 Buffers

func ToFloat64

func ToFloat64(num interface{}) float64

func Uint32ToByte

func Uint32ToByte(data uint32, bigEndian bool) (ret []byte, err error)

func WaitTerm

func WaitTerm(cancel context.CancelFunc)

func WriteUint16ToByte

func WriteUint16ToByte(w io.Writer, data uint16, bigEndian bool) error

func WriteUint24ToByte

func WriteUint24ToByte(w io.Writer, data uint32, bigEndian bool) error

func WriteUint32ToByte

func WriteUint32ToByte(w io.Writer, data uint32, bigEndian bool) error

func WriteUint40ToByte

func WriteUint40ToByte(w io.Writer, data uint64, bigEndian bool) error

func WriteUint48ToByte

func WriteUint48ToByte(w io.Writer, data uint64, bigEndian bool) error

func WriteUint64ToByte

func WriteUint64ToByte(w io.Writer, data uint64, bigEndian bool) error

func WriteUint8ToByte

func WriteUint8ToByte(w io.Writer, data uint8) error

Types

type AMF added in v4.11.0

type AMF struct {
	Buffer
}

func (*AMF) Marshal added in v4.11.0

func (amf *AMF) Marshal(v any) []byte

func (*AMF) Marshals added in v4.11.0

func (amf *AMF) Marshals(v ...any) []byte

func (*AMF) ReadBool added in v4.11.0

func (amf *AMF) ReadBool() (result bool)

func (*AMF) ReadNumber added in v4.11.0

func (amf *AMF) ReadNumber() (result float64)

func (*AMF) ReadObject added in v4.11.0

func (amf *AMF) ReadObject() (result map[string]any)

func (*AMF) ReadShortString added in v4.11.0

func (amf *AMF) ReadShortString() (result string)

func (*AMF) Unmarshal added in v4.11.0

func (amf *AMF) Unmarshal() (obj any, err error)

type BLL added in v4.11.0

type BLL struct {
	List[Buffer]
	ByteLength int
}

ByteLinkList

func (*BLL) Clear added in v4.11.0

func (list *BLL) Clear()

func (*BLL) GetByte added in v4.11.0

func (list *BLL) GetByte(index int) (b byte)

func (*BLL) GetUint24 added in v4.11.0

func (list *BLL) GetUint24(index int) uint32

func (*BLL) GetUintN added in v4.11.0

func (list *BLL) GetUintN(index int, n int) (result uint32)

func (*BLL) NewReader added in v4.11.0

func (list *BLL) NewReader() *BLLReader

func (*BLL) Push added in v4.11.0

func (list *BLL) Push(item *ListItem[Buffer])

func (*BLL) Recycle added in v4.11.0

func (list *BLL) Recycle()

全部回收掉

func (*BLL) Shift added in v4.11.0

func (list *BLL) Shift() (item *ListItem[Buffer])

func (*BLL) ToBuffers added in v4.11.0

func (list *BLL) ToBuffers() (result net.Buffers)

func (*BLL) ToBytes added in v4.11.0

func (list *BLL) ToBytes() (b []byte)

func (*BLL) WriteTo added in v4.11.0

func (list *BLL) WriteTo(w io.Writer) (int64, error)

type BLLReader added in v4.11.0

type BLLReader struct {
	*ListItem[Buffer]
	// contains filtered or unexported fields
}

func (*BLLReader) CanRead added in v4.11.0

func (r *BLLReader) CanRead() bool

func (*BLLReader) ReadBE added in v4.11.0

func (r *BLLReader) ReadBE(n int) (be uint32, err error)

func (*BLLReader) ReadByte added in v4.11.0

func (r *BLLReader) ReadByte() (b byte, err error)

func (*BLLReader) ReadN added in v4.11.0

func (r *BLLReader) ReadN(n int) (result net.Buffers)

func (*BLLReader) Skip added in v4.11.0

func (r *BLLReader) Skip(n int) (err error)

type BLLs added in v4.11.0

type BLLs struct {
	List[*BLL]
	ByteLength int
}

func (*BLLs) NewReader added in v4.11.0

func (list *BLLs) NewReader() *BLLsReader

func (*BLLs) Push added in v4.11.0

func (list *BLLs) Push(item *ListItem[Buffer])

func (*BLLs) PushValue added in v4.11.0

func (list *BLLs) PushValue(item *BLL)

func (*BLLs) Recycle added in v4.11.0

func (list *BLLs) Recycle()

func (*BLLs) ToBuffers added in v4.11.5

func (list *BLLs) ToBuffers() (result net.Buffers)

func (*BLLs) ToBytes added in v4.11.0

func (list *BLLs) ToBytes() (result []byte)

func (*BLLs) ToList added in v4.11.0

func (list *BLLs) ToList() (result [][][]byte)

type BLLsReader added in v4.11.0

type BLLsReader struct {
	*ListItem[*BLL]
	BLLReader
}

func (*BLLsReader) CanRead added in v4.11.0

func (r *BLLsReader) CanRead() bool

func (*BLLsReader) ReadByte added in v4.11.0

func (r *BLLsReader) ReadByte() (b byte, err error)

type Buffer

type Buffer []byte

func (Buffer) CanRead

func (b Buffer) CanRead() bool

func (Buffer) CanReadN added in v4.9.9

func (b Buffer) CanReadN(n int) bool

func (Buffer) Cap

func (b Buffer) Cap() int

func (*Buffer) Glow

func (b *Buffer) Glow(n int)

func (Buffer) Len

func (b Buffer) Len() int

func (*Buffer) Malloc

func (b *Buffer) Malloc(count int) Buffer

func (*Buffer) MarshalAMFs added in v4.11.0

func (b *Buffer) MarshalAMFs(v ...any)

func (*Buffer) Read added in v4.11.5

func (b *Buffer) Read(buf []byte) (n int, err error)

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() byte

func (*Buffer) ReadFloat64

func (b *Buffer) ReadFloat64() float64

func (*Buffer) ReadN

func (b *Buffer) ReadN(n int) Buffer

func (*Buffer) ReadUint16

func (b *Buffer) ReadUint16() uint16

func (*Buffer) ReadUint24

func (b *Buffer) ReadUint24() uint32

func (*Buffer) ReadUint32

func (b *Buffer) ReadUint32() uint32

func (*Buffer) ReadUint64

func (b *Buffer) ReadUint64() uint64

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) Split added in v4.11.0

func (b *Buffer) Split(n int) (result net.Buffers)

func (Buffer) SubBuf

func (b Buffer) SubBuf(start int, length int) Buffer

func (*Buffer) Write

func (b *Buffer) Write(a []byte) (n int, err error)

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(v byte)

func (*Buffer) WriteFloat64

func (b *Buffer) WriteFloat64(v float64)

func (*Buffer) WriteString

func (b *Buffer) WriteString(a string)

func (*Buffer) WriteUint16

func (b *Buffer) WriteUint16(v uint16)

func (*Buffer) WriteUint24

func (b *Buffer) WriteUint24(v uint32)

func (*Buffer) WriteUint32

func (b *Buffer) WriteUint32(v uint32)

type BytesPool added in v4.11.0

type BytesPool []List[Buffer]

func (BytesPool) Get added in v4.11.0

func (p BytesPool) Get(size int) (item *ListItem[Buffer])

func (BytesPool) GetShell added in v4.11.0

func (p BytesPool) GetShell(b []byte) (item *ListItem[Buffer])

获取来自真实内存的切片的——假内存块,即只回收外壳

type Crc32Reader

type Crc32Reader struct {
	R     io.Reader
	Crc32 uint32
}

func (*Crc32Reader) Read

func (cr *Crc32Reader) Read(b []byte) (n int, err error)

func (*Crc32Reader) ReadCrc32UIntAndCheck

func (cr *Crc32Reader) ReadCrc32UIntAndCheck() (err error)

type Crc32Writer

type Crc32Writer struct {
	W     io.Writer
	Crc32 uint32
}

func (*Crc32Writer) Write

func (wr *Crc32Writer) Write(b []byte) (n int, err error)

type EcmaArray added in v4.11.0

type EcmaArray map[string]any

type IList added in v4.11.0

type IList[T any] interface {
	Push(*ListItem[T])
	Shift() *ListItem[T]
	Clear()
}

type IOVec

type IOVec struct {
	Data   [][]byte
	Length int
	// contains filtered or unexported fields
}

func (*IOVec) Append

func (iov *IOVec) Append(b []byte)

func (*IOVec) WriteTo

func (iov *IOVec) WriteTo(w io.Writer, n int) (written int, err error)

type IOVecWriter

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

func NewIOVecWriter

func NewIOVecWriter(w io.Writer) (iow *IOVecWriter)

func (*IOVecWriter) Flush

func (iow *IOVecWriter) Flush() error

func (*IOVecWriter) Write

func (iow *IOVecWriter) Write(data []byte) (written int, err error)

type Integer

type Integer interface {
	~int | ~int16 | ~int32 | ~int64 | ~uint | ~uint16 | ~uint32 | ~uint64
}

type List added in v4.11.0

type List[T any] struct {
	ListItem[T]
	Length int
}

func (*List[T]) Clear added in v4.11.0

func (p *List[T]) Clear()

func (*List[T]) PoolShift added in v4.11.0

func (p *List[T]) PoolShift() (head *ListItem[T])

func (*List[T]) Push added in v4.11.0

func (p *List[T]) Push(item *ListItem[T])

func (*List[T]) PushValue added in v4.11.0

func (p *List[T]) PushValue(value T)

func (*List[T]) Range added in v4.11.0

func (p *List[T]) Range(do func(value T) bool)

func (*List[T]) RangeItem added in v4.11.0

func (p *List[T]) RangeItem(do func(*ListItem[T]) bool)

func (*List[T]) Recycle added in v4.11.0

func (p *List[T]) Recycle()

func (*List[T]) Shift added in v4.11.0

func (p *List[T]) Shift() (head *ListItem[T])

func (*List[T]) ShiftValue added in v4.11.0

func (p *List[T]) ShiftValue() T

func (*List[T]) Transfer added in v4.11.0

func (p *List[T]) Transfer(target IList[T])

Transfer 把链表中的所有元素转移到另一个链表中

func (*List[T]) Unshift added in v4.11.0

func (p *List[T]) Unshift(item *ListItem[T])

func (*List[T]) UnshiftValue added in v4.11.0

func (p *List[T]) UnshiftValue(value T)

type ListItem added in v4.11.0

type ListItem[T any] struct {
	Value     T
	Next, Pre *ListItem[T]
	Pool      *List[T] // 回收池
	// contains filtered or unexported fields
}

func (*ListItem[T]) InsertAfter added in v4.11.0

func (item *ListItem[T]) InsertAfter(insert *ListItem[T])

func (*ListItem[T]) InsertAfterValue added in v4.11.0

func (item *ListItem[T]) InsertAfterValue(value T) (result *ListItem[T])

func (*ListItem[T]) InsertBefore added in v4.11.0

func (item *ListItem[T]) InsertBefore(insert *ListItem[T])

func (*ListItem[T]) InsertBeforeValue added in v4.11.0

func (item *ListItem[T]) InsertBeforeValue(value T) (result *ListItem[T])

func (*ListItem[T]) IsRoot added in v4.11.0

func (item *ListItem[T]) IsRoot() bool

func (*ListItem[T]) Range added in v4.11.0

func (item *ListItem[T]) Range(do func(value T) bool)

func (*ListItem[T]) RangeItem added in v4.11.0

func (item *ListItem[T]) RangeItem(do func(*ListItem[T]) bool)

func (*ListItem[T]) Recycle added in v4.11.0

func (item *ListItem[T]) Recycle()

type Map

type Map[K comparable, V any] struct {
	sync.RWMutex
	Map map[K]V
}

func (*Map[K, V]) Add

func (m *Map[K, V]) Add(k K, v V) bool

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(k K) (v V, ok bool)

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) V

func (*Map[K, V]) Has

func (m *Map[K, V]) Has(k K) (ok bool)

func (*Map[K, V]) Init

func (m *Map[K, V]) Init()

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

func (*Map[K, V]) ModifyRange added in v4.5.1

func (m *Map[K, V]) ModifyRange(f func(K, V))

遍历时有写入操作

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(K, V))

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

func (*Map[K, V]) ToList

func (m *Map[K, V]) ToList() (r []V)

type Pool added in v4.11.0

type Pool[T any] List[T]

func (*Pool[T]) Get added in v4.11.0

func (p *Pool[T]) Get() (item *ListItem[T])

type Promise

type Promise[S any] struct {
	Value S
	// contains filtered or unexported fields
}

func NewPromise

func NewPromise[S any](value S) *Promise[S]

func (*Promise[S]) Await added in v4.9.0

func (p *Promise[S]) Await() error

func (*Promise[S]) Reject

func (r *Promise[S]) Reject(err error)

func (*Promise[S]) Resolve

func (r *Promise[S]) Resolve()

type RTPReorder added in v4.7.7

type RTPReorder[T comparable] struct {
	Total uint32 // 总共收到的包数量
	Drop  uint32 // 丢弃的包数量
	// contains filtered or unexported fields
}

RTPReorder RTP包乱序重排

func (*RTPReorder[T]) Pop added in v4.7.7

func (p *RTPReorder[T]) Pop() (result T)

Pop 从缓存中取出一个包,需要连续调用直到返回nil

func (*RTPReorder[T]) Push added in v4.7.7

func (p *RTPReorder[T]) Push(seq uint16, v T) (result T)

type Recyclable added in v4.11.10

type Recyclable interface {
	Recycle()
}

type Ring

type Ring[T any] struct {
	Value T // for use by client; untouched by this library
	// contains filtered or unexported fields
}

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.

func NewRing

func NewRing[T any](n int) *Ring[T]

New creates a ring of n elements.

func (*Ring[T]) Do

func (r *Ring[T]) Do(f func(T))

Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[T]) Move

func (r *Ring[T]) Move(n int) *Ring[T]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

func (*Ring[T]) Next

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

func (*Ring[T]) Prev

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

type SSE

type SSE struct {
	http.ResponseWriter
	context.Context
}

func NewSSE

func NewSSE(w http.ResponseWriter, ctx context.Context) *SSE

func (*SSE) Write

func (sse *SSE) Write(data []byte) (n int, err error)

func (*SSE) WriteEvent

func (sse *SSE) WriteEvent(event string, data []byte) (err error)

func (*SSE) WriteExec

func (sse *SSE) WriteExec(cmd *exec.Cmd) error

func (*SSE) WriteJSON

func (sse *SSE) WriteJSON(data any) error

type SafeChan

type SafeChan[T any] struct {
	C chan T
	// contains filtered or unexported fields
}

SafeChan安全的channel,可以防止close后被写入的问题

func (*SafeChan[T]) Close

func (sc *SafeChan[T]) Close() bool

Close senders为0的时候可以安全关闭,否则不能关闭

func (*SafeChan[T]) Init

func (sc *SafeChan[T]) Init(n int)

func (*SafeChan[T]) IsClosed

func (sc *SafeChan[T]) IsClosed() bool

func (*SafeChan[T]) IsEmpty

func (sc *SafeChan[T]) IsEmpty() bool

func (*SafeChan[T]) IsFull

func (sc *SafeChan[T]) IsFull() bool

func (*SafeChan[T]) Send

func (sc *SafeChan[T]) Send(v T) bool

type Slice

type Slice[T comparable] []T

func (*Slice[T]) Add

func (s *Slice[T]) Add(v T)

func (*Slice[T]) Delete

func (s *Slice[T]) Delete(v T) bool

func (Slice[T]) Len

func (s Slice[T]) Len() int

func (*Slice[T]) Reset

func (s *Slice[T]) Reset()

func (*Slice[T]) ResetAppend

func (s *Slice[T]) ResetAppend(first T)

type SysIOVec

type SysIOVec struct {
	Base   uintptr
	Length uint64
}

Directories

Path Synopsis
pio

Jump to

Keyboard shortcuts

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