Skip to content

Commit

Permalink
Clean room init/deinit handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Oct 15, 2023
1 parent 989d3b1 commit 072b674
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
15 changes: 11 additions & 4 deletions pkg/worker/coordinatorhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,34 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke
app.EnableCloudStorage(uid, w.storage)
app.EnableRecording(rq.Record, rq.RecordUser, rq.Game.Name)

r.SetApp(app)

w.log.Info().Msgf("Starting the game: %v", rq.Game.Name)
if err := app.Load(game, w.conf.Worker.Library.BasePath); err != nil {
c.log.Error().Err(err).Msgf("couldn't load the game %v", game)
app.Close()
r.Close()
w.router.SetRoom(nil)
return api.EmptyPacket
}
r.SetApp(app)

m := media.NewWebRtcMediaPipe(w.conf.Encoder.Audio, w.conf.Encoder.Video, w.log)
m.AudioSrcHz = app.AudioSampleRate()
m.AudioFrame = w.conf.Encoder.Audio.Frame
m.VideoW, m.VideoH = app.ViewportSize()

r.SetMedia(m)

if err := m.Init(); err != nil {
c.log.Error().Err(err).Msgf("couldn't init the media")
app.Close()
r.Close()
w.router.SetRoom(nil)
return api.EmptyPacket
}
if app.Flipped() {
m.SetVideoFlip(true)
}
r.SetMedia(m)
m.SetPixFmt(app.PixFormat())
m.SetRot(app.Rotation())

r.BindAppMedia()
r.StartApp()
Expand All @@ -148,13 +153,15 @@ func (c *coordinator) HandleTerminateSession(rq api.TerminateSessionRequest[com.
if user := w.router.FindUser(rq.Id); user != nil {
w.router.Remove(user)
user.Disconnect()
w.router.SetRoom(nil)
}
}

// HandleQuitGame handles cases when a user manually exits the game.
func (c *coordinator) HandleQuitGame(rq api.GameQuitRequest[com.Uid], w *Worker) {
if user := w.router.FindUser(rq.Id); user != nil {
w.router.Remove(user)
w.router.SetRoom(nil)
}
}

Expand Down
18 changes: 5 additions & 13 deletions pkg/worker/room/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *Room[T]) SetMedia(m MediaPipe) { r.media = m }
func (r *Room[T]) StartApp() { r.app.Start() }

func (r *Room[T]) Close() {
if r.closed {
if r == nil || r.closed {
return
}
r.closed = true
Expand All @@ -104,17 +104,6 @@ type Router[T Session] struct {
mu sync.Mutex
}

func (r *Router[T]) AddUser(user T) { r.users.Add(user) }

func (r *Router[T]) Close() {
r.mu.Lock()
if r.room != nil {
r.room.Close()
r.room = nil
}
r.mu.Unlock()
}

func (r *Router[T]) FindRoom(id string) *Room[T] {
r.mu.Lock()
defer r.mu.Unlock()
Expand All @@ -129,8 +118,11 @@ func (r *Router[T]) Remove(user T) {
r.Close()
}
}
func (r *Router[T]) Room() *Room[T] { r.mu.Lock(); defer r.mu.Unlock(); return r.room }

func (r *Router[T]) AddUser(user T) { r.users.Add(user) }
func (r *Router[T]) Close() { r.mu.Lock(); r.room.Close(); r.room = nil; r.mu.Unlock() }
func (r *Router[T]) FindUser(uid Uid) T { return r.users.Find(uid.Id()) }
func (r *Router[T]) Room() *Room[T] { r.mu.Lock(); defer r.mu.Unlock(); return r.room }
func (r *Router[T]) SetRoom(room *Room[T]) { r.mu.Lock(); r.room = room; r.mu.Unlock() }
func (r *Router[T]) Users() SessionManager[T] { return r.users }

Expand Down
8 changes: 2 additions & 6 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ type Worker struct {
storage cloud.Storage
}

func (w *Worker) Reset() {
w.log.Debug().Msgf("Users before close: %v", w.router.Users())
w.router.Close()
w.log.Debug().Msgf("Users after close: %v", w.router.Users())
}

const retry = 10 * time.Second

func New(conf config.WorkerConfig, log *logger.Logger) (*Worker, error) {
Expand Down Expand Up @@ -74,6 +68,8 @@ func New(conf config.WorkerConfig, log *logger.Logger) (*Worker, error) {
return worker, nil
}

func (w *Worker) Reset() { w.router.Close() }

func (w *Worker) Start(done chan struct{}) {
for _, s := range w.services {
if s != nil {
Expand Down

0 comments on commit 072b674

Please sign in to comment.