netLoop 通过循环来不停接收数据包
()
| 332 | |
| 333 | // netLoop 通过循环来不停接收数据包 |
| 334 | func (c *QQClient) netLoop() { |
| 335 | errCount := 0 |
| 336 | for c.alive { |
| 337 | l, err := c.TCP.ReadInt32() |
| 338 | if err != nil { |
| 339 | time.Sleep(time.Millisecond * 500) |
| 340 | continue |
| 341 | } |
| 342 | if l < 4 || l > 1024*1024*10 { // max 10MB |
| 343 | c.error("parse incoming packet error: invalid packet length %v", l) |
| 344 | errCount++ |
| 345 | if errCount > 2 { |
| 346 | go c.quickReconnect() |
| 347 | } |
| 348 | continue |
| 349 | } |
| 350 | data, _ := c.TCP.ReadBytes(int(l) - 4) |
| 351 | resp, err := c.transport.ReadResponse(data) |
| 352 | // pkt, err := packets.ParseIncomingPacket(data, c.sig.D2Key) |
| 353 | if err != nil { |
| 354 | switch { |
| 355 | case errors.Is(err, network.ErrSessionExpired) || errors.Is(err, network.ErrAuthenticationFailed): |
| 356 | // 返回错误 |
| 357 | go func() { |
| 358 | if f, ok := c.handlers.LoadAndDelete(uint32(resp.SequenceID)); ok { |
| 359 | // does not need decoder |
| 360 | f.fun(nil, err) |
| 361 | } |
| 362 | }() |
| 363 | case errors.Is(err, network.ErrPacketDropped): |
| 364 | fallthrough |
| 365 | default: |
| 366 | c.error("parse incoming packet error: %v", err) |
| 367 | c.Disconnect() |
| 368 | go c.DisconnectedEvent.dispatch(c, &DisconnectedEvent{Message: err.Error()}) |
| 369 | continue |
| 370 | } |
| 371 | errCount++ |
| 372 | if errCount > 2 { |
| 373 | go c.quickReconnect() |
| 374 | } |
| 375 | continue |
| 376 | } |
| 377 | if resp.EncryptType == network.EncryptTypeEmptyKey && strings.HasPrefix(resp.CommandName, "wtlogin") { |
| 378 | m, err := c.oicq.Unmarshal(resp.Body) |
| 379 | if err != nil { |
| 380 | c.error("decrypt payload error: %v", err) |
| 381 | if errors.Is(err, oicq.ErrUnknownFlag) { |
| 382 | go c.quickReconnect() |
| 383 | } |
| 384 | continue |
| 385 | } |
| 386 | resp.Body = m.Body |
| 387 | } |
| 388 | errCount = 0 |
| 389 | c.debug("rev pkt: %v seq: %v", resp.CommandName, resp.SequenceID) |
| 390 | c.stat.PacketReceived.Add(1) |
| 391 | pkt := &network.Packet{ |
no test coverage detected