121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
// Package tcp -----------------------------
|
|
// @file : clientMaper.go
|
|
// @author : JJXu
|
|
// @contact : wavingbear@163.com
|
|
// @time : 2024/5/23 上午10:43
|
|
// -------------------------------------------
|
|
package tcp
|
|
|
|
import (
|
|
"fmt"
|
|
message2 "github.com/fonchain/fonchain-container/cmd/internal/embedded/implement/tcp/message"
|
|
"net"
|
|
"sync"
|
|
)
|
|
|
|
func NewClientRoom() *ClientRoom {
|
|
return &ClientRoom{
|
|
room: make(map[string]client, 6000),
|
|
}
|
|
}
|
|
|
|
type client struct {
|
|
lock sync.Mutex
|
|
conn net.Conn
|
|
}
|
|
|
|
type ClientRoom struct {
|
|
locker sync.RWMutex
|
|
room map[string]client
|
|
}
|
|
|
|
func (c *ClientRoom) Save(mchId string, conn net.Conn) {
|
|
c.locker.Lock()
|
|
defer c.locker.Unlock()
|
|
c.room[mchId] = client{conn: conn}
|
|
}
|
|
|
|
func (c *ClientRoom) Delete(mchId string) error {
|
|
c.locker.Lock()
|
|
defer c.locker.Unlock()
|
|
if thisClient, ok := c.room[mchId]; !ok {
|
|
return fmt.Errorf("mchId:%s not exist", mchId)
|
|
} else {
|
|
thisClient.conn.Close()
|
|
delete(c.room, mchId)
|
|
}
|
|
return nil
|
|
}
|
|
func (c *ClientRoom) Reading(mchId string) error {
|
|
c.locker.RLock()
|
|
thisClient, ok := c.room[mchId]
|
|
c.locker.RUnlock()
|
|
if !ok {
|
|
return fmt.Errorf("mchId:%s not exist", mchId)
|
|
}
|
|
thisClient.lock.Lock()
|
|
defer thisClient.lock.Unlock()
|
|
var msg = make([]byte, 512)
|
|
_, err := thisClient.conn.Read(msg)
|
|
if err != nil {
|
|
fmt.Println("读取数据失败,断开连接", err)
|
|
c.Delete(mchId)
|
|
return nil
|
|
}
|
|
c.HandleMsg(msg)
|
|
return nil
|
|
}
|
|
func (c *ClientRoom) GetMchIdList() []string {
|
|
c.locker.RLock()
|
|
defer c.locker.RUnlock()
|
|
var mchIdList []string
|
|
for mchId := range c.room {
|
|
mchIdList = append(mchIdList, mchId)
|
|
}
|
|
return mchIdList
|
|
}
|
|
func (c *ClientRoom) SendMsg(mchId, msg string) error {
|
|
c.locker.RLock()
|
|
thisClient, ok := c.room[mchId]
|
|
c.locker.RUnlock()
|
|
if !ok {
|
|
return fmt.Errorf("mchId:%s not exist", mchId)
|
|
}
|
|
thisClient.lock.Lock()
|
|
defer thisClient.lock.Unlock()
|
|
_, err := thisClient.conn.Write([]byte(msg))
|
|
if err != nil {
|
|
fmt.Println("发送消息失败", err)
|
|
c.Delete(mchId)
|
|
}
|
|
return nil
|
|
}
|
|
func (c *ClientRoom) HandleMsg(msg []byte) {
|
|
mb := message2.CommonType(msg)
|
|
fmt.Println("接收消息", mb.GetFull())
|
|
switch mb.GetCmd() {
|
|
case "002":
|
|
pingMb := message2.PingType(msg)
|
|
fmt.Printf("获取pong消息: [mchId:%s] [body:%s]\n", pingMb.GetMchId(), pingMb.GetBody())
|
|
default:
|
|
fmt.Printf("未知命令[%s] ,[mchId:%s] [body:%s]\n", mb.GetCmd(), mb.GetMchId(), mb.GetBody())
|
|
}
|
|
}
|
|
|
|
func WriteMessage(conn net.Conn, msg string) error {
|
|
_, err := conn.Write([]byte(msg))
|
|
return err
|
|
}
|
|
func WriteMessage512(conn net.Conn, msg string) error {
|
|
//补全512字节
|
|
var runes = []rune(msg)
|
|
if len(runes) < 512 {
|
|
for i := 0; i < 512-len(runes); i++ {
|
|
runes = append(runes, 48)
|
|
}
|
|
}
|
|
fmt.Println("发送消息", string(runes))
|
|
_, err := conn.Write([]byte(string(runes)))
|
|
return err
|
|
}
|