| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package model
- import (
- "errors"
- "fmt"
- "rocommon"
- "rocommon/rpc"
- "rocommon/socket"
- "rocommon/util"
- )
- //todo...
- var (
- GateServiceID string //gate server id 可以用type,group,id来组合
- ClientSessionMag socket.SessionManager
- ErrSessionNotFound = errors.New("session node not found")
- ErrServiceNodeNotFound = errors.New("service node not found")
- ErrServiceNodeSdInfoInvalid = errors.New("service node sd info not found")
- )
- func CreateUser(cliSession rocommon.Session, openId, platform string) *ClientUser {
- //todo...
- user := ClientMag.AddClient(cliSession, openId, platform)
- //绑定到对应的session上(一个session对应一个玩家)
- cliSession.(rocommon.ContextSet).SetContextData("user", user, "CreateUser")
- return user
- }
- //session获取用户
- func Session2User(cliSession rocommon.Session) *ClientUser {
- if cliSession == nil {
- return nil
- }
- if data, ok := cliSession.(rocommon.ContextSet).GetContextData("user"); ok {
- //todo...
- //外层有设置nil的地方这边需要判断一下
- if data == nil {
- return nil
- }
- return data.(*ClientUser)
- }
- return nil
- }
- func BindUser2Session(cliUser *ClientUser, cliSession rocommon.Session) error {
- if cliSession == nil {
- return ErrSessionNotFound
- }
- oldCli := Session2User(cliSession)
- //用户已经绑定
- if oldCli != nil {
- return errors.New("user has been bind")
- }
- //绑定到对应的session上(一个session对应一个玩家)
- cliSession.(rocommon.ContextSet).SetContextData("user", cliUser, "BinUser2Session")
- cliUser.ClientSession = cliSession
- return nil
- }
- func GetUserBySessionID(sessionID uint64) *ClientUser {
- return Session2User(ClientSessionMag.GetSession(sessionID))
- }
- func GetClientSession(cliSessionId uint64) rocommon.Session {
- return ClientSessionMag.GetSession(cliSessionId)
- }
- //将消息广播列表中的客户端
- func BroadCastClient(uidList []uint64, msg interface{}) {
- msgData, info, err := rpc.EncodeMessage(msg)
- if err != nil {
- return
- }
- ackMsg := &rocommon.TransmitPacket{
- MsgData: msgData,
- MsgId: uint32(info.ID),
- }
- for _, data := range uidList {
- sess := GetClientSession(data)
- if sess == nil {
- continue
- }
- sess.Send(ackMsg)
- }
- }
- //绑定客户端连接到后台服务
- //CSLoginReq消息的第一次绑定
- func BindClientToBackendNew(serviceID string, cliSession rocommon.Session, openId, platform string) (*ClientUser, error) {
- //检查是否存在serviceID对应的服务器(远程服务器的一个连接session)
- serviceNode := GetServiceNode(serviceID)
- if serviceNode == nil {
- return nil, ErrServiceNodeNotFound
- }
- //检查服务器节点信息是否存在
- sd := Session2Context(serviceNode)
- if sd == nil {
- return nil, ErrServiceNodeSdInfoInvalid
- }
- cliUser := Session2User(cliSession)
- //用户已经绑定
- if cliUser != nil {
- return nil, errors.New(fmt.Sprintf("user has been bind:%v", cliUser))
- }
- cliUser = CreateUser(cliSession, openId, platform)
- //成功登陆gameserver后,把成功的gameserver写入到数据中(离线数据失效后,删除对应的绑定信息,在gameserver中做处理)
- cliUser.SetServiceBackend(sd.Name, sd.ID)
- //log.Println("[BindClientToBackend] success")
- return cliUser, nil
- }
- func BindClientToBackend(serviceID string, cliSession rocommon.Session) (*ClientUser, bool) {
- cliUser := Session2User(cliSession)
- util.WarnF("Session2User cliUser=%v", cliUser)
- if cliUser == nil {
- return cliUser, false
- }
- if serviceID == "" {
- return cliUser, false
- }
- util.WarnF("BindClientToBackend serviceID=%v", serviceID)
- serviceNodeSess := GetServiceNode(serviceID)
- util.WarnF("BindClientToBackend serviceNodeSess=%v", serviceNodeSess)
- if serviceNodeSess == nil {
- RemoveServiceNodeByName(serviceID)
- return cliUser, false
- }
- sd := Session2Context(serviceNodeSess)
- if sd == nil {
- return cliUser, false
- }
- cliUser.SetServiceBackend(sd.Name, sd.ID)
- return cliUser, true
- }
|