Documentation ¶
Index ¶
- Constants
- Variables
- func BinaryPow(n int) int
- func BufferReset(b *bytes.Buffer, p []byte)
- func CheckEncoding(enabled bool, opcode uint8, payload []byte) bool
- func ComputeAcceptKey(challengeKey string) string
- func Errors(funcs ...func() error) error
- func FnvNumber[T Integer](x T) uint64
- func FnvString(s string) uint64
- func GetIntersectionElem(a, b []string) string
- func HttpHeaderContains(a, b string) bool
- func HttpHeaderEqual(a, b string) bool
- func InCollection(elem string, elems []string) bool
- func IsSameSlice[T comparable](a, b []T) bool
- func IsZero[T comparable](v T) bool
- func MaskByByte(content []byte, key []byte)
- func MaskXOR(b []byte, key []byte)
- func Max(a, b int) int
- func MethodExists(in any, method string) (reflect.Value, bool)
- func Min(a, b int) int
- func NewMaskKey() [4]byte
- func ReadN(reader io.Reader, data []byte) error
- func SelectValue[T any](ok bool, a, b T) T
- func Split(s string, sep string) []string
- func StringToBytes(s string) []byte
- func ToBinaryNumber[T Integer](n T) T
- func WithDefault[T comparable](rawValue, newValue T) T
- func WriteN(writer io.Writer, content []byte) error
- type BufferPool
- type Buffers
- type Bytes
- type Deque
- func (c *Deque[T]) Back() *Element[T]
- func (c *Deque[T]) Clone() *Deque[T]
- func (c *Deque[T]) Front() *Element[T]
- func (c *Deque[T]) Get(addr Pointer) *Element[T]
- func (c *Deque[T]) InsertAfter(value T, mark Pointer) *Element[T]
- func (c *Deque[T]) InsertBefore(value T, mark Pointer) *Element[T]
- func (c *Deque[T]) Len() int
- func (c *Deque[T]) MoveToBack(addr Pointer)
- func (c *Deque[T]) MoveToFront(addr Pointer)
- func (c *Deque[T]) PopBack() (value T)
- func (c *Deque[T]) PopFront() (value T)
- func (c *Deque[T]) PushBack(value T) *Element[T]
- func (c *Deque[T]) PushFront(value T) *Element[T]
- func (c *Deque[T]) Range(f func(ele *Element[T]) bool)
- func (c *Deque[T]) Remove(addr Pointer)
- func (c *Deque[T]) Reset()
- func (c *Deque[T]) Update(addr Pointer, value T)
- type Element
- type Error
- type Integer
- type NetConn
- type Pair
- type Payload
- type Pointer
- type Pool
- type RandomString
- type Stack
- type StatusCode
Constants ¶
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 = "=" )
const ( ThresholdV1 = 125 ThresholdV2 = math.MaxUint16 ThresholdV3 = math.MaxUint64 )
const MagicNumber = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
MagicNumber WebSocket 握手过程中使用的魔术字符串 the magic string used during the WebSocket handshake
const Nil = 0
Variables ¶
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", ""} )
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 BufferReset ¶
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 ¶
CheckEncoding 检查 payload 的编码是否有效 checks if the encoding of the payload is valid
func ComputeAcceptKey ¶
func Errors ¶
Errors 依次执行传入的函数,返回第一个遇到的错误 executes the passed functions in sequence and returns the first encountered error
func GetIntersectionElem ¶
GetIntersectionElem 获取两个字符串切片 a 和 b 的交集中的一个元素 Gets an element in the intersection of two string slices a and b
func HttpHeaderContains ¶
func HttpHeaderEqual ¶
func InCollection ¶
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 MaskByByte ¶
func MethodExists ¶
MethodExists if nil return false
func NewMaskKey ¶
func NewMaskKey() [4]byte
func ReadN ¶
ReadN 精准地读取 len(data) 个字节, 否则返回错误 reads exactly len(data) bytes, otherwise returns an error
func SelectValue ¶
func Split ¶
Split 分割给定的字符串 s,使用 sep 作为分隔符。空值将会被过滤掉。 Splits the given string s using sep as the separator. Empty values will be filtered out.
func StringToBytes ¶
func ToBinaryNumber ¶
func ToBinaryNumber[T Integer](n T) T
func WithDefault ¶
func WithDefault[T comparable](rawValue, newValue T) T
WithDefault 如果原值为零值, 返回新值, 否则返回原值
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 Deque ¶
type Deque[T any] struct { // contains filtered or unexported fields }
Deque 结构体表示一个双端队列 Deque struct represents 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]) Front ¶
func (c *Deque[T]) Front() *Element[T]
Front 返回队列头部的元素 returns the element at the front of the queue
func (*Deque[T]) InsertAfter ¶
InsertAfter 在指定元素之后插入一个新元素 inserts a new element after the specified element
func (*Deque[T]) InsertBefore ¶
InsertBefore 在指定元素之前插入一个新元素 inserts a new element before the specified element
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 ¶
Range 遍历队列中的每个元素,并对每个元素执行给定的函数 iterates over each element in the deque and executes the given function on each element
type Element ¶
type Element[T any] struct { // contains filtered or unexported fields }
type Error ¶
type Error struct { Err error // 错误信息 Code StatusCode // 状态码 }
func NewError ¶
func NewError(code StatusCode, err error) *Error
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool 泛型内存池 generic 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
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