front_proc.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package model
  2. import (
  3. "errors"
  4. "fmt"
  5. "rocommon"
  6. "rocommon/rpc"
  7. "rocommon/socket"
  8. "rocommon/util"
  9. )
  10. //todo...
  11. var (
  12. GateServiceID string //gate server id 可以用type,group,id来组合
  13. ClientSessionMag socket.SessionManager
  14. ErrSessionNotFound = errors.New("session node not found")
  15. ErrServiceNodeNotFound = errors.New("service node not found")
  16. ErrServiceNodeSdInfoInvalid = errors.New("service node sd info not found")
  17. )
  18. func CreateUser(cliSession rocommon.Session, openId, platform string) *ClientUser {
  19. //todo...
  20. user := ClientMag.AddClient(cliSession, openId, platform)
  21. //绑定到对应的session上(一个session对应一个玩家)
  22. cliSession.(rocommon.ContextSet).SetContextData("user", user, "CreateUser")
  23. return user
  24. }
  25. //session获取用户
  26. func Session2User(cliSession rocommon.Session) *ClientUser {
  27. if cliSession == nil {
  28. return nil
  29. }
  30. if data, ok := cliSession.(rocommon.ContextSet).GetContextData("user"); ok {
  31. //todo...
  32. //外层有设置nil的地方这边需要判断一下
  33. if data == nil {
  34. return nil
  35. }
  36. return data.(*ClientUser)
  37. }
  38. return nil
  39. }
  40. func BindUser2Session(cliUser *ClientUser, cliSession rocommon.Session) error {
  41. if cliSession == nil {
  42. return ErrSessionNotFound
  43. }
  44. oldCli := Session2User(cliSession)
  45. //用户已经绑定
  46. if oldCli != nil {
  47. return errors.New("user has been bind")
  48. }
  49. //绑定到对应的session上(一个session对应一个玩家)
  50. cliSession.(rocommon.ContextSet).SetContextData("user", cliUser, "BinUser2Session")
  51. cliUser.ClientSession = cliSession
  52. return nil
  53. }
  54. func GetUserBySessionID(sessionID uint64) *ClientUser {
  55. return Session2User(ClientSessionMag.GetSession(sessionID))
  56. }
  57. func GetClientSession(cliSessionId uint64) rocommon.Session {
  58. return ClientSessionMag.GetSession(cliSessionId)
  59. }
  60. //将消息广播列表中的客户端
  61. func BroadCastClient(uidList []uint64, msg interface{}) {
  62. msgData, info, err := rpc.EncodeMessage(msg)
  63. if err != nil {
  64. return
  65. }
  66. ackMsg := &rocommon.TransmitPacket{
  67. MsgData: msgData,
  68. MsgId: uint32(info.ID),
  69. }
  70. for _, data := range uidList {
  71. sess := GetClientSession(data)
  72. if sess == nil {
  73. continue
  74. }
  75. sess.Send(ackMsg)
  76. }
  77. }
  78. //绑定客户端连接到后台服务
  79. //CSLoginReq消息的第一次绑定
  80. func BindClientToBackendNew(serviceID string, cliSession rocommon.Session, openId, platform string) (*ClientUser, error) {
  81. //检查是否存在serviceID对应的服务器(远程服务器的一个连接session)
  82. serviceNode := GetServiceNode(serviceID)
  83. if serviceNode == nil {
  84. return nil, ErrServiceNodeNotFound
  85. }
  86. //检查服务器节点信息是否存在
  87. sd := Session2Context(serviceNode)
  88. if sd == nil {
  89. return nil, ErrServiceNodeSdInfoInvalid
  90. }
  91. cliUser := Session2User(cliSession)
  92. //用户已经绑定
  93. if cliUser != nil {
  94. return nil, errors.New(fmt.Sprintf("user has been bind:%v", cliUser))
  95. }
  96. cliUser = CreateUser(cliSession, openId, platform)
  97. //成功登陆gameserver后,把成功的gameserver写入到数据中(离线数据失效后,删除对应的绑定信息,在gameserver中做处理)
  98. cliUser.SetServiceBackend(sd.Name, sd.ID)
  99. //log.Println("[BindClientToBackend] success")
  100. return cliUser, nil
  101. }
  102. func BindClientToBackend(serviceID string, cliSession rocommon.Session) (*ClientUser, bool) {
  103. cliUser := Session2User(cliSession)
  104. util.WarnF("Session2User cliUser=%v", cliUser)
  105. if cliUser == nil {
  106. return cliUser, false
  107. }
  108. if serviceID == "" {
  109. return cliUser, false
  110. }
  111. util.WarnF("BindClientToBackend serviceID=%v", serviceID)
  112. serviceNodeSess := GetServiceNode(serviceID)
  113. util.WarnF("BindClientToBackend serviceNodeSess=%v", serviceNodeSess)
  114. if serviceNodeSess == nil {
  115. RemoveServiceNodeByName(serviceID)
  116. return cliUser, false
  117. }
  118. sd := Session2Context(serviceNodeSess)
  119. if sd == nil {
  120. return cliUser, false
  121. }
  122. cliUser.SetServiceBackend(sd.Name, sd.ID)
  123. return cliUser, true
  124. }