front_proc.go 3.6 KB

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