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

Method StartReader

znet/connection.go:231–292  ·  view source on GitHub ↗

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

()

Source from the content-addressed store, hash-verified

229// StartReader is a goroutine that reads data from the client
230// (读消息Goroutine,用于从客户端中读取数据)
231func (c *Connection) StartReader() {
232 zlog.Ins().InfoF("[Reader Goroutine is running]")
233 defer zlog.Ins().InfoF("%s [conn Reader exit!]", c.RemoteAddr().String())
234 defer c.Stop()
235 defer func() {
236 if err := recover(); err != nil {
237 zlog.Ins().ErrorF("connID=%d, panic err=%v", c.GetConnID(), err)
238 }
239 }()
240
241 //Reduce buffer allocation times to improve efficiency
242 // add by ray 2023-02-03
243 buffer := make([]byte, zconf.GlobalObject.IOReadBuffSize)
244
245 for {
246 select {
247 case <-c.ctx.Done():
248 return
249 default:
250
251 // read data from the connection's IO into the memory buffer
252 // (从conn的IO中读取数据到内存缓冲buffer中)
253 n, err := c.conn.Read(buffer)
254 if err != nil {
255 zlog.Ins().ErrorF("read msg head [read datalen=%d], error = %s", n, err)
256 return
257 }
258 zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n]))
259
260 // If normal data is read from the peer, update the heartbeat detection Active state
261 // (正常读取到对端数据,更新心跳检测Active状态)
262 if n > 0 && c.hc != nil {
263 c.updateActivity()
264 }
265
266 // Deal with the custom protocol fragmentation problem, added by uuxia 2023-03-21
267 // (处理自定义协议断粘包问题)
268 if c.frameDecoder != nil {
269 // Decode the 0-n bytes of data read
270 // (为读取到的0-n个字节的数据进行解码)
271 bufArrays := c.frameDecoder.Decode(buffer[0:n])
272 if bufArrays == nil {
273 continue
274 }
275 for _, bytes := range bufArrays {
276 // zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes))
277 msg := zpack.NewMessage(uint32(len(bytes)), bytes)
278 // Get the current client's Request data
279 // (得到当前客户端请求的Request数据)
280 req := GetRequest(c, msg)
281 c.msgHandler.Execute(req)
282 }
283 } else {
284 msg := zpack.NewMessage(uint32(n), buffer[0:n])
285 // Get the current client's Request data
286 // (得到当前客户端请求的Request数据)
287 req := GetRequest(c, msg)
288 c.msgHandler.Execute(req)

Callers 1

StartMethod · 0.95

Calls 13

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

Tested by

no test coverage detected