internal

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PermessageDeflate       = "permessage-deflate"
	ServerMaxWindowBits     = "server_max_window_bits"
	ClientMaxWindowBits     = "client_max_window_bits"
	ServerNoContextTakeover = "server_no_context_takeover"
	ClientNoContextTakeover = "client_no_context_takeover"
	EQ                      = "="
)
View Source
const (
	ThresholdV1 = 125
	ThresholdV2 = math.MaxUint16
	ThresholdV3 = math.MaxUint64
)
View Source
const MagicNumber = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

MagicNumber WebSocket 握手过程中使用的魔术字符串 the magic string used during the WebSocket handshake

View Source
const Nil = 0

Variables

View Source
var (
	SecWebSocketVersion    = Pair{"Sec-WebSocket-Version", "13"}
	SecWebSocketKey        = Pair{"Sec-WebSocket-Key", ""}
	SecWebSocketExtensions = Pair{"Sec-WebSocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
	Connection             = Pair{"Connection", "Upgrade"}
	Upgrade                = Pair{"Upgrade", "websocket"}
	SecWebSocketAccept     = Pair{"Sec-WebSocket-Accept", ""}
	SecWebSocketProtocol   = Pair{"Sec-WebSocket-Protocol", ""}
)
View Source
var (
	// AlphabetNumeric 包含字母和数字字符集的 RandomString 实例
	// It's a RandomString instance with an alphanumeric character set
	AlphabetNumeric = &RandomString{
		layout: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
		r:      rand.New(rand.NewSource(time.Now().UnixNano())),
		mu:     sync.Mutex{},
	}

	// Numeric 仅包含数字字符集的 RandomString 实例
	// It's a RandomString instance with a numeric character set
	Numeric = &RandomString{
		layout: "0123456789",
		r:      rand.New(rand.NewSource(time.Now().UnixNano())),
		mu:     sync.Mutex{},
	}
)

Functions

func BinaryPow

func BinaryPow(n int) int

BinaryPow 返回2的n次方

func BufferReset

func BufferReset(b *bytes.Buffer, p []byte)

BufferReset 重置buffer底层的切片 Reset the buffer's underlying slice 注意:修改后面的属性一定要加偏移量,否则可能会导致未定义的行为。 Note: Be sure to add an offset when modifying the following properties, otherwise it may lead to undefined behavior.

func CheckEncoding

func CheckEncoding(enabled bool, opcode uint8, payload []byte) bool

CheckEncoding 检查 payload 的编码是否有效 checks if the encoding of the payload is valid

func ComputeAcceptKey

func ComputeAcceptKey(challengeKey string) string

func Errors

func Errors(funcs ...func() error) error

Errors 依次执行传入的函数,返回第一个遇到的错误 executes the passed functions in sequence and returns the first encountered error

func FnvNumber

func FnvNumber[T Integer](x T) uint64

func FnvString

func FnvString(s string) uint64

func GetIntersectionElem

func GetIntersectionElem(a, b []string) string

GetIntersectionElem 获取两个字符串切片 a 和 b 的交集中的一个元素 Gets an element in the intersection of two string slices a and b

func HttpHeaderContains

func HttpHeaderContains(a, b string) bool

func HttpHeaderEqual

func HttpHeaderEqual(a, b string) bool

func InCollection

func InCollection(elem string, elems []string) bool

InCollection 检查给定的字符串 elem 是否在字符串切片 elems 中 Checks if the given string elem is in the string slice elems.

func IsSameSlice

func IsSameSlice[T comparable](a, b []T) bool

func IsZero

func IsZero[T comparable](v T) bool

IsZero 零值判断

func MaskByByte

func MaskByByte(content []byte, key []byte)

func MaskXOR

func MaskXOR(b []byte, key []byte)

MaskXOR 计算掩码

func Max

func Max(a, b int) int

func MethodExists

func MethodExists(in any, method string) (reflect.Value, bool)

MethodExists if nil return false

func Min

func Min(a, b int) int

func NewMaskKey

func NewMaskKey() [4]byte

func ReadN

func ReadN(reader io.Reader, data []byte) error

ReadN 精准地读取 len(data) 个字节, 否则返回错误 reads exactly len(data) bytes, otherwise returns an error

func SelectValue

func SelectValue[T any](ok bool, a, b T) T

func Split

func Split(s string, sep string) []string

Split 分割给定的字符串 s,使用 sep 作为分隔符。空值将会被过滤掉。 Splits the given string s using sep as the separator. Empty values will be filtered out.

func StringToBytes

func StringToBytes(s string) []byte

func ToBinaryNumber

func ToBinaryNumber[T Integer](n T) T

func WithDefault

func WithDefault[T comparable](rawValue, newValue T) T

WithDefault 如果原值为零值, 返回新值, 否则返回原值

func WriteN

func WriteN(writer io.Writer, content []byte) error

WriteN 将 content 写入 writer 中 writes the content to the writer

Types

type BufferPool

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

func NewBufferPool

func NewBufferPool(left, right uint32) *BufferPool

NewBufferPool 创建一个内存池 creates a memory pool left 和 right 表示内存池的区间范围,它们将被转换为 2 的 n 次幂 left and right indicate the interval range of the memory pool, they will be transformed into pow(2, n) 小于 left 的情况下,Get 方法将返回至少 left 字节的缓冲区;大于 right 的情况下,Put 方法不会回收缓冲区 Below left, the Get method will return at least left bytes; above right, the Put method will not reclaim the buffer

func (*BufferPool) Get

func (p *BufferPool) Get(n int) *bytes.Buffer

Get 从内存池中获取一个至少 n 字节的缓冲区 fetches a buffer from the memory pool, of at least n bytes

func (*BufferPool) Put

func (p *BufferPool) Put(b *bytes.Buffer)

Put 将缓冲区放回到内存池 returns the buffer to the memory pool

type Buffers

type Buffers [][]byte

func (Buffers) CheckEncoding

func (b Buffers) CheckEncoding(enabled bool, opcode uint8) bool

func (Buffers) Len

func (b Buffers) Len() int

func (Buffers) WriteTo

func (b Buffers) WriteTo(w io.Writer) (int64, error)

WriteTo 可重复写

type Bytes

type Bytes []byte

func (Bytes) CheckEncoding

func (b Bytes) CheckEncoding(enabled bool, opcode uint8) bool

func (Bytes) Len

func (b Bytes) Len() int

func (Bytes) WriteTo

func (b Bytes) WriteTo(w io.Writer) (int64, error)

WriteTo 可重复写

type Deque

type Deque[T any] struct {
	// contains filtered or unexported fields
}

Deque 结构体表示一个双端队列 Deque struct represents a double-ended queue

func New

func New[T any](capacity int) *Deque[T]

New 创建双端队列 creates a double-ended queue

func (*Deque[T]) Back

func (c *Deque[T]) Back() *Element[T]

Back 返回队列尾部的元素 returns the element at the back of the queue

func (*Deque[T]) Clone

func (c *Deque[T]) Clone() *Deque[T]

Clone 深拷贝 deep copy

func (*Deque[T]) Front

func (c *Deque[T]) Front() *Element[T]

Front 返回队列头部的元素 returns the element at the front of the queue

func (*Deque[T]) Get

func (c *Deque[T]) Get(addr Pointer) *Element[T]

Get 根据地址获取元素 retrieves an element based on its address

func (*Deque[T]) InsertAfter

func (c *Deque[T]) InsertAfter(value T, mark Pointer) *Element[T]

InsertAfter 在指定元素之后插入一个新元素 inserts a new element after the specified element

func (*Deque[T]) InsertBefore

func (c *Deque[T]) InsertBefore(value T, mark Pointer) *Element[T]

InsertBefore 在指定元素之前插入一个新元素 inserts a new element before the specified element

func (*Deque[T]) Len

func (c *Deque[T]) Len() int

Len 返回双端队列的长度 returns the length of the deque

func (*Deque[T]) MoveToBack

func (c *Deque[T]) MoveToBack(addr Pointer)

MoveToBack 将指定地址的元素移动到队列尾部 moves the element at the specified address to the back of the deque

func (*Deque[T]) MoveToFront

func (c *Deque[T]) MoveToFront(addr Pointer)

MoveToFront 将指定地址的元素移动到队列头部 moves the element at the specified address to the front of the deque

func (*Deque[T]) PopBack

func (c *Deque[T]) PopBack() (value T)

PopBack 从队列尾部弹出一个元素并返回其值 pops an element from the back of the deque and returns its value

func (*Deque[T]) PopFront

func (c *Deque[T]) PopFront() (value T)

PopFront 从队列头部弹出一个元素并返回其值 pops an element from the front of the deque and returns its value

func (*Deque[T]) PushBack

func (c *Deque[T]) PushBack(value T) *Element[T]

PushBack 将一个元素添加到队列的尾部 adds an element to the back of the deque

func (*Deque[T]) PushFront

func (c *Deque[T]) PushFront(value T) *Element[T]

PushFront 将一个元素添加到队列的头部 adds an element to the front of the deque

func (*Deque[T]) Range

func (c *Deque[T]) Range(f func(ele *Element[T]) bool)

Range 遍历队列中的每个元素,并对每个元素执行给定的函数 iterates over each element in the deque and executes the given function on each element

func (*Deque[T]) Remove

func (c *Deque[T]) Remove(addr Pointer)

Remove 从队列中移除指定地址的元素 removes the element at the specified address from the deque

func (*Deque[T]) Reset

func (c *Deque[T]) Reset()

Reset 重置双端队列 resets the deque

func (*Deque[T]) Update

func (c *Deque[T]) Update(addr Pointer, value T)

Update 更新指定地址的元素的值 updates the value of the element at the specified address

type Element

type Element[T any] struct {
	// contains filtered or unexported fields
}

func (*Element[T]) Addr

func (c *Element[T]) Addr() Pointer

Addr 返回元素的地址 Addr returns the address of the element

func (*Element[T]) Next

func (c *Element[T]) Next() Pointer

Next 返回下一个元素的地址 returns the address of the next element

func (*Element[T]) Prev

func (c *Element[T]) Prev() Pointer

Prev 返回前一个元素的地址 returns the address of the previous element

func (*Element[T]) Value

func (c *Element[T]) Value() T

Value 返回元素的值 returns the value of the element

type Error

type Error struct {
	Err  error      // 错误信息
	Code StatusCode // 状态码
}

func NewError

func NewError(code StatusCode, err error) *Error

func (*Error) Error

func (c *Error) Error() string

type Integer

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

type NetConn

type NetConn interface {
	NetConn() net.Conn
}

type Pair

type Pair struct {
	Key string
	Val string
}

type Payload

type Payload interface {
	io.WriterTo
	Len() int
	CheckEncoding(enabled bool, opcode uint8) bool
}

type Pointer

type Pointer uint32

func (Pointer) IsNil

func (c Pointer) IsNil() bool

IsNil 检查指针是否为空 checks if the pointer is null

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool 泛型内存池 generic pool

func NewPool

func NewPool[T any](f func() T) *Pool[T]

NewPool 创建一个新的泛型内存池 creates a new generic pool

func (*Pool[T]) Get

func (c *Pool[T]) Get() T

Get 从池中获取一个值 gets a value from the pool

func (*Pool[T]) Put

func (c *Pool[T]) Put(v T)

Put 将一个值放入池中 puts a value into the pool

type RandomString

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

RandomString 随机字符串生成器 random string generator

func (*RandomString) Generate

func (c *RandomString) Generate(n int) []byte

Generate 生成一个长度为 n 的随机字节切片 generates a random byte slice of length n

func (*RandomString) Intn

func (c *RandomString) Intn(n int) int

Intn 返回一个 [0, n) 范围内的随机整数 returns a random integer in the range [0, n)

func (*RandomString) Uint32

func (c *RandomString) Uint32() uint32

Uint32 返回一个随机的 uint32 值 returns a random uint32 value

func (*RandomString) Uint64

func (c *RandomString) Uint64() uint64

Uint64 返回一个随机的 uint64 值 returns a random uint64 value

type Stack

type Stack[T any] []T

Stack 泛型栈 generic stack

func (*Stack[T]) Len

func (c *Stack[T]) Len() int

Len 获取栈中元素的数量 returns the number of elements in the stack

func (*Stack[T]) Pop

func (c *Stack[T]) Pop() T

Pop 从栈顶弹出元素并返回其值 removes the top element from the stack and returns its value

func (*Stack[T]) Push

func (c *Stack[T]) Push(v T)

Push 将元素追加到栈顶 appends an element to the top of the stack

type StatusCode

type StatusCode uint16

StatusCode WebSocket错误码 websocket error code

const (
	// CloseNormalClosure 正常关闭; 无论为何目的而创建, 该链接都已成功完成任务.
	CloseNormalClosure StatusCode = 1000

	// CloseGoingAway 终端离开, 可能因为服务端错误, 也可能因为浏览器正从打开连接的页面跳转离开.
	CloseGoingAway StatusCode = 1001

	// CloseProtocolError 由于协议错误而中断连接.
	CloseProtocolError StatusCode = 1002

	// CloseUnsupported 由于接收到不允许的数据类型而断开连接 (如仅接收文本数据的终端接收到了二进制数据).
	CloseUnsupported StatusCode = 1003

	// CloseNoStatusReceived 保留. 表示没有收到预期的状态码.
	CloseNoStatusReceived StatusCode = 1005

	// CloseAbnormalClosure 保留. 用于期望收到状态码时连接非正常关闭 (也就是说, 没有发送关闭帧).
	CloseAbnormalClosure StatusCode = 1006

	// CloseUnsupportedData 由于收到了格式不符的数据而断开连接 (如文本消息中包含了非 UTF-8 数据).
	CloseUnsupportedData StatusCode = 1007

	// ClosePolicyViolation 由于收到不符合约定的数据而断开连接. 这是一个通用状态码, 用于不适合使用 1003 和 1009 状态码的场景.
	ClosePolicyViolation StatusCode = 1008

	// CloseMessageTooLarge 由于收到过大的数据帧而断开连接.
	CloseMessageTooLarge StatusCode = 1009

	// CloseMissingExtension 客户端期望服务器商定一个或多个拓展, 但服务器没有处理, 因此客户端断开连接.
	CloseMissingExtension StatusCode = 1010

	// CloseInternalErr 客户端由于遇到没有预料的情况阻止其完成请求, 因此服务端断开连接.
	CloseInternalErr StatusCode = 1011

	// CloseServiceRestart 服务器由于重启而断开连接. [Ref]
	CloseServiceRestart StatusCode = 1012

	// CloseTryAgainLater 服务器由于临时原因断开连接, 如服务器过载因此断开一部分客户端连接. [Ref]
	CloseTryAgainLater StatusCode = 1013

	// CloseTLSHandshake 保留. 表示连接由于无法完成 TLS 握手而关闭 (例如无法验证服务器证书).
	CloseTLSHandshake StatusCode = 1015
)

func (StatusCode) Bytes

func (c StatusCode) Bytes() []byte

func (StatusCode) Error

func (c StatusCode) Error() string

func (StatusCode) Uint16

func (c StatusCode) Uint16() uint16

Jump to

Keyboard shortcuts

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