innerserver.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Code generated by goctl. DO NOT EDIT!
  2. // Source: inner.proto
  3. package server
  4. import (
  5. "context"
  6. "encoding/json"
  7. "github.com/Shopify/sarama"
  8. treemap "github.com/liyue201/gostl/ds/map"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. "go.opentelemetry.io/otel/attribute"
  11. "ylink/comm/kafka"
  12. "ylink/comm/model"
  13. "ylink/comm/trace"
  14. "ylink/core/inner/rpc/internal/ext"
  15. "ylink/core/inner/rpc/internal/logic"
  16. "ylink/core/inner/rpc/internal/svc"
  17. "ylink/core/inner/rpc/pb"
  18. )
  19. type InnerServer struct {
  20. svcCtx *svc.ServiceContext
  21. pb.UnimplementedInnerServer
  22. ConsumerGroup *kafka.ConsumerGroup
  23. }
  24. func NewInnerServer(svcCtx *svc.ServiceContext) *InnerServer {
  25. server := &InnerServer{
  26. svcCtx: svcCtx,
  27. ConsumerGroup: kafka.NewConsumerGroup(&kafka.ConsumerGroupConfig{
  28. KafkaVersion: sarama.V1_0_0_0,
  29. OffsetsInitial: sarama.OffsetNewest,
  30. IsReturnErr: false,
  31. },
  32. svcCtx.Config.KqMsgBoxConsumerConf.Brokers,
  33. []string{svcCtx.Config.KqMsgBoxConsumerConf.Topic},
  34. svcCtx.Config.KqMsgBoxConsumerConf.GroupId),
  35. }
  36. server.subscribe()
  37. return server
  38. }
  39. func (s *InnerServer) PlayerFetchCsInfo(ctx context.Context, in *pb.InnerPlayerFetchCsInfoReq) (*pb.InnerPlayerFetchCsInfoResp, error) {
  40. l := logic.NewPlayerFetchCsInfoLogic(ctx, s.svcCtx)
  41. return l.PlayerFetchCsInfo(in)
  42. }
  43. func (s *InnerServer) PlayerDisconnect(ctx context.Context, in *pb.InnerPlayerDisconnectReq) (*pb.InnerPlayerDisconnectResp, error) {
  44. l := logic.NewPlayerDisconnectLogic(ctx, s.svcCtx)
  45. return l.PlayerDisconnect(in)
  46. }
  47. func (s *InnerServer) CsFetchPlayerQueue(ctx context.Context, in *pb.InnerCsFetchPlayerQueueReq) (*pb.InnerCsFetchPlayerQueueResp, error) {
  48. l := logic.NewCsFetchPlayerQueueLogic(ctx, s.svcCtx)
  49. return l.CsFetchPlayerQueue(in)
  50. }
  51. func (s *InnerServer) CsConnectPlayer(ctx context.Context, in *pb.InnerCsConnectPlayerReq) (*pb.InnerCsConnectPlayerResp, error) {
  52. l := logic.NewCsConnectPlayerLogic(ctx, s.svcCtx)
  53. return l.CsConnectPlayer(in)
  54. }
  55. func (s *InnerServer) UpdateUserStatus(ctx context.Context, in *pb.UpdateUserStatusReq) (*pb.UpdateUserStatusResp, error) {
  56. l := logic.NewUpdateUserStatusLogic(ctx, s.svcCtx)
  57. return l.UpdateUserStatus(in)
  58. }
  59. func (s *InnerServer) Setup(_ sarama.ConsumerGroupSession) error {
  60. return nil
  61. }
  62. func (s *InnerServer) Cleanup(_ sarama.ConsumerGroupSession) error {
  63. return nil
  64. }
  65. func (s *InnerServer) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
  66. for msg := range claim.Messages() {
  67. if msg.Topic == s.svcCtx.Config.KqMsgBoxConsumerConf.Topic {
  68. s.handleMessage(sess, msg)
  69. }
  70. }
  71. return nil
  72. }
  73. func (s *InnerServer) handleMessage(sess sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage) {
  74. traceId := kafka.GetTraceFromHeader(msg.Headers)
  75. if len(traceId) == 0 {
  76. return
  77. }
  78. trace.RunOnTracing(traceId, func(ctx context.Context) {
  79. var message model.KqMessage
  80. if err := json.Unmarshal(msg.Value, &message); err != nil {
  81. logx.WithContext(ctx).Errorf("unmarshal msg error: %v", err)
  82. return
  83. }
  84. trace.StartTrace(ctx, "InnerServer.handleMessage.SendMessage", func(ctx context.Context) {
  85. if len(message.ReceiverId) == 0 || message.ReceiverId == "" {
  86. // 玩家发的消息
  87. p2cMap := ext.Game2PlayerMap.Get(message.GameId).(*treemap.Map)
  88. message.ReceiverId = p2cMap.Get(message.SenderId).(string)
  89. logx.WithContext(ctx).Infof("receiver: %s", message.ReceiverId)
  90. kMsg, _ := json.Marshal(message)
  91. s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(kMsg), message.ReceiverId)
  92. } else {
  93. s.svcCtx.KqMsgBoxProducer.SendMessage(ctx, string(msg.Value), message.ReceiverId)
  94. }
  95. sess.MarkMessage(msg, "")
  96. }, attribute.String("msg.key", string(msg.Key)))
  97. })
  98. }
  99. func (s *InnerServer) subscribe() {
  100. go s.ConsumerGroup.RegisterHandleAndConsumer(s)
  101. }