| 500 | } |
| 501 | |
| 502 | func (c *KcpConnection) finalizer() { |
| 503 | // If the connection has already been closed |
| 504 | if c.isClosed() == true { |
| 505 | return |
| 506 | } |
| 507 | |
| 508 | //set closed |
| 509 | if !c.setClose() { |
| 510 | return |
| 511 | } |
| 512 | |
| 513 | // Call the callback function registered by the user when closing the connection if it exists |
| 514 | //(如果用户注册了该连接的 关闭回调业务,那么在此刻应该显示调用) |
| 515 | c.callOnConnStop() |
| 516 | |
| 517 | c.msgLock.Lock() |
| 518 | defer c.msgLock.Unlock() |
| 519 | |
| 520 | // Stop the heartbeat detector associated with the connection |
| 521 | if c.hc != nil { |
| 522 | c.hc.Stop() |
| 523 | } |
| 524 | |
| 525 | // Close the socket connection |
| 526 | _ = c.conn.Close() |
| 527 | |
| 528 | // Remove the connection from the connection manager |
| 529 | if c.connManager != nil { |
| 530 | c.connManager.Remove(c) |
| 531 | } |
| 532 | |
| 533 | // Close all channels associated with the connection |
| 534 | if c.msgBuffChan != nil { |
| 535 | close(c.msgBuffChan) |
| 536 | } |
| 537 | |
| 538 | go func() { |
| 539 | defer func() { |
| 540 | if err := recover(); err != nil { |
| 541 | zlog.Ins().ErrorF("Conn finalizer panic: %v", err) |
| 542 | } |
| 543 | }() |
| 544 | |
| 545 | c.InvokeCloseCallbacks() |
| 546 | }() |
| 547 | |
| 548 | zlog.Ins().DebugF("Conn Stop()...ConnID = %d", c.connID) |
| 549 | } |
| 550 | |
| 551 | func (c *KcpConnection) callOnConnStart() { |
| 552 | if c.onConnStart != nil { |