You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
  1. package znet
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "github.com/aceld/zinx/ziface"
  7. )
  8. //ConnManager 连接管理模块
  9. type ConnManager struct {
  10. connections map[uint32]ziface.IConnection
  11. connLock sync.RWMutex
  12. }
  13. //NewConnManager 创建一个链接管理
  14. func NewConnManager() *ConnManager {
  15. return &ConnManager{
  16. connections: make(map[uint32]ziface.IConnection),
  17. }
  18. }
  19. //Add 添加链接
  20. func (connMgr *ConnManager) Add(conn ziface.IConnection) {
  21. connMgr.connLock.Lock()
  22. //将conn连接添加到ConnMananger中
  23. connMgr.connections[conn.GetConnID()] = conn
  24. connMgr.connLock.Unlock()
  25. fmt.Println("connection add to ConnManager successfully: conn num = ", connMgr.Len())
  26. }
  27. //Remove 删除连接
  28. func (connMgr *ConnManager) Remove(conn ziface.IConnection) {
  29. connMgr.connLock.Lock()
  30. //删除连接信息
  31. delete(connMgr.connections, conn.GetConnID())
  32. connMgr.connLock.Unlock()
  33. fmt.Println("connection Remove ConnID=", conn.GetConnID(), " successfully: conn num = ", connMgr.Len())
  34. }
  35. //Get 利用ConnID获取链接
  36. func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error) {
  37. connMgr.connLock.RLock()
  38. defer connMgr.connLock.RUnlock()
  39. if conn, ok := connMgr.connections[connID]; ok {
  40. return conn, nil
  41. }
  42. return nil, errors.New("connection not found")
  43. }
  44. //Len 获取当前连接
  45. func (connMgr *ConnManager) Len() int {
  46. connMgr.connLock.RLock()
  47. length := len(connMgr.connections)
  48. connMgr.connLock.RUnlock()
  49. return length
  50. }
  51. //ClearConn 清除并停止所有连接
  52. func (connMgr *ConnManager) ClearConn() {
  53. connMgr.connLock.Lock()
  54. //停止并删除全部的连接信息
  55. for connID, conn := range connMgr.connections {
  56. //停止
  57. conn.Stop()
  58. //删除
  59. delete(connMgr.connections, connID)
  60. }
  61. connMgr.connLock.Unlock()
  62. fmt.Println("Clear All Connections successfully: conn num = ", connMgr.Len())
  63. }
  64. //ClearOneConn 利用ConnID获取一个链接 并且删除
  65. func (connMgr *ConnManager) ClearOneConn(connID uint32) {
  66. connMgr.connLock.Lock()
  67. defer connMgr.connLock.Unlock()
  68. connections := connMgr.connections
  69. if conn, ok := connections[connID]; ok {
  70. //停止
  71. conn.Stop()
  72. //删除
  73. delete(connections, connID)
  74. fmt.Println("Clear Connections ID: ", connID, "succeed")
  75. return
  76. }
  77. fmt.Println("Clear Connections ID: ", connID, "err")
  78. return
  79. }