type Conn struct{
conn net.Conn // 底层网络连接
isServer bool// 如果这个连接作为服务器端的连接则为true,如果是客户端则为false
subprotocol string// 代表WebSocket连接中协商的子协议// Write fields (写操作相关字段)
mu chanstruct{}// used as mutex to protect write to conn(用作互斥锁保护对连接的写操作)
writeBuf []byte// frame is constructed in this buffer.(字节切片,用于构造要写入的帧)
writePool BufferPool // 提供和管理写缓冲区的池
writeBufSize int// 写缓冲区的大小
writeDeadline time.Time // 写操作的截止时间
writer io.WriteCloser // the current writer returned to the application(当前返回给应用程序的写入器)
isWriting bool// for best-effort concurrent write detection(用于尽最大努力检测并发写操作)
writeErrMu sync.Mutex // 用于保护写操作错误的互斥锁
writeErr error// 保存写操作中发生的错误
enableWriteCompression bool// 指示是否启用写操作压缩
compressionLevel int// 写操作压缩的级别
newCompressionWriter func(io.WriteCloser,int) io.WriteCloser // 用于创建新的压缩写入器// Read fields(读操作相关字段)
reader io.ReadCloser // the current reader returned to the application (当前 返回给应用程序的读取器)
readErr error// 保存读操作中发生的错误
br *bufio.Reader // 带缓冲的读取器// bytes remaining in current frame. // set setReadRemaining to safely update this value and prevent overflow
readRemaining int64// 当前帧剩余的字节数
readFinal bool// true the current message has more frames.(指示当前消息是否有更多帧)
readLength int64// Message size. // 消息大小
readLimit int64// Maximum message size. // 消息的最大大小
readMaskPos int// 掩码在消息中的位置
readMaskKey [4]byte// 用于WebSocket消息掩码
handlePong func(string)error// 处理Pong帧的回调函数
handlePing func(string)error// 处理Ping帧的回调函数
handleClose func(int,string)error// 处理关闭帧的回调函数
readErrCount int// 记录读取错误的次数
messageReader *messageReader // the current low-level reader(当前的底层消息读取器)
readDecompress bool// whether last read frame had RSV1 set(指示最后读取的帧是否设置了RSV1(用于压缩))
newDecompressionReader func(io.Reader) io.ReadCloser // 用于创建新的解压缩读取器}
之后我们来看一个关键的函数ReadMessage
// ReadMessage is a helper method for getting a reader using NextReader and// reading from that reader to a buffer.func(c *Conn)ReadMessage()(messageType int, p []byte, err error){var r io.Reader
messageType, r, err = c.NextReader()if err !=nil{return messageType,nil, err
}
p, err = io.ReadAll(r)return messageType, p, err
}
// NextReader returns the next data message received from the peer. The// returned messageType is either TextMessage or BinaryMessage.//// There can be at most one open reader on a connection. NextReader discards// the previous message if the application has not already consumed it.//// Applications must break out of the application's read loop when this method// returns a non-nil error value. Errors returned from this method are// permanent. Once this method returns a non-nil error, all subsequent calls to// this method return the same error.func(c *Conn)NextReader()(messageType int, r io.Reader, err error){// Close previous reader, only relevant for decompression.if c.reader !=nil{_= c.reader.Close()
c.reader =nil}
c.messageReader =nil
c.readLength =0for c.readErr ==nil{
frameType, err := c.advanceFrame()if err !=nil{
c.readErr = err
break}if frameType == TextMessage || frameType == BinaryMessage {
c.messageReader =&messageReader{c}
c.reader = c.messageReader
if c.readDecompress {
c.reader = c.newDecompressionReader(c.reader)}return frameType, c.reader,nil}}// Applications that do handle the error returned from this method spin in// tight loop on connection failure. To help application developers detect// this error, panic on repeated reads to the failed connection.
c.readErrCount++if c.readErrCount >=1000{panic("repeated read on failed websocket connection")}return noFrame,nil, c.readErr
}
【LetMeFly】2581.统计可能的树根数目:换根DP(树形DP)
力扣题目链接:https://leetcode.cn/problems/count-number-of-possible-root-nodes/
Alice 有一棵 n 个节点的树,节点编号为 0 到 n - 1 。树用一个长度为 n - 1 的二维整数数组 edges…
R语言安装和简单入门HelloWorld用法
#R语言安装地址 https://www.r-project.org/ click->CRAN mirror->选择China下列表: https://mirrors.tuna.tsinghua.edu.cn/CRAN/
选择Download R for Windows 选择base Download R-4.3.2 for Windows 下载文件R-4.3.2-…