重写消息

This commit is contained in:
liuxiaobo 2025-06-08 22:51:10 +08:00
parent 143c152b0e
commit ff058d4a09
10 changed files with 36 additions and 36 deletions

View File

@ -10,21 +10,21 @@ import (
func (s *ChatService) initProcessor() {
s.processor.RegisterMessages(processor.RegisterMetas{
pb.MsgId_C2SChatId: {pb.C2SChat{}, s.onChat},
pb.MsgId_ReqChatId: {pb.ReqChat{}, s.onChat},
})
}
// 收到登陆成功消息,判断是否顶号
func (s *ChatService) onChat(uid int64, msg *pb.C2SChat) {
func (s *ChatService) onChat(uid int64, msg *pb.ReqChat) {
switch msg.Type {
case pb.ChatType_CT_Private:
tName, _ := s.bindService.FindTopic(msg.DstUser.UserId, pb.ServiceTypeId_STI_Gate)
if tName == "" {
log.DebugF("user:%v find gate failed", uid)
}
s.SendServiceMsg(service.TopicEx(tName), msg.DstUser.UserId, int32(pb.MsgId_S2CChatId), msg)
s.SendServiceMsg(service.TopicEx(tName), msg.DstUser.UserId, int32(pb.MsgId_RspChatId), msg)
default:
s.SendServiceMsg(service.TopicEx(topicName.WorldMessage), uid, int32(pb.MsgId_S2CChatId), msg)
s.SendServiceMsg(service.TopicEx(topicName.WorldMessage), uid, int32(pb.MsgId_RspChatId), msg)
}
}

View File

@ -17,7 +17,7 @@ func (s *ClientService) chat() {
}
// 收到登陆成功消息,判断是否顶号
func (s *ClientService) onChat(cMsg *pb.ClientMsg, msg *pb.S2CUserLogin) {
func (s *ClientService) onChat(cMsg *pb.ClientMsg, msg *pb.RspUserLogin) {
if msg.Code != pb.ErrCode_OK {
log.ErrorF("login error: %v", msg.Code)
return

View File

@ -7,7 +7,7 @@ import (
func (s *ClientService) login() {
log.DebugF("user:%v ready login", s.username)
s.SendMsg(pb.ServiceTypeId_STI_Login, int32(pb.MsgId_C2SUserLoginId), &pb.C2SUserLogin{
s.SendMsg(pb.ServiceTypeId_STI_Login, int32(pb.MsgId_ReqUserLoginId), &pb.ReqUserLogin{
Username: s.username,
Password: s.password,
Ip: "",
@ -17,7 +17,7 @@ func (s *ClientService) login() {
}
// 收到登陆成功消息,判断是否顶号
func (s *ClientService) onLogin(cMsg *pb.ClientMsg, msg *pb.S2CUserLogin) {
func (s *ClientService) onLogin(cMsg *pb.ClientMsg, msg *pb.RspUserLogin) {
if msg.Code != pb.ErrCode_OK {
log.ErrorF("login error:%v :%v", msg.Code, msg.Code.String())
return

View File

@ -7,6 +7,6 @@ import (
func (s *ClientService) initProcessor() {
s.processor.RegisterMessages(processor.RegisterMetas{
pb.MsgId_S2CUserLoginId: {pb.S2CUserLogin{}, s.onLogin},
pb.MsgId_RspUserLoginId: {pb.RspUserLogin{}, s.onLogin},
})
}

View File

@ -5,12 +5,12 @@ import (
"github.com/fox/fox/ipb"
)
func (rm *ColorRoom) checkEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
func (rm *ColorRoom) checkEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqMatchRoom) {
return
}
func (rm *ColorRoom) OnEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
func (rm *ColorRoom) OnEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqMatchRoom) {
return
}

View File

@ -16,17 +16,17 @@ const (
func (s *ColorService) initProcessor() {
s.processor.RegisterMessages(processor.RegisterMetas{
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, s.onEnterRoom},
pb.MsgId_ReqMatchRoomId: {pb.ReqMatchRoom{}, s.onEnterRoom},
})
}
// 登录或注册
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.ReqMatchRoom) {
ksync.GoSafe(func() {
us, code := rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId)
if code != pb.ErrCode_OK {
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId,
int32(pb.MsgId_S2CMatchRoomId), &pb.S2CMatchRoom{Code: pb.ErrCode_SystemErr})
int32(pb.MsgId_RspMatchRoomId), &pb.RspMatchRoom{Code: pb.ErrCode_SystemErr})
}
// 切回服务协程处理业务逻辑
s.RunOnce(func() {

View File

@ -14,38 +14,38 @@ import (
*/
func (s *GateService) initProcessor() {
s.processor.RegisterMessages(processor.RegisterMetas{
pb.MsgId_S2CUserLoginId: {pb.S2CUserLogin{}, s.onUserLogin},
pb.MsgId_S2CUserLogoutId: {pb.S2CUserLogout{}, s.onUserLogout},
pb.MsgId_RspUserLoginId: {pb.RspUserLogin{}, s.onUserLogin},
pb.MsgId_RspUserLogoutId: {pb.RspUserLogout{}, s.onUserLogout},
})
}
// 收到登陆成功消息,判断是否顶号
func (s *GateService) onUserLogin(iMsg *ipb.InternalMsg, conn ws.IConn, msg *pb.S2CUserLogin) {
func (s *GateService) onUserLogin(iMsg *ipb.InternalMsg, conn ws.IConn, msg *pb.RspUserLogin) {
if conn == nil {
return
}
// 登陆失败,回传玩家
if msg.Code != pb.ErrCode_OK {
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_S2CUserLoginId), msg)
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_RspUserLoginId), msg)
return
}
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_S2CUserLoginId), msg)
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_RspUserLoginId), msg)
s.wss.SetUserId(conn.Id(), msg.UserId)
sName := s.bindService.LoadFromRedis(conn.UserId(), pb.ServiceTypeId_STI_Gate)
// 网关不同,说明玩家在其它网关上登陆,
if sName != "" && sName != s.Name() {
s.SendServiceMsg(service.TopicEx(sName), conn, int32(pb.MsgId_S2CUserLogoutId), &pb.S2CUserLogout{Code: pb.ErrCode_LoginDiffLoc})
s.SendServiceMsg(service.TopicEx(sName), conn, int32(pb.MsgId_RspUserLogoutId), &pb.RspUserLogout{Code: pb.ErrCode_LoginDiffLoc})
}
// 广播玩家上线
s.SendServiceMsg(topicName.UserOnline, conn, int32(pb.MsgId_NtfUserOnlineId), &pb.NtfUserOnline{UserId: msg.UserId})
}
// 收到登出消息
func (s *GateService) onUserLogout(iMsg *ipb.InternalMsg, conn ws.IConn, msg *pb.S2CUserLogout) {
func (s *GateService) onUserLogout(iMsg *ipb.InternalMsg, conn ws.IConn, msg *pb.RspUserLogout) {
if conn == nil {
return
}
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_S2CUserLogoutId), msg)
s.SendClientMsg(conn, iMsg.ServiceName, int32(pb.MsgId_RspUserLogoutId), msg)
// 登出的清理工作由WsOnDisconnect实现
conn.Close()
}

View File

@ -115,7 +115,7 @@ func (s *GateService) OnStop() {
func (s *GateService) findConn(msg *ipb.InternalMsg) ws.IConn {
switch msg.MsgId {
case int32(pb.MsgId_S2CUserLoginId):
case int32(pb.MsgId_RspUserLoginId):
conn, _ := s.wss.FindConnByConnId(msg.ConnId)
return conn
default:
@ -169,8 +169,8 @@ func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
topic, msg.ServiceName = s.bindService.FindTopic(conn.UserId(), msg.ServiceTid)
}
if topic != "" {
if msg.MsgId == int32(pb.MsgId_C2SUserLoginId) {
req := &pb.C2SUserLogin{}
if msg.MsgId == int32(pb.MsgId_ReqUserLoginId) {
req := &pb.ReqUserLogin{}
_ = proto.Unmarshal(msg.Data, req)
req.Ip = conn.Addr()
msg.Data, _ = proto.Marshal(req)

View File

@ -21,11 +21,11 @@ const (
func (s *LoginService) initProcessor() {
s.processor.RegisterMessages(processor.RegisterMetas{
pb.MsgId_C2SUserLoginId: {pb.C2SUserLogin{}, s.onLoginOrRegister},
pb.MsgId_ReqUserLoginId: {pb.ReqUserLogin{}, s.onLoginOrRegister},
})
}
func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.UserAccount, code pb.ErrCode, node *etcd.ServiceNode) {
func (s *LoginService) checkLoginOrRegister(req *pb.ReqUserLogin) (us *user.UserAccount, code pb.ErrCode, node *etcd.ServiceNode) {
var err error
node, err = s.bindService.RandServiceNode(pb.ServiceTypeId_STI_DB)
if err != nil {
@ -116,11 +116,11 @@ func (s *LoginService) getUser(accountId int64, tName string) (*user.User, pb.Er
}
// 登录或注册
func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.C2SUserLogin) {
func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.ReqUserLogin) {
ksync.GoSafe(func() {
account, code, node := s.checkLoginOrRegister(req)
userId := int64(0)
rsp := &pb.S2CUserLogin{Code: code}
rsp := &pb.RspUserLogin{Code: code}
if account != nil && code == pb.ErrCode_OK {
// 拉取用户数据
us := &user.User{}
@ -131,7 +131,7 @@ func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.C2SUserL
userId = rsp.UserId
}
}
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_S2CUserLoginId), rsp)
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_RspUserLoginId), rsp)
if account != nil && account.ID > 0 {
loginLog := &user.UserLoginLog{

View File

@ -12,7 +12,7 @@ import (
)
// 匹配房间
func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.ReqMatchRoom) {
ksync.GoSafe(func() {
// color game无需进入匹配队列
playType := pb.ServiceTypeId(req.PlayType)
@ -22,18 +22,18 @@ func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom)
if err != nil {
log.ErrorF("db service node error:%v", err)
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId,
int32(pb.MsgId_S2CMatchRoomId), &pb.S2CMatchRoom{Code: pb.ErrCode_SystemErr})
int32(pb.MsgId_RspMatchRoomId), &pb.RspMatchRoom{Code: pb.ErrCode_SystemErr})
return
}
s.SendServiceMsg(service.TopicEx(node.Name), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_C2SMatchRoomId), req)
s.SendServiceMsg(service.TopicEx(node.Name), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_ReqMatchRoomId), req)
return
}
var us *user.GameUser
rsp := &pb.S2CMatchRoom{}
rsp := &pb.RspMatchRoom{}
us, rsp.Code = rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId)
if rsp.Code != pb.ErrCode_OK {
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_S2CMatchRoomId), rsp)
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_RspMatchRoomId), rsp)
return
}
vipLv, vipExp := utils.VipLevel(us.VipExp)
@ -51,9 +51,9 @@ func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom)
rsp.RoomType = req.RoomType
switch pb.ServiceTypeId(rsp.PlayType) {
case pb.ServiceTypeId_STI_ColorGame:
rsp.ColorInfo = &pb.S2CMatchRoom_ColorInfo{}
rsp.ColorInfo = &pb.RspMatchRoom_ColorInfo{}
}
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_S2CMatchRoomId), rsp)
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_RspMatchRoomId), rsp)
}, nil)
}