-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6f0edc
commit 2f6e296
Showing
9 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: "liuLI V0.8 demoServerApp" | ||
name: "liuLI V0.9 demoServerApp" | ||
Host: "127.0.0.1" | ||
TcpPort: 9527 | ||
MaxConn: 3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package iface | ||
|
||
type IConnManager interface { | ||
Add(conn IConnection) | ||
|
||
Remove(conn IConnection) | ||
|
||
Get(connId uint32) (IConnection, error) | ||
|
||
Len() int | ||
|
||
ClearConn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ type IServer interface { | |
Serve() | ||
|
||
AddRouter(msgId uint32, router IRouter) | ||
GetConnManager() IConnManager | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package net | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"wukong/iface" | ||
) | ||
|
||
type ConnManager struct { | ||
connections map[uint32]iface.IConnection | ||
connLock sync.RWMutex | ||
} | ||
|
||
func NewConnManager() *ConnManager { | ||
return &ConnManager{ | ||
connections: make(map[uint32]iface.IConnection), | ||
} | ||
} | ||
|
||
func (cm *ConnManager) Add(conn iface.IConnection) { | ||
cm.connLock.Lock() | ||
defer cm.connLock.Unlock() | ||
|
||
cm.connections[conn.GetConnID()] = conn | ||
fmt.Println("connId = ", conn.GetConnID(), " add to ConnManager conn num = ", cm.Len()) | ||
|
||
} | ||
|
||
func (cm *ConnManager) Remove(conn iface.IConnection) { | ||
cm.connLock.Lock() | ||
defer cm.connLock.Unlock() | ||
|
||
delete(cm.connections, conn.GetConnID()) | ||
fmt.Println("connId = ", conn.GetConnID(), " remove to ConnManager conn num = ", cm.Len()) | ||
|
||
} | ||
|
||
func (cm *ConnManager) Get(connId uint32) (iface.IConnection, error) { | ||
cm.connLock.RLock() | ||
defer cm.connLock.RUnlock() | ||
|
||
if conn, ok := cm.connections[connId]; ok { | ||
return conn, nil | ||
} else { | ||
return nil, errors.New("connection not found") | ||
} | ||
} | ||
|
||
func (cm *ConnManager) Len() int { | ||
return len(cm.connections) | ||
} | ||
|
||
func (cm *ConnManager) ClearConn() { | ||
cm.connLock.Lock() | ||
defer cm.connLock.Unlock() | ||
|
||
for connId, conn := range cm.connections { | ||
conn.Stop() | ||
|
||
delete(cm.connections, connId) | ||
} | ||
|
||
fmt.Println("clear all connection conn num = ", cm.Len()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters