rpc请求集中放到rpc目录下
This commit is contained in:
parent
c4f64b2054
commit
ac1c6ce04b
41
common/rpc/rpcGameUser.go
Normal file
41
common/rpc/rpcGameUser.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"game/common/model/user"
|
||||||
|
"game/common/proto/pb"
|
||||||
|
"game/common/userBindService"
|
||||||
|
"github.com/fox/fox/ipb"
|
||||||
|
"github.com/fox/fox/log"
|
||||||
|
"github.com/fox/fox/service"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
timeout = time.Second * 30
|
||||||
|
)
|
||||||
|
|
||||||
|
// 获取玩家数据
|
||||||
|
func RpcGetGameUser(bindService *userBindService.UserBindService, s service.IService, uid int64) (*user.GameUser, pb.ErrCode) {
|
||||||
|
node, err := bindService.HashServiceNode(pb.ServiceTypeId_STI_DB, uid)
|
||||||
|
if err != nil {
|
||||||
|
log.ErrorF("db service node error:%v", err)
|
||||||
|
return nil, pb.ErrCode_SystemErr
|
||||||
|
}
|
||||||
|
us := &user.GameUser{
|
||||||
|
User: user.User{
|
||||||
|
ID: uid,
|
||||||
|
},
|
||||||
|
UserResources: user.UserResources{
|
||||||
|
UID: uid,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
rpcMsg := ipb.MakeRpcMsg[user.GameUser](GetGameUser, uid, us)
|
||||||
|
rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||||
|
if err != nil {
|
||||||
|
log.ErrorF("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error())
|
||||||
|
return nil, pb.ErrCode_SystemErr
|
||||||
|
}
|
||||||
|
_ = json.Unmarshal(rspMsg.Msg, us)
|
||||||
|
return us, pb.ErrCode_OK
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package rpcName
|
package rpc
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// 用户、用户资源及帐号相关操作
|
// 用户、用户资源及帐号相关操作
|
@ -6,11 +6,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ColorPlayer struct {
|
type ColorPlayer struct {
|
||||||
user.User
|
*user.User
|
||||||
|
*user.UserResources
|
||||||
gateTopicName string
|
gateTopicName string
|
||||||
roomId int
|
roomId int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewColorPlayer(gateTopicName string, roomId int, u *user.User, res *user.UserResources) *ColorPlayer {
|
||||||
|
return &ColorPlayer{
|
||||||
|
User: u,
|
||||||
|
UserResources: res,
|
||||||
|
gateTopicName: gateTopicName,
|
||||||
|
roomId: roomId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *ColorPlayer) Id() int64 {
|
func (p *ColorPlayer) Id() int64 {
|
||||||
return p.ID
|
return p.ID
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"game/common/model/user"
|
||||||
|
"game/common/proto/pb"
|
||||||
|
"game/common/rpc"
|
||||||
|
"github.com/fox/fox/ipb"
|
||||||
|
"github.com/fox/fox/ksync"
|
||||||
"github.com/fox/fox/processor"
|
"github.com/fox/fox/processor"
|
||||||
|
"github.com/fox/fox/service"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -10,6 +17,46 @@ const (
|
|||||||
|
|
||||||
func (s *ColorService) initProcessor() {
|
func (s *ColorService) initProcessor() {
|
||||||
s.processor.RegisterMessages(processor.RegisterMetas{
|
s.processor.RegisterMessages(processor.RegisterMetas{
|
||||||
//pb.MsgId_C2SUserLoginId: {pb.C2SUserLogin{}, s.onLoginOrRegister},
|
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, s.onLoginOrRegister},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 登录或注册
|
||||||
|
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
||||||
|
ksync.GoSafe(func() {
|
||||||
|
account, code, node := s.checkLoginOrRegister(req)
|
||||||
|
userId := int64(0)
|
||||||
|
rsp := &pb.S2CUserLogin{Code: code}
|
||||||
|
if account != nil && code == pb.ErrCode_OK {
|
||||||
|
// 拉取用户数据
|
||||||
|
us := &user.User{}
|
||||||
|
us, code = s.getUser(userId, req.Username)
|
||||||
|
if code == pb.ErrCode_OK {
|
||||||
|
rsp.UserId = us.ID
|
||||||
|
rsp.Token, _ = generateToken(account.ID, account.Username)
|
||||||
|
userId = rsp.UserId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_S2CUserLoginId), rsp)
|
||||||
|
|
||||||
|
if account != nil && account.ID > 0 {
|
||||||
|
loginLog := &user.UserLoginLog{
|
||||||
|
AccountID: account.ID,
|
||||||
|
LoginIP: req.Ip,
|
||||||
|
LoginTime: time.Now(),
|
||||||
|
DeviceInfo: req.DeviceId,
|
||||||
|
LoginResult: code == pb.ErrCode_OK,
|
||||||
|
FailReason: code.String(),
|
||||||
|
}
|
||||||
|
switch code {
|
||||||
|
case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK:
|
||||||
|
rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpc.LogUserAccountLogin, 0, loginLog)
|
||||||
|
ksync.GoSafe(func() {
|
||||||
|
_, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}, nil)
|
||||||
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"game/common/rpcName"
|
"game/common/rpc"
|
||||||
"github.com/fox/fox/processor"
|
"github.com/fox/fox/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *DbService) initRpcProcessor() {
|
func (s *DbService) initRpcProcessor() {
|
||||||
s.RpcProcessor.RegisterMessages(map[string]processor.RpcHandler{
|
s.RpcProcessor.RegisterMessages(map[string]processor.RpcHandler{
|
||||||
rpcName.CreateUserAccount: s.onCreateUserAccount,
|
rpc.CreateUserAccount: s.onCreateUserAccount,
|
||||||
rpcName.GetUserAccount: s.onGetUserAccount,
|
rpc.GetUserAccount: s.onGetUserAccount,
|
||||||
rpcName.UpdateUserPassword: s.onUpdateUserAccount,
|
rpc.UpdateUserPassword: s.onUpdateUserAccount,
|
||||||
rpcName.LogUserAccountLogin: s.onLogUserAccountLogin,
|
rpc.LogUserAccountLogin: s.onLogUserAccountLogin,
|
||||||
rpcName.GetUserByUid: s.onGetUserByUid,
|
rpc.GetUserByUid: s.onGetUserByUid,
|
||||||
rpcName.GetUserByAccountId: s.onGetUserByAccountId,
|
rpc.GetUserByAccountId: s.onGetUserByAccountId,
|
||||||
rpcName.GetUserResources: s.onGetUserResources,
|
rpc.GetUserResources: s.onGetUserResources,
|
||||||
rpcName.GetGameUser: s.onGetGameUser,
|
rpc.GetGameUser: s.onGetGameUser,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"game/common/model/user"
|
"game/common/model/user"
|
||||||
"game/common/proto/pb"
|
"game/common/proto/pb"
|
||||||
"game/common/rpcName"
|
"game/common/rpc"
|
||||||
"game/common/utils"
|
"game/common/utils"
|
||||||
"github.com/fox/fox/etcd"
|
"github.com/fox/fox/etcd"
|
||||||
"github.com/fox/fox/ipb"
|
"github.com/fox/fox/ipb"
|
||||||
@ -41,7 +41,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User
|
|||||||
DeviceID: req.DeviceId,
|
DeviceID: req.DeviceId,
|
||||||
LastLoginIP: req.Ip,
|
LastLoginIP: req.Ip,
|
||||||
}
|
}
|
||||||
rpcMsg := ipb.MakeRpcMsg[user.UserAccount](rpcName.GetUserAccount, 0, us)
|
rpcMsg := ipb.MakeRpcMsg[user.UserAccount](rpc.GetUserAccount, 0, us)
|
||||||
rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()))
|
log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()))
|
||||||
@ -60,7 +60,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User
|
|||||||
RegisterIP: req.Ip,
|
RegisterIP: req.Ip,
|
||||||
RegisterTime: time.Now(),
|
RegisterTime: time.Now(),
|
||||||
}
|
}
|
||||||
rpcMsg = ipb.MakeRpcMsg[user.UserAccount](rpcName.CreateUserAccount, 0, us)
|
rpcMsg = ipb.MakeRpcMsg[user.UserAccount](rpc.CreateUserAccount, 0, us)
|
||||||
rspMsg, err = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
rspMsg, err = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()))
|
log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()))
|
||||||
@ -71,7 +71,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User
|
|||||||
log.ErrorF(s.Log("call rpc:%v err", rpcMsg.RpcMsgId))
|
log.ErrorF(s.Log("call rpc:%v err", rpcMsg.RpcMsgId))
|
||||||
return nil, pb.ErrCode_SystemErr, node
|
return nil, pb.ErrCode_SystemErr, node
|
||||||
}
|
}
|
||||||
log.DebugF("收到rcp:%v返回数据:%v", rpcName.CreateUserAccount, string(rspMsg.Msg))
|
log.DebugF("收到rcp:%v返回数据:%v", rpc.CreateUserAccount, string(rspMsg.Msg))
|
||||||
}
|
}
|
||||||
if !utils.CheckPassword(req.Password, us.Password) {
|
if !utils.CheckPassword(req.Password, us.Password) {
|
||||||
log.ErrorF(s.Log("用户密码:%v 数据库中密码:%v", req.Password, us.Password))
|
log.ErrorF(s.Log("用户密码:%v 数据库中密码:%v", req.Password, us.Password))
|
||||||
@ -101,7 +101,7 @@ func (s *LoginService) getUser(accountId int64, tName string) (*user.User, pb.Er
|
|||||||
us := &user.User{
|
us := &user.User{
|
||||||
AccountId: accountId,
|
AccountId: accountId,
|
||||||
}
|
}
|
||||||
rpcMsg := ipb.MakeRpcMsg[user.User](rpcName.GetUserByAccountId, 0, us)
|
rpcMsg := ipb.MakeRpcMsg[user.User](rpc.GetUserByAccountId, 0, us)
|
||||||
rsp, err := s.Call(service.RpcTopicEx(tName), timeout, rpcMsg)
|
rsp, err := s.Call(service.RpcTopicEx(tName), timeout, rpcMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ErrorF(s.Log("call rpc:%v err:%s", rpcMsg.RpcMsgId, err.Error()))
|
log.ErrorF(s.Log("call rpc:%v err:%s", rpcMsg.RpcMsgId, err.Error()))
|
||||||
@ -144,7 +144,7 @@ func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.C2SUserL
|
|||||||
}
|
}
|
||||||
switch code {
|
switch code {
|
||||||
case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK:
|
case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK:
|
||||||
rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpcName.LogUserAccountLogin, 0, loginLog)
|
rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpc.LogUserAccountLogin, 0, loginLog)
|
||||||
ksync.GoSafe(func() {
|
ksync.GoSafe(func() {
|
||||||
_, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
_, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||||
}, nil)
|
}, nil)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"game/common/model/user"
|
"game/common/model/user"
|
||||||
"game/common/proto/pb"
|
"game/common/proto/pb"
|
||||||
"game/common/rpcName"
|
"game/common/rpc"
|
||||||
"game/common/utils"
|
"game/common/utils"
|
||||||
"github.com/fox/fox/ipb"
|
"github.com/fox/fox/ipb"
|
||||||
"github.com/fox/fox/ksync"
|
"github.com/fox/fox/ksync"
|
||||||
@ -12,30 +11,6 @@ import (
|
|||||||
"github.com/fox/fox/service"
|
"github.com/fox/fox/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *MatchService) getGameUser(uid int64) (*user.GameUser, pb.ErrCode) {
|
|
||||||
node, err := s.bindService.HashServiceNode(pb.ServiceTypeId_STI_DB, uid)
|
|
||||||
if err != nil {
|
|
||||||
log.ErrorF("db service node error:%v", err)
|
|
||||||
return nil, pb.ErrCode_SystemErr
|
|
||||||
}
|
|
||||||
us := &user.GameUser{
|
|
||||||
User: user.User{
|
|
||||||
ID: uid,
|
|
||||||
},
|
|
||||||
UserResources: user.UserResources{
|
|
||||||
UID: uid,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
rpcMsg := ipb.MakeRpcMsg[user.GameUser](rpcName.GetGameUser, uid, us)
|
|
||||||
rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
|
||||||
if err != nil {
|
|
||||||
log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()))
|
|
||||||
return nil, pb.ErrCode_SystemErr
|
|
||||||
}
|
|
||||||
_ = json.Unmarshal(rspMsg.Msg, us)
|
|
||||||
return us, pb.ErrCode_OK
|
|
||||||
}
|
|
||||||
|
|
||||||
// 匹配房间
|
// 匹配房间
|
||||||
func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
||||||
ksync.GoSafe(func() {
|
ksync.GoSafe(func() {
|
||||||
@ -56,7 +31,7 @@ func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom)
|
|||||||
|
|
||||||
var us *user.GameUser
|
var us *user.GameUser
|
||||||
rsp := &pb.S2CMatchRoom{}
|
rsp := &pb.S2CMatchRoom{}
|
||||||
us, rsp.Code = s.getGameUser(iMsg.UserId)
|
us, rsp.Code = rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId)
|
||||||
if rsp.Code != pb.ErrCode_OK {
|
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_S2CMatchRoomId), rsp)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user