MCPcopy
hub / github.com/aceld/zinx / StartReader

Method StartReader

znet/ws_connection.go:210–272  ·  view source on GitHub ↗

StartReader is a Goroutine that reads messages from the client. (StartReader 读消息Goroutine,用于从客户端中读取数据)

()

Source from the content-addressed store, hash-verified

208// StartReader is a Goroutine that reads messages from the client.
209// (StartReader 读消息Goroutine,用于从客户端中读取数据)
210func (c *WsConnection) StartReader() {
211 zlog.Ins().InfoF("[Reader Goroutine is running]")
212 defer zlog.Ins().InfoF("%s [conn Reader exit!]", c.RemoteAddr().String())
213 defer c.Stop()
214
215 // Create a pack-unpack object. (创建拆包解包的对象)
216 for {
217 select {
218 case <-c.ctx.Done():
219 return
220 default:
221 // add by uuxia 2023-02-03
222 // Read data from the conn's IO to the memory buffer.
223 // (从conn的IO中读取数据到内存缓冲buffer中)
224 messageType, buffer, err := c.conn.ReadMessage()
225 if err != nil {
226 c.cancel()
227 return
228 }
229 if messageType == websocket.PingMessage {
230 c.updateActivity()
231 continue
232 }
233 n := len(buffer)
234 if err != nil {
235 zlog.Ins().ErrorF("read msg head [read datalen=%d], error = %s", n, err.Error())
236 return
237 }
238 zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n]))
239
240 // Update the Active status of heartbeat detection normally after reading data from the peer.
241 // (正常读取到对端数据,更新心跳检测Active状态)
242 if n > 0 && c.hc != nil {
243 c.updateActivity()
244 }
245
246 // Handle custom protocol fragmentation and packet sticking issues add by uuxia 2023-03-21
247 // (处理自定义协议断粘包问题)
248 if c.frameDecoder != nil {
249 // Decode the 0-n bytes of data read.
250 // (为读取到的0-n个字节的数据进行解码)
251 bufArrays := c.frameDecoder.Decode(buffer)
252 if bufArrays == nil {
253 continue
254 }
255 for _, bytes := range bufArrays {
256 zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes))
257 msg := zpack.NewMessage(uint32(len(bytes)), bytes)
258 // Get the Request data requested by the current client.
259 // (得到当前客户端请求的Request数据)
260 req := GetRequest(c, msg)
261 c.msgHandler.Execute(req)
262 }
263 } else {
264 msg := zpack.NewMessage(uint32(n), buffer[0:n])
265 // Get the Request data requested by the current client.
266 // (得到当前客户端请求的Request数据)
267 req := GetRequest(c, msg)

Callers 1

StartMethod · 0.95

Calls 13

RemoteAddrMethod · 0.95
StopMethod · 0.95
updateActivityMethod · 0.95
InsFunction · 0.92
NewMessageFunction · 0.92
GetRequestFunction · 0.85
ErrorMethod · 0.80
InfoFMethod · 0.65
ErrorFMethod · 0.65
DebugFMethod · 0.65
DecodeMethod · 0.65
ExecuteMethod · 0.65

Tested by

no test coverage detected