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

Method StartReader

znet/kcp_connection.go:214–273  ·  view source on GitHub ↗

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

()

Source from the content-addressed store, hash-verified

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

Callers 1

StartMethod · 0.95

Calls 12

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

Tested by

no test coverage detected