```
refactor(common/rpc): 修改服务引用从blazing到config模块 将RPC服务中的blservice引用替换为config服务, 统一使用config.NewServerService()进行服务器信息获取。 feat(blazing): 实现新的会话生成机制 - 添加Gensession方法,基于accountID、UUID生成唯一会话标识 - 会话ID由accountID(4字节) + UUID(16字节)组成,编码为十六进制字符串 - 更新登录控制器使用新的会话生成方式 fix(pet_info): 添加宠物信息空值检查 在切换宠物背包仓库时,当宠物信息查询结果为空时, 返回系统错误避免空指针异常。
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
blservice "blazing/modules/blazing/service"
|
config "blazing/modules/config/service"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-jsonrpc"
|
"github.com/filecoin-project/go-jsonrpc"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
@@ -50,7 +50,7 @@ func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) erro
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("no reverse client")
|
return fmt.Errorf("no reverse client")
|
||||||
}
|
}
|
||||||
t := blservice.NewLoginServiceService().GetServerID(id)
|
t := config.NewServerService().GetServerID((id))
|
||||||
|
|
||||||
aa, ok := cool.GetClient(t.Port)
|
aa, ok := cool.GetClient(t.Port)
|
||||||
if ok && aa != nil { //如果已经存在且这个端口已经被存过
|
if ok && aa != nil { //如果已经存在且这个端口已经被存过
|
||||||
|
|||||||
@@ -132,6 +132,9 @@ func (h Controller) TogglePetBagWarehouse(
|
|||||||
//如果背包没找到,再放入背包
|
//如果背包没找到,再放入背包
|
||||||
if !ok {
|
if !ok {
|
||||||
r := player.Service.Pet.PetInfo_One(data.CatchTime)
|
r := player.Service.Pet.PetInfo_One(data.CatchTime)
|
||||||
|
if r == nil {
|
||||||
|
return result, errorcode.ErrorCodes.ErrSystemError
|
||||||
|
}
|
||||||
player.Info.PetList = append(player.Info.PetList, r.Data)
|
player.Info.PetList = append(player.Info.PetList, r.Data)
|
||||||
result.PetInfo = r.Data
|
result.PetInfo = r.Data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"blazing/logic/controller"
|
"blazing/logic/controller"
|
||||||
|
|
||||||
blservice "blazing/modules/blazing/service"
|
config "blazing/modules/config/service"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -56,7 +56,7 @@ func isPortAvailable(port uint32) bool {
|
|||||||
// 如果id是0,那就是login server
|
// 如果id是0,那就是login server
|
||||||
func Start() {
|
func Start() {
|
||||||
serverID := cool.Config.GameOnlineID
|
serverID := cool.Config.GameOnlineID
|
||||||
cool.Config.ServerInfo = blservice.NewLoginServiceService().GetServerID(serverID).ServerList
|
cool.Config.ServerInfo = config.NewServerService().GetServerID(serverID).ServerList
|
||||||
|
|
||||||
if cool.Config.ServerInfo.IsVip == 1 {
|
if cool.Config.ServerInfo.IsVip == 1 {
|
||||||
g.DB().SetDebug(true)
|
g.DB().SetDebug(true)
|
||||||
@@ -78,6 +78,7 @@ func Start() {
|
|||||||
controller.Maincontroller.Port = uint16(port) //赋值服务器ID
|
controller.Maincontroller.Port = uint16(port) //赋值服务器ID
|
||||||
controller.Init(true)
|
controller.Init(true)
|
||||||
xmlres.Initfile()
|
xmlres.Initfile()
|
||||||
blservice.NewLoginServiceService().SetServerID(serverID, gconv.Uint16(port))
|
config.NewServerService().SetServerID(serverID, gconv.Uint16(port))
|
||||||
|
|
||||||
server.Boot()
|
server.Boot()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"blazing/modules/base/service"
|
"blazing/modules/base/service"
|
||||||
|
|
||||||
blazing_service "blazing/modules/blazing/service"
|
blazing "blazing/modules/blazing/service"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
)
|
)
|
||||||
@@ -50,7 +50,7 @@ func (c *BaseSysUserController) GetSession(ctx context.Context, req *SessionReq)
|
|||||||
|
|
||||||
return &SessionRes{}, nil
|
return &SessionRes{}, nil
|
||||||
}
|
}
|
||||||
retsid := blazing_service.NewLoginServiceService().GetSessionId(t.UserId)
|
retsid := blazing.NewInfoService(uint32(t.UserId)).Gensession()
|
||||||
|
|
||||||
res = &SessionRes{}
|
res = &SessionRes{}
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ func (c *BaseSysUserController) GetSession(ctx context.Context, req *SessionReq)
|
|||||||
|
|
||||||
res.Session = retsid
|
res.Session = retsid
|
||||||
|
|
||||||
if !blazing_service.NewUserService(uint32(t1.ID)).Info.IsReg() {
|
if !blazing.NewUserService(uint32(t1.ID)).Info.IsReg() {
|
||||||
res.UserID = int(t1.ID)
|
res.UserID = int(t1.ID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func init() {
|
|||||||
g.Server().BindHandler("/server/*", func(r *ghttp.Request) {
|
g.Server().BindHandler("/server/*", func(r *ghttp.Request) {
|
||||||
|
|
||||||
tt := new(ServerHandler)
|
tt := new(ServerHandler)
|
||||||
id := gconv.Uint32(r.URL.Query().Get("id"))
|
id := gconv.Uint16(r.URL.Query().Get("id"))
|
||||||
tt.ServerList = service.NewServerService().GetServerByID(id)
|
tt.ServerList = service.NewServerService().GetServerID(id)
|
||||||
tt.isinstall = gconv.Uint32(r.URL.Query().Get("isinstall"))
|
tt.isinstall = gconv.Uint32(r.URL.Query().Get("isinstall"))
|
||||||
|
|
||||||
upgrader := gws.NewUpgrader(tt, &gws.ServerOption{
|
upgrader := gws.NewUpgrader(tt, &gws.ServerOption{
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ echo "#SCRIPT_EXECUTION_COMPLETE#"
|
|||||||
|
|
||||||
// 保存会话名称
|
// 保存会话名称
|
||||||
config.NewServerService().SetServerScreen(s.ServerList.OnlineID, randomFileName)
|
config.NewServerService().SetServerScreen(s.ServerList.OnlineID, randomFileName)
|
||||||
|
|
||||||
s.sendTerminalOutput(s.session.WebSocket, "自动化部署完成")
|
s.sendTerminalOutput(s.session.WebSocket, "自动化部署完成")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ package app
|
|||||||
import (
|
import (
|
||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
baseservice "blazing/modules/base/service"
|
baseservice "blazing/modules/base/service"
|
||||||
"blazing/modules/blazing/service"
|
blazing "blazing/modules/blazing/service"
|
||||||
|
"blazing/modules/config/service"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -26,14 +27,12 @@ type BlazingController struct {
|
|||||||
*cool.Controller
|
*cool.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
var biazing_service = service.NewLoginServiceService()
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var blazing_controller = &BlazingController{
|
var blazing_controller = &BlazingController{
|
||||||
&cool.Controller{
|
&cool.Controller{
|
||||||
Prefix: "/seer/game",
|
Prefix: "/seer/game",
|
||||||
Api: []string{},
|
Api: []string{},
|
||||||
Service: biazing_service,
|
Service: service.NewServerService(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// 注册路由
|
// 注册路由
|
||||||
@@ -61,7 +60,7 @@ func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (re
|
|||||||
|
|
||||||
accountID := res1.ID
|
accountID := res1.ID
|
||||||
|
|
||||||
res.Session = biazing_service.GetSessionId(accountID)
|
res.Session = blazing.NewInfoService(uint32(accountID)).Gensession()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,15 @@ import (
|
|||||||
"blazing/modules/blazing/model"
|
"blazing/modules/blazing/model"
|
||||||
"blazing/modules/config/service"
|
"blazing/modules/config/service"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 是否注册,如果注册过,那么就会产生用户player信息
|
// 是否注册,如果注册过,那么就会产生用户player信息
|
||||||
@@ -104,6 +108,34 @@ func (s *InfoService) Personself() *model.PlayerInfo {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成session
|
||||||
|
// GetSessionId 生成并返回会话ID、UUID字符串及可能的错误
|
||||||
|
// 会话ID由accountID(4字节) + UUID(16字节) + 随机数(4字节)组成,最终编码为十六进制字符串
|
||||||
|
func (s *InfoService) Gensession() string {
|
||||||
|
uuidV7, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
// 移除UUID中的连字符,便于后续处理
|
||||||
|
uuidStr := strings.ReplaceAll(uuidV7.String(), "-", "")
|
||||||
|
|
||||||
|
// 解码UUID字符串为字节数组(32位十六进制字符串对应16字节)
|
||||||
|
uuidBytes, _ := hex.DecodeString(uuidStr)
|
||||||
|
|
||||||
|
// 将accountID转换为4字节大端序字节数组
|
||||||
|
accountBytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(accountBytes, uint32(s.userid))
|
||||||
|
|
||||||
|
// 预分配缓冲区(总长度:4+16+4=24字节),减少内存分配
|
||||||
|
sessionBytes := make([]byte, 0, 24)
|
||||||
|
sessionBytes = append(sessionBytes, accountBytes...)
|
||||||
|
sessionBytes = append(sessionBytes, uuidBytes...)
|
||||||
|
//sessionBytes = append(sessionBytes, grand.B(4)...)
|
||||||
|
|
||||||
|
// 编码为十六进制字符串作为最终会话ID
|
||||||
|
sessionID := hex.EncodeToString(sessionBytes)
|
||||||
|
share.ShareManager.SaveSession(string(uuidStr), uint32(s.userid))
|
||||||
|
return sessionID
|
||||||
|
}
|
||||||
|
|
||||||
func (s *InfoService) Kick(id uint32) {
|
func (s *InfoService) Kick(id uint32) {
|
||||||
|
|
||||||
cool.Logger.Info(context.TODO(), "服务器收到踢人")
|
cool.Logger.Info(context.TODO(), "服务器收到踢人")
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"blazing/common/data/share"
|
|
||||||
"blazing/cool"
|
|
||||||
"blazing/modules/config/model"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"encoding/binary"
|
|
||||||
"encoding/hex"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoginService struct {
|
|
||||||
*cool.Service
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLoginServiceService() *LoginService {
|
|
||||||
return &LoginService{
|
|
||||||
&cool.Service{
|
|
||||||
Model: model.NewServerList(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成session
|
|
||||||
// GetSessionId 生成并返回会话ID、UUID字符串及可能的错误
|
|
||||||
// 会话ID由accountID(4字节) + UUID(16字节) + 随机数(4字节)组成,最终编码为十六进制字符串
|
|
||||||
func (s *LoginService) GetSessionId(accountID uint) string {
|
|
||||||
uuidV7, _ := uuid.NewV7()
|
|
||||||
|
|
||||||
// 移除UUID中的连字符,便于后续处理
|
|
||||||
uuidStr := strings.ReplaceAll(uuidV7.String(), "-", "")
|
|
||||||
|
|
||||||
// 解码UUID字符串为字节数组(32位十六进制字符串对应16字节)
|
|
||||||
uuidBytes, _ := hex.DecodeString(uuidStr)
|
|
||||||
|
|
||||||
// 将accountID转换为4字节大端序字节数组
|
|
||||||
accountBytes := make([]byte, 4)
|
|
||||||
binary.BigEndian.PutUint32(accountBytes, uint32(accountID))
|
|
||||||
|
|
||||||
// 预分配缓冲区(总长度:4+16+4=24字节),减少内存分配
|
|
||||||
sessionBytes := make([]byte, 0, 24)
|
|
||||||
sessionBytes = append(sessionBytes, accountBytes...)
|
|
||||||
sessionBytes = append(sessionBytes, uuidBytes...)
|
|
||||||
//sessionBytes = append(sessionBytes, grand.B(4)...)
|
|
||||||
|
|
||||||
// 编码为十六进制字符串作为最终会话ID
|
|
||||||
sessionID := hex.EncodeToString(sessionBytes)
|
|
||||||
share.ShareManager.SaveSession(string(uuidStr), uint32(accountID))
|
|
||||||
return sessionID
|
|
||||||
}
|
|
||||||
func (s *LoginService) SetServerID(OnlineID uint16, Port uint16) error {
|
|
||||||
|
|
||||||
m := cool.DBM(s.Model).Where("online_id", OnlineID)
|
|
||||||
|
|
||||||
// record, err := m.One()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// if record == nil {
|
|
||||||
// //说明是新的服务器
|
|
||||||
|
|
||||||
// _, err := m.InsertAndGetId(&model.ServerList{OnlineID: OnlineID, Port: Port})
|
|
||||||
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
var tttt model.ServerList
|
|
||||||
m.Scan(&tttt)
|
|
||||||
tttt.Port = Port
|
|
||||||
tttt.OnlineID = OnlineID
|
|
||||||
|
|
||||||
m.Save(tttt)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
func (s *LoginService) GetServerID(OnlineID uint16) *model.ServerList {
|
|
||||||
var tttt *model.ServerList
|
|
||||||
err := cool.DBM(s.Model).Where("online_id", OnlineID).Scan(&tttt)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return tttt
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -59,10 +59,10 @@ func (s *PetService) UPdate(t model.PetInfo) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (s *PetService) PetInfo_One(cachetime uint32) model.PetEX {
|
func (s *PetService) PetInfo_One(cachetime uint32) *model.PetEX {
|
||||||
|
|
||||||
m := s.TestModel(s.Model).Where("player_id", s.userid).Where("catch_time", cachetime)
|
m := s.TestModel(s.Model).Where("player_id", s.userid).Where("catch_time", cachetime)
|
||||||
var tt model.PetEX
|
var tt *model.PetEX
|
||||||
|
|
||||||
m.Scan(&tt)
|
m.Scan(&tt)
|
||||||
tt.Data.CatchTime = tt.CatchTime
|
tt.Data.CatchTime = tt.CatchTime
|
||||||
|
|||||||
@@ -78,13 +78,32 @@ func (s *ServerService) GetServer() []model.ServerList {
|
|||||||
return item
|
return item
|
||||||
|
|
||||||
}
|
}
|
||||||
func (s *ServerService) GetServerByID(id uint32) model.ServerList {
|
|
||||||
var item model.ServerList
|
|
||||||
dbm(s.Model).Where("online_id", id).Scan(&item)
|
|
||||||
|
|
||||||
return item
|
func (s *ServerService) SetServerID(OnlineID uint16, Port uint16) error {
|
||||||
|
|
||||||
|
m := cool.DBM(s.Model).Where("online_id", OnlineID)
|
||||||
|
|
||||||
|
var tttt model.ServerList
|
||||||
|
m.Scan(&tttt)
|
||||||
|
tttt.Port = Port
|
||||||
|
tttt.OnlineID = OnlineID
|
||||||
|
|
||||||
|
m.Save(tttt)
|
||||||
|
// s.CleanCache()
|
||||||
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func (s *ServerService) GetServerID(OnlineID uint16) model.ServerList {
|
||||||
|
var tttt model.ServerList
|
||||||
|
cool.DBM(s.Model).Where("online_id", OnlineID).Scan(&tttt)
|
||||||
|
|
||||||
|
return tttt
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// func (s *ServerService) CleanCache() {
|
||||||
|
// g.DB().GetCore().ClearCache(context.TODO(), s.Model.TableName())
|
||||||
|
// }
|
||||||
func (s *ServerService) SetServerScreen(id uint16, name string) {
|
func (s *ServerService) SetServerScreen(id uint16, name string) {
|
||||||
|
|
||||||
dbm(s.Model).Where("online_id", id).Data("old_screen", name).Update()
|
dbm(s.Model).Where("online_id", id).Data("old_screen", name).Update()
|
||||||
|
|||||||
Reference in New Issue
Block a user