aceld
6 years ago
4 changed files with 108 additions and 24 deletions
@ -0,0 +1,12 @@ |
|||
package ziface |
|||
|
|||
/* |
|||
连接管理抽象层 |
|||
*/ |
|||
type IConnManager interface { |
|||
Add(conn IConnection) //添加链接
|
|||
Remove(conn IConnection) //删除连接
|
|||
Get(connID uint32) (IConnection, error) //利用ConnID获取链接
|
|||
Len() int //获取当前连接
|
|||
ClearConn() //删除并停止所有链接
|
|||
} |
@ -0,0 +1,85 @@ |
|||
package znet |
|||
|
|||
import ( |
|||
"errors" |
|||
"fmt" |
|||
"sync" |
|||
"zinx/ziface" |
|||
) |
|||
|
|||
/* |
|||
连接管理模块 |
|||
*/ |
|||
type ConnManager struct { |
|||
connections map[uint32]ziface.IConnection //管理的连接信息
|
|||
connLock sync.RWMutex //读写连接的读写锁
|
|||
} |
|||
|
|||
/* |
|||
创建一个链接管理 |
|||
*/ |
|||
func NewConnManager() *ConnManager { |
|||
return &ConnManager{ |
|||
connections:make(map[uint32] ziface.IConnection), |
|||
} |
|||
} |
|||
|
|||
//添加链接
|
|||
func (connMgr *ConnManager) Add(conn ziface.IConnection) { |
|||
//保护共享资源Map 加写锁
|
|||
connMgr.connLock.Lock() |
|||
defer connMgr.connLock.Unlock() |
|||
|
|||
//将conn连接添加到ConnMananger中
|
|||
connMgr.connections[conn.GetConnID()] = conn |
|||
|
|||
fmt.Println("connection add to ConnManager successfully: conn num = ", connMgr.Len()) |
|||
} |
|||
|
|||
//删除连接
|
|||
func (connMgr *ConnManager) Remove(conn ziface.IConnection) { |
|||
//保护共享资源Map 加写锁
|
|||
connMgr.connLock.Lock() |
|||
defer connMgr.connLock.Unlock() |
|||
|
|||
//删除连接信息
|
|||
delete(connMgr.connections, conn.GetConnID()) |
|||
|
|||
fmt.Println("connection Remove ConnID=",conn.GetConnID(), " successfully: conn num = ", connMgr.Len()) |
|||
} |
|||
|
|||
//利用ConnID获取链接
|
|||
func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error) { |
|||
//保护共享资源Map 加读锁
|
|||
connMgr.connLock.RLock() |
|||
defer connMgr.connLock.RUnlock() |
|||
|
|||
if conn, ok := connMgr.connections[connID]; ok { |
|||
return conn, nil |
|||
} else { |
|||
return nil, errors.New("connection not found") |
|||
} |
|||
} |
|||
|
|||
//获取当前连接
|
|||
func (connMgr *ConnManager) Len() int { |
|||
return len(connMgr.connections) |
|||
} |
|||
|
|||
//清除并停止所有连接
|
|||
func (connMgr *ConnManager) ClearConn() { |
|||
//保护共享资源Map 加写锁
|
|||
connMgr.connLock.Lock() |
|||
defer connMgr.connLock.Unlock() |
|||
|
|||
//停止并删除全部的连接信息
|
|||
for connID, conn := range connMgr.connections { |
|||
//停止
|
|||
conn.Stop() |
|||
//删除
|
|||
delete(connMgr.connections,connID) |
|||
} |
|||
|
|||
|
|||
fmt.Println("Clear All Connections successfully: conn num = ", connMgr.Len()) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue