Skip to content

Commit

Permalink
client Login,message distribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jzy committed Aug 24, 2021
1 parent d8a3561 commit a50fb71
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Navmesh寻路 [服务器](https://github.com/jzyong/game-server/tree/master/game
* Influxdb
* mongoDB
* 多网关测试,消息分发
* 编写测试客户端(测试tcp通信,模拟登录流程)
* 编写测试客户端(测试tcp通信,模拟登录流程) (转发大厅返回消息给client)



Expand Down
4 changes: 4 additions & 0 deletions src/gate/gate_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/jzyong/go-mmo-server/src/core/log"
"github.com/jzyong/go-mmo-server/src/core/util"
"github.com/jzyong/go-mmo-server/src/gate/config"
"github.com/jzyong/go-mmo-server/src/gate/handler"
"github.com/jzyong/go-mmo-server/src/gate/manager"
"runtime"
)
Expand All @@ -28,6 +29,9 @@ func main() {
return
}
manager.Module.Run()

handler.RegisterClientHandler()
handler.RegisterGameHandler()
util.WaitForTerminate()
manager.Module.Stop()
log.Info("gate stop")
Expand Down
16 changes: 16 additions & 0 deletions src/gate/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handler

import (
"github.com/jzyong/go-mmo-server/src/gate/manager"
"github.com/jzyong/go-mmo-server/src/message"
)

//注册client handler
func RegisterClientHandler() {
manager.GetClientManager().RegisterHandler(int32(message.MID_PlayerHeartReq), HandlePlayerHeartReq)
}

//注册game handler
func RegisterGameHandler() {
manager.GetGameManager().RegisterHandler(int32(message.MID_ServerRegisterUpdateReq), HandleServerRegister)
}
4 changes: 4 additions & 0 deletions src/gate/handler/player_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"time"
)

//func init() {
// manager.GetClientManager().RegisterHandler(int32(message.MID_PlayerHeartReq), HandlePlayerHeartReq)
//}

//玩家心跳请求
func HandlePlayerHeartReq(msg network.TcpMessage) bool {
response := &message.PlayerHeartResponse{
Expand Down
14 changes: 14 additions & 0 deletions src/gate/handler/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/jzyong/go-mmo-server/src/core/log"
network "github.com/jzyong/go-mmo-server/src/core/network/tcp"
"github.com/jzyong/go-mmo-server/src/gate/manager"
"github.com/jzyong/go-mmo-server/src/message"
)

Expand All @@ -27,3 +28,16 @@ func HandleServerList(msg network.TcpMessage) bool {
network.SendClientProtoMsg(msg.GetChannel(), int32(message.MID_ServerListRes), response)
return true
}

//后端服务器注册
func HandleServerRegister(msg network.TcpMessage) bool {
request := &message.ServerRegisterUpdateRequest{}
proto.Unmarshal(msg.GetData(), request)
serverInfo := request.GetServerInfo()
//log.Infof("server %d %d: %s register to gate state %d", serverInfo.GetId(), serverInfo.GetType(), serverInfo.GetIp(), serverInfo.GetState())

manager.GetGameManager().UpdateHallServerInfo(serverInfo, msg.GetChannel())
//TODO 加到连接管理中

return true
}
19 changes: 11 additions & 8 deletions src/gate/manager/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
network "github.com/jzyong/go-mmo-server/src/core/network/tcp"
"github.com/jzyong/go-mmo-server/src/core/util"
"github.com/jzyong/go-mmo-server/src/gate/config"
"github.com/jzyong/go-mmo-server/src/gate/handler"
"github.com/jzyong/go-mmo-server/src/message"
)

//注册处理来自客户端的消息
func (this *ClientManager) registerHandlers() {
this.server.RegisterHandler(int32(message.MID_PlayerHeartReq), handler.HandlePlayerHeartReq)
}
////注册处理来自客户端的消息
//func (this *ClientManager) registerHandlers() {
// this.server.RegisterHandler(int32(message.MID_PlayerHeartReq), handler.HandlePlayerHeartReq)
//}

//客户端网络连接管理
type ClientManager struct {
Expand All @@ -39,7 +37,7 @@ func (this *ClientManager) Init() error {
this.server = server
this.server.SetChannelActive(clientChannelActive)
this.server.SetChannelInactive(clientChannelInactive)
this.registerHandlers()
//this.registerHandlers()
go this.server.Start()

log.Info("ClientManager:inited")
Expand All @@ -51,6 +49,11 @@ func (this *ClientManager) GetServer() network.Server {
return this.server
}

//注册消息
func (this ClientManager) RegisterHandler(mid int32, handler network.HandlerMethod) {
this.server.RegisterHandler(mid, handler)
}

//链接激活
func clientChannelActive(channel network.Channel) {
// 创建用户,加入。。。
Expand Down Expand Up @@ -83,7 +86,7 @@ func clientChannelInactive(channel network.Channel) {
func unregisterMessageDistribute(tcpMessage network.TcpMessage) {
log.Debugf("转发消息:%d", tcpMessage.GetMsgId())
u, _ := tcpMessage.GetChannel().GetProperty("user")
user := u.(User)
user := u.(*User)
user.SendTcpMessageToHall(tcpMessage)
}

Expand Down
58 changes: 49 additions & 9 deletions src/gate/manager/game_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
network "github.com/jzyong/go-mmo-server/src/core/network/tcp"
"github.com/jzyong/go-mmo-server/src/core/util"
"github.com/jzyong/go-mmo-server/src/gate/config"
"github.com/jzyong/go-mmo-server/src/gate/handler"
"github.com/jzyong/go-mmo-server/src/message"
"sync"
)
Expand All @@ -19,7 +18,9 @@ type GameManager struct {
}

func NewGameManager() *GameManager {
return &GameManager{}
return &GameManager{
HallGames: make(map[int32]*GameServerInfo),
}
}

//@
Expand All @@ -33,7 +34,7 @@ func (this *GameManager) Init() error {
this.server = server
this.server.SetChannelActive(gameChannelActive)
this.server.SetChannelInactive(gameChannelInactive)
this.registerHandlers()
//this.registerHandlers()
go this.server.Start()

log.Info("GameManager:inited")
Expand All @@ -49,18 +50,58 @@ func (this *GameManager) GetServer() network.Server {
return this.server
}

//注册消息
func (this GameManager) RegisterHandler(mid int32, handler network.HandlerMethod) {
this.server.RegisterHandler(mid, handler)
}

//更新服务器列表
func (this *GameManager) UpdateHallServerInfo(serverInfo *message.ServerInfo, channel network.Channel) {
this.HallGamesLock.Lock()
defer this.HallGamesLock.Unlock()

hallGame, ok := this.HallGames[serverInfo.GetId()]
if !ok {
hallGame = &GameServerInfo{
ServerId: serverInfo.GetId(),
}
this.HallGames[serverInfo.GetId()] = hallGame
channel.SetProperty("serverId", serverInfo.GetId())
log.Infof("server %d-%d %s register to gate", serverInfo.GetType(), serverInfo.GetId(), serverInfo.GetIp())
}
hallGame.State = serverInfo.GetState()
hallGame.ServerType = serverInfo.GetType()
hallGame.Channel = channel
}

func (this *GameManager) RemoveHall(serverId int32) {
this.HallGamesLock.Lock()
defer this.HallGamesLock.Unlock()
delete(this.HallGames, serverId)
}

//获取大厅后端
func (this *GameManager) GetGameServerInfo(serverId int32) *GameServerInfo {
this.HallGamesLock.Lock()
defer this.HallGamesLock.Unlock()
server := this.HallGames[serverId]
return server
}

//链接激活
func gameChannelActive(channel network.Channel) {
//TODO 创建用户,加入。。。
}

//链接断开
func gameChannelInactive(channel network.Channel) {
//TODO 移除用户,。。。
}

func (this *GameManager) registerHandlers() {
this.server.RegisterHandler(int32(message.MID_ServerListReq), handler.HandleServerList)
// 移除服务器
id, err := channel.GetProperty("serverId")
if err == nil {
serverId := id.(int32)
GetGameManager().RemoveHall(serverId)
log.Infof("hall server %d close", serverId)
}
}

func (this *GameManager) Stop() {
Expand All @@ -76,5 +117,4 @@ type GameServerInfo struct {
ServerType int32
Channel network.Channel
State int32 //服务器状态

}
10 changes: 3 additions & 7 deletions src/gate/manager/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,9 @@ func (u *User) SendToHall(mid message.MID, message proto.Message) {
//向游戏服发消息
func (u *User) SendTcpMessageToHall(tcpMessage network.TcpMessage) {
if u.GameChannel == nil {
//TODO 获取链接
//serverInfo, _ := manager.ServerInfoManagerInstance.GetGameServerInfo(0)
//if serverInfo == nil {
// log.Error("没有找到一个可用的大厅:", mid)
// return
//}
//u.GameChannel = serverInfo.Channel
//TODO 获取链接 暂时写死,后面根据规则获取分配
server := GetGameManager().GetGameServerInfo(1)
u.GameChannel = server.Channel
}
network.SendMsg(u.GameChannel, tcpMessage.GetMsgId(), u.SessionId, u.Id, tcpMessage.GetData())
}
Expand Down
2 changes: 2 additions & 0 deletions src/hall/hall_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/jzyong/go-mmo-server/src/core/log"
"github.com/jzyong/go-mmo-server/src/core/util"
"github.com/jzyong/go-mmo-server/src/hall/config"
"github.com/jzyong/go-mmo-server/src/hall/handler"
"github.com/jzyong/go-mmo-server/src/hall/manager"
"runtime"
)
Expand All @@ -31,6 +32,7 @@ func main() {
return
}
manager.Module.Run()
handler.RegisterHandlers()
util.WaitForTerminate()
manager.Module.Stop()
log.Info("hall stop")
Expand Down
11 changes: 11 additions & 0 deletions src/hall/handler/hander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package handler

import (
"github.com/jzyong/go-mmo-server/src/hall/manager"
"github.com/jzyong/go-mmo-server/src/message"
)

//注册消息处理器
func RegisterHandlers() {
manager.GetClientManager().RegisterHandler(message.MID_UserLoginReq, HandUserLogin)
}
7 changes: 4 additions & 3 deletions src/hall/handler/player_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"time"
)

func init() {
manager.GetClientManager().MessageDistribute.RegisterHandler(int32(message.MID_UserLoginReq), network.NewTcpHandler(HandUserLogin))
}
//func init() {
// manager.GetClientManager().MessageDistribute.RegisterHandler(int32(message.MID_UserLoginReq), network.NewTcpHandler(HandUserLogin))
//}

//处理玩家登录
func HandUserLogin(msg network.TcpMessage) bool {
Expand All @@ -30,6 +30,7 @@ func HandUserLogin(msg network.TcpMessage) bool {
//登录世界服,测试
c, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
//TODO nil 处理
worldResponse, _ := manager.GetClientManager().PlayerWorldClient.Login(c, request)
log.Infof("world return %v", worldResponse)

Expand Down
6 changes: 5 additions & 1 deletion src/hall/manager/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (this *ClientManager) Init() error {
return nil
}

//注册消息
func (this *ClientManager) RegisterHandler(mid message.MID, method network.HandlerMethod) {
this.MessageDistribute.RegisterHandler(int32(mid), network.NewTcpHandler(method))
}

//发送心跳消息
func sendServerHeartMessage(channel network.Channel) {
if channel == nil {
Expand All @@ -94,7 +99,6 @@ func sendServerHeartMessage(channel network.Channel) {
}

SendMsg(channel, int32(message.MID_ServerRegisterUpdateReq), -1, request)

}

//链接激活
Expand Down

0 comments on commit a50fb71

Please sign in to comment.