Skip to content

Commit

Permalink
链接管理
Browse files Browse the repository at this point in the history
  • Loading branch information
liuligames committed Aug 9, 2021
1 parent c6f0edc commit 2f6e296
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 19 deletions.
2 changes: 1 addition & 1 deletion demo/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {

for {
dp := wkNet.NewDataPack()
binaryMsg, err := dp.Pack(wkNet.NewMessage(0, []byte("LiuLiGamesV0.8 client0 Test Message ")))
binaryMsg, err := dp.Pack(wkNet.NewMessage(0, []byte("LiuLiGamesV0.9 client0 Test Message ")))
if err != nil {
fmt.Println("Pack error", err)
}
Expand Down
2 changes: 1 addition & 1 deletion demo/Client1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {

for {
dp := wkNet.NewDataPack()
binaryMsg, err := dp.Pack(wkNet.NewMessage(1, []byte("LiuLiGamesV0.8 client1 Test Message ")))
binaryMsg, err := dp.Pack(wkNet.NewMessage(1, []byte("LiuLiGamesV0.9 client1 Test Message ")))
if err != nil {
fmt.Println("Pack error", err)
}
Expand Down
2 changes: 1 addition & 1 deletion demo/conf/conf.yaml
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
Expand Down
13 changes: 13 additions & 0 deletions iface/IConnManager.go
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()
}
1 change: 1 addition & 0 deletions iface/IServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type IServer interface {
Serve()

AddRouter(msgId uint32, router IRouter)
GetConnManager() IConnManager
}
66 changes: 66 additions & 0 deletions net/ConnManager.go
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())

}
10 changes: 8 additions & 2 deletions net/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type Connection struct {
TcpServer iface.IServer
Conn *net.TCPConn
ConnID uint32
isClosed bool
Expand All @@ -18,15 +19,18 @@ type Connection struct {
MsgHandler iface.IMsgHandler
}

func NewConnection(conn *net.TCPConn, connID uint32, msgHandler iface.IMsgHandler) *Connection {
return &Connection{
func NewConnection(tcpServer iface.IServer, conn *net.TCPConn, connID uint32, msgHandler iface.IMsgHandler) *Connection {
c := &Connection{
TcpServer: tcpServer,
Conn: conn,
ConnID: connID,
MsgHandler: msgHandler,
isClosed: false,
msgChan: make(chan []byte),
ExitChan: make(chan bool, 1),
}
c.TcpServer.GetConnManager().Add(c)
return c
}

func (c *Connection) StartReader() {
Expand Down Expand Up @@ -118,6 +122,8 @@ func (c *Connection) Stop() {

c.ExitChan <- true

c.TcpServer.GetConnManager().Remove(c)

close(c.ExitChan)
close(c.msgChan)
}
Expand Down
40 changes: 27 additions & 13 deletions net/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

type Server struct {
Name string
IPVersion string
IP string
Port int
MsgHandler iface.IMsgHandler
Name string
IPVersion string
IP string
Port int
MsgHandler iface.IMsgHandler
ConnManager iface.IConnManager
}

func (s *Server) Start() {
Expand All @@ -26,7 +27,6 @@ func (s *Server) Start() {

s.MsgHandler.StartWorkerPool()


addr, err := net.ResolveTCPAddr(s.IPVersion, fmt.Sprintf("%s:%d", s.IP, s.Port))
if err != nil {
fmt.Println("resolve tcp addr error :", err)
Expand All @@ -50,7 +50,15 @@ func (s *Server) Start() {
continue
}

dealConn := NewConnection(conn, connId, s.MsgHandler)
if s.ConnManager.Len() >= utils.GlobalObject.MaxConn {
fmt.Println("too many connections maxConn = ", utils.GlobalObject.MaxConn)
if err := conn.Close(); err != nil {
fmt.Println("MaxConn Close error :", err)
}
continue
}

dealConn := NewConnection(s, conn, connId, s.MsgHandler)
connId++

go dealConn.Start()
Expand All @@ -59,7 +67,8 @@ func (s *Server) Start() {
}

func (s *Server) Stop() {

fmt.Println("stop server name", s.Name)
s.ConnManager.ClearConn()
}

func (s *Server) Serve() {
Expand All @@ -75,12 +84,17 @@ func (s *Server) AddRouter(msgId uint32, router iface.IRouter) {
fmt.Println("Add Router !!!!")
}

func (s *Server) GetConnManager() iface.IConnManager {
return s.ConnManager
}

func NewServer() iface.IServer {
return &Server{
Name: utils.GlobalObject.Name,
IPVersion: "tcp4",
IP: utils.GlobalObject.Host,
Port: utils.GlobalObject.TcpPort,
MsgHandler: NewMsgHandler(),
Name: utils.GlobalObject.Name,
IPVersion: "tcp4",
IP: utils.GlobalObject.Host,
Port: utils.GlobalObject.TcpPort,
MsgHandler: NewMsgHandler(),
ConnManager: NewConnManager(),
}
}
2 changes: 1 addition & 1 deletion utils/globalobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (g *GlobalObj) Reload() {
func init() {
GlobalObject = &GlobalObj{
Name: "LiuLIServerApp",
Version: "V0.8",
Version: "V0.9",
TcpPort: 9527,
Host: "0.0.0.0",
MaxConn: 1000,
Expand Down

0 comments on commit 2f6e296

Please sign in to comment.