feat(rpc): 更新客户端映射变量命名以符合导出规则

将 rpc.go 中的 clientmap 变量重命名为 Clientmap,使其可被其他包访问。
同时修复了拼写错误 useid1 -> userid,并在 RegisterLogic 方法中调用 Refurh 函数。

fix
This commit is contained in:
2025-11-03 19:14:34 +08:00
parent 9a802ce948
commit 9b73de4faf
9 changed files with 174 additions and 124 deletions

130
common/rpc/client.go Normal file
View File

@@ -0,0 +1,130 @@
package rpc
import (
"blazing/cool"
"blazing/modules/base/service"
"blazing/modules/blazing/model"
"context"
"sync"
"github.com/butoften/array"
)
func GetServerInfoList1() []ServerInfo {
dictInfoModel1 := model.NewServerList()
mType := cool.DBM(dictInfoModel1)
t, _ := mType.All()
//fmt.Println(t)
var ret []model.ServerList
t.Structs(&ret)
//fmt.Println(t)
var ret1 []ServerInfo
ip, _ := service.NewBaseSysParamService().DataByKey(context.Background(), "server_ip")
testip, _ := service.NewBaseSysParamService().DataByKey(context.Background(), "test_ip")
for _, v := range ret {
tt := newServerInfo()
tt.OnlineID = uint32(v.OnlineID)
tt.IP = ip
if tt.OnlineID == 2 {
tt.UserCnt = 300
tt.IP = testip
}
tt.Port = v.Port
t, ok := Clientmap[v.Port]
if ok && t != nil {
// tt.Friends = v.Friends
ret1 = append(ret1, *tt)
}
}
array.Sort(&ret1, func(a ServerInfo, b ServerInfo) bool {
return a.OnlineID < b.OnlineID
})
//fmt.Printf("升序 Sort: %v\n", ret1)
return ret1
}
// CommendSvrInfo 初始连接请求信息结构体
type CommendSvrInfo struct {
//Handler player.TomeeHeader //` struc:"[0]pad"` //消息头 ,这里为传入的头部数据,遍历此头部实现解析CommendSvrInfo
MaxOnlineID uint32 `struc:"sizeof=ServerList"` // 最大连接数
IsVip uint32 // 建议为0
ServerInfoLen uint32 `struc:"sizeof=ServerList"` // 服务器信息长度 ServerInfo
ServerList []ServerInfo // 服务器具体信息
FriendInfoLen uint32 `struc:"sizeof=FriendInfo"`
FriendInfo []FriendInfo // 好友id
BlackInfoLen uint32 `struc:"sizeof=BlackInfo"`
BlackInfo []BlackInfo // 黑名单id
}
// NewInInfo 创建并返回一个新的 commendSvrInfo 结构体实例
// 返回的实例包含初始化的 ServerList、FriendInfo 和 BlackInfo 切片
// IsVip 和 ServerInfoLen 字段被初始化为 0
func NewInInfo() *CommendSvrInfo {
return &CommendSvrInfo{
// Handler: player.TomeeHeader{},
// MaxOnlineID: 100,
IsVip: 0,
ServerInfoLen: 0,
ServerList: make([]ServerInfo, 0),
FriendInfo: make([]FriendInfo, 0),
BlackInfo: make([]BlackInfo, 0),
//Reversed: 0,
}
}
// ServerInfo 服务器信息结构体
type ServerInfo struct {
// 连接ID, 即服务器序号
OnlineID uint32
// 当前服务器玩家在线数量, 供SWF显示
UserCnt uint32
// 服务器IP, 16字节UTF-8, 不足16补齐到16
IP string `struc:"[16]byte"` // 定长模式16字节
// 端口
Port uint16
// 好友在线的个数
Friends uint32
}
// NewServerInfo 创建新的服务器信息实例
func newServerInfo() *ServerInfo {
//getServerInfoList()
return &ServerInfo{
//OnlineID: 0,
UserCnt: 20,
//IP: "",
// Port: 0,
Friends: 1,
}
}
type FriendInfo struct {
BlackInfo
TimePoke uint32
}
type BlackInfo struct {
Userid uint32
//TimePoke uint32
}
func Refurh() {
l.Lock()
defer l.Unlock()
GetServerInfoList = GetServerInfoList1()
}
func GetServer() []ServerInfo {
l.RLock()
defer l.RUnlock()
return GetServerInfoList
}
var GetServerInfoList = GetServerInfoList1()
var l sync.RWMutex

View File

@@ -3,6 +3,7 @@ package rpc
import (
"blazing/common/data/share"
"blazing/cool"
"blazing/modules/base/service"
"context"
"fmt"
@@ -17,7 +18,7 @@ import (
var rpcport = gconv.String(cool.Config.RPC)
var clientmap = make(map[uint16]*ClientHandler) //客户端map
var Clientmap = make(map[uint16]*ClientHandler) //客户端map
type ClientHandler struct {
KickPerson func(uint32) error //踢人,这里是返回具体的logic
@@ -36,7 +37,7 @@ func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
if err != nil {
return fmt.Errorf("user not found", err)
}
cl, ok := clientmap[useid1]
cl, ok := Clientmap[useid1]
if ok {
err := cl.KickPerson(userid) //实现指定服务器踢人
if err != nil {
@@ -49,18 +50,20 @@ func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
// 注册logic服务器
func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
cool.Loger.Debug(context.Background(), "注册logic服务器", id, port)
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
revClient, ok := jsonrpc.ExtractReverseClient[ClientHandler](ctx)
if !ok {
return fmt.Errorf("no reverse client")
}
t, _ := blservice.NewLoginServiceService().GetServerID(id)
aa, ok := clientmap[t]
aa, ok := Clientmap[t]
if ok && aa != nil { //如果已经存在且这个端口已经被存过
aa.QuitSelf(0)
}
clientmap[port] = &revClient
Clientmap[port] = &revClient
Refurh()
return nil
}

View File

@@ -83,7 +83,6 @@ func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
if s.network != "tcp" {
return nil, gnet.Close
}
conn.SetReadDeadline(<-time.After(3000))
if conn.Context() == nil {
conn.SetContext(player.NewClientData(conn)) //注入data