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 c975f2c commit 58cf063
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
16 changes: 16 additions & 0 deletions demo/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,27 @@ func DoConnectionBegin(conn iface.IConnection) {
if err := conn.SendMsg(202, []byte("DoConnection Begin")); err != nil {
fmt.Println(err)
}

fmt.Println("set conn name")

conn.SetProperty("Name", "LiuLiGames")
conn.SetProperty("GitHub", "https://github.com/liuligames")
conn.SetProperty("Blob", "https://liuligames.com")
}

func DoConnectionLost(conn iface.IConnection) {
fmt.Println("DoConnectionLast is Called ...")
fmt.Println("conn id = ", conn.GetConnID(), "is lost")

if name, err := conn.GetProperty("Name"); err == nil {
fmt.Println("Name = ", name)
}
if github, err := conn.GetProperty("GitHub"); err == nil {
fmt.Println("GitHub = ", github)
}
if blob, err := conn.GetProperty("Blob"); err == nil {
fmt.Println("Blob = ", blob)
}
}

func main() {
Expand Down
6 changes: 6 additions & 0 deletions iface/IConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ type IConnection interface {
GetRemoteAddr() net.Addr

SendMsg(msgId uint32, data []byte) error

SetProperty(key string, value interface{})

GetProperty(key string) (interface{}, error)

RemoveProperty(key string)
}

type HandleFunc func(*net.TCPConn, []byte, int) error
42 changes: 35 additions & 7 deletions net/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"fmt"
"io"
"net"
"sync"
"wukong/iface"
"wukong/utils"
)

type Connection struct {
TcpServer iface.IServer
Conn *net.TCPConn
ConnID uint32
isClosed bool
ExitChan chan bool
msgChan chan []byte
MsgHandler iface.IMsgHandler
TcpServer iface.IServer
Conn *net.TCPConn
ConnID uint32
isClosed bool
ExitChan chan bool
msgChan chan []byte
MsgHandler iface.IMsgHandler
property map[string]interface{}
propertyLock sync.RWMutex
}

func NewConnection(tcpServer iface.IServer, conn *net.TCPConn, connID uint32, msgHandler iface.IMsgHandler) *Connection {
Expand All @@ -28,6 +31,7 @@ func NewConnection(tcpServer iface.IServer, conn *net.TCPConn, connID uint32, ms
isClosed: false,
msgChan: make(chan []byte),
ExitChan: make(chan bool, 1),
property: make(map[string]interface{}),
}
c.TcpServer.GetConnManager().Add(c)
return c
Expand Down Expand Up @@ -161,3 +165,27 @@ func (c *Connection) SendMsg(msgId uint32, data []byte) error {

return nil
}

func (c *Connection) SetProperty(key string, value interface{}) {
c.propertyLock.Lock()
defer c.propertyLock.Unlock()

c.property[key] = value
}

func (c *Connection) GetProperty(key string) (interface{}, error) {
c.propertyLock.RLock()
defer c.propertyLock.RUnlock()
if value, ok := c.property[key]; ok {
return value, nil
} else {
return nil, errors.New("no property found")
}
}

func (c *Connection) RemoveProperty(key string) {
c.propertyLock.Lock()
defer c.propertyLock.Unlock()

delete(c.property, key)
}

0 comments on commit 58cf063

Please sign in to comment.