92 lines
3.8 KiB
Go
92 lines
3.8 KiB
Go
// Package grpc 将 room-service 领域服务适配成 protobuf gRPC 接口。
|
||
package grpc
|
||
|
||
import (
|
||
"context"
|
||
|
||
roomv1 "hyapp/api/proto/room/v1"
|
||
roomservice "hyapp/services/room-service/internal/room/service"
|
||
)
|
||
|
||
// Server 把 room-service 领域实现挂到 gRPC 接口上。
|
||
type Server struct {
|
||
// UnimplementedRoomCommandServiceServer 保持新增 RPC 时的向前兼容默认行为。
|
||
roomv1.UnimplementedRoomCommandServiceServer
|
||
// UnimplementedRoomGuardServiceServer 保持守卫服务新增 RPC 时的向前兼容默认行为。
|
||
roomv1.UnimplementedRoomGuardServiceServer
|
||
|
||
// svc 是领域服务入口,gRPC 层不保存任何房间状态。
|
||
svc *roomservice.Service
|
||
}
|
||
|
||
// NewServer 创建一个可注册到 gRPC 的 room server。
|
||
func NewServer(svc *roomservice.Service) *Server {
|
||
return &Server{svc: svc}
|
||
}
|
||
|
||
// CreateRoom 代理到领域服务。
|
||
func (s *Server) CreateRoom(ctx context.Context, req *roomv1.CreateRoomRequest) (*roomv1.CreateRoomResponse, error) {
|
||
// 创建房间会获取 Redis lease、写 MySQL meta/command/outbox/snapshot,并安装本地 Cell。
|
||
return s.svc.CreateRoom(ctx, req)
|
||
}
|
||
|
||
// JoinRoom 代理到领域服务。
|
||
func (s *Server) JoinRoom(ctx context.Context, req *roomv1.JoinRoomRequest) (*roomv1.JoinRoomResponse, error) {
|
||
// JoinRoom 只维护 room-service presence,WS 订阅仍由 im-service 处理。
|
||
return s.svc.JoinRoom(ctx, req)
|
||
}
|
||
|
||
// LeaveRoom 代理到领域服务。
|
||
func (s *Server) LeaveRoom(ctx context.Context, req *roomv1.LeaveRoomRequest) (*roomv1.LeaveRoomResponse, error) {
|
||
// LeaveRoom 不直接关闭 WebSocket,只修改房间业务态。
|
||
return s.svc.LeaveRoom(ctx, req)
|
||
}
|
||
|
||
// MicUp 代理到领域服务。
|
||
func (s *Server) MicUp(ctx context.Context, req *roomv1.MicUpRequest) (*roomv1.MicUpResponse, error) {
|
||
// 麦位修改由 Room Cell 串行执行,gRPC 层不做状态判断。
|
||
return s.svc.MicUp(ctx, req)
|
||
}
|
||
|
||
// MicDown 代理到领域服务。
|
||
func (s *Server) MicDown(ctx context.Context, req *roomv1.MicDownRequest) (*roomv1.MicDownResponse, error) {
|
||
// 下麦结果会进入 command log 和 outbox,失败不产生部分状态。
|
||
return s.svc.MicDown(ctx, req)
|
||
}
|
||
|
||
// ChangeMicSeat 代理到领域服务。
|
||
func (s *Server) ChangeMicSeat(ctx context.Context, req *roomv1.ChangeMicSeatRequest) (*roomv1.ChangeMicSeatResponse, error) {
|
||
// 换位在领域层原子修改源麦位和目标麦位。
|
||
return s.svc.ChangeMicSeat(ctx, req)
|
||
}
|
||
|
||
// MuteUser 代理到领域服务。
|
||
func (s *Server) MuteUser(ctx context.Context, req *roomv1.MuteUserRequest) (*roomv1.MuteUserResponse, error) {
|
||
// 禁言状态是 im-service 发言守卫的权威来源。
|
||
return s.svc.MuteUser(ctx, req)
|
||
}
|
||
|
||
// KickUser 代理到领域服务。
|
||
func (s *Server) KickUser(ctx context.Context, req *roomv1.KickUserRequest) (*roomv1.KickUserResponse, error) {
|
||
// 踢人会修改 presence、ban 集合和麦位占用。
|
||
return s.svc.KickUser(ctx, req)
|
||
}
|
||
|
||
// SendGift 代理到领域服务。
|
||
func (s *Server) SendGift(ctx context.Context, req *roomv1.SendGiftRequest) (*roomv1.SendGiftResponse, error) {
|
||
// SendGift 先同步扣费,成功后才更新房间热度和本地榜。
|
||
return s.svc.SendGift(ctx, req)
|
||
}
|
||
|
||
// CheckSpeakPermission 代理到领域服务。
|
||
func (s *Server) CheckSpeakPermission(ctx context.Context, req *roomv1.CheckSpeakPermissionRequest) (*roomv1.CheckSpeakPermissionResponse, error) {
|
||
// im-service 发公屏前调用该守卫,避免复制禁言和房间状态规则。
|
||
return s.svc.CheckSpeakPermission(ctx, req)
|
||
}
|
||
|
||
// VerifyRoomPresence 代理到领域服务。
|
||
func (s *Server) VerifyRoomPresence(ctx context.Context, req *roomv1.VerifyRoomPresenceRequest) (*roomv1.VerifyRoomPresenceResponse, error) {
|
||
// im-service subscribe_room 前调用该守卫,防止未进房或被踢用户收消息。
|
||
return s.svc.VerifyRoomPresence(ctx, req)
|
||
}
|