server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package server generate by warden_gen
  2. package server
  3. import (
  4. "context"
  5. "time"
  6. pb "go-common/app/interface/main/broadcast/api/grpc/v1"
  7. "go-common/app/interface/main/broadcast/conf"
  8. service "go-common/app/interface/main/broadcast/server"
  9. "go-common/library/net/rpc/warden"
  10. )
  11. // New boradcast grpc server.
  12. func New(c *conf.Config, svr *service.Server) (ws *warden.Server) {
  13. var (
  14. err error
  15. )
  16. ws = warden.NewServer(c.WardenServer)
  17. pb.RegisterZergServer(ws.Server(), &server{svr})
  18. if ws, err = ws.Start(); err != nil {
  19. panic(err)
  20. }
  21. return
  22. }
  23. type server struct {
  24. srv *service.Server
  25. }
  26. var _ pb.ZergServer = &server{}
  27. // Ping Service
  28. func (s *server) Ping(ctx context.Context, req *pb.Empty) (*pb.Empty, error) {
  29. return &pb.Empty{}, nil
  30. }
  31. // Close Service
  32. func (s *server) Close(ctx context.Context, req *pb.Empty) (*pb.Empty, error) {
  33. // TODO: some graceful close
  34. return &pb.Empty{}, nil
  35. }
  36. // PushMsg push a message to specified sub keys.
  37. func (s *server) PushMsg(ctx context.Context, req *pb.PushMsgReq) (reply *pb.PushMsgReply, err error) {
  38. if len(req.Keys) == 0 || req.Proto == nil {
  39. return nil, service.ErrPushMsgArg
  40. }
  41. for _, key := range req.Keys {
  42. if channel := s.srv.Bucket(key).Channel(key); channel != nil {
  43. if !channel.NeedPush(req.ProtoOp, "") {
  44. continue
  45. }
  46. if err = channel.Push(req.Proto); err != nil {
  47. return
  48. }
  49. }
  50. }
  51. return &pb.PushMsgReply{}, nil
  52. }
  53. // Broadcast broadcast msg to all user.
  54. func (s *server) Broadcast(ctx context.Context, req *pb.BroadcastReq) (*pb.BroadcastReply, error) {
  55. if req.Proto == nil {
  56. return nil, service.ErrBroadCastArg
  57. }
  58. go func() {
  59. for _, bucket := range s.srv.Buckets() {
  60. bucket.Broadcast(req.GetProto(), req.ProtoOp, req.Platform)
  61. if req.Speed > 0 {
  62. t := bucket.ChannelCount() / int(req.Speed)
  63. time.Sleep(time.Duration(t) * time.Second)
  64. }
  65. }
  66. }()
  67. return &pb.BroadcastReply{}, nil
  68. }
  69. // BroadcastRoom broadcast msg to specified room.
  70. func (s *server) BroadcastRoom(ctx context.Context, req *pb.BroadcastRoomReq) (*pb.BroadcastRoomReply, error) {
  71. if req.Proto == nil || req.RoomID == "" {
  72. return nil, service.ErrBroadCastRoomArg
  73. }
  74. for _, bucket := range s.srv.Buckets() {
  75. bucket.BroadcastRoom(service.ProtoRoom{
  76. RoomID: req.RoomID,
  77. Proto: req.Proto,
  78. })
  79. }
  80. return &pb.BroadcastRoomReply{}, nil
  81. }
  82. // Rooms gets all the room ids for the server.
  83. func (s *server) Rooms(ctx context.Context, req *pb.RoomsReq) (*pb.RoomsReply, error) {
  84. var (
  85. roomIds = make(map[string]bool)
  86. )
  87. for _, bucket := range s.srv.Buckets() {
  88. for roomID := range bucket.Rooms() {
  89. roomIds[roomID] = true
  90. }
  91. }
  92. return &pb.RoomsReply{Rooms: roomIds}, nil
  93. }