diff --git a/handler/browser.go b/handler/browser.go index b76e9a55..cd57bbd9 100644 --- a/handler/browser.go +++ b/handler/browser.go @@ -106,6 +106,11 @@ func (s *Session) RouteBrowser() { if room == nil { // Create new room room = s.handler.createNewRoom(s.GameName, s.RoomID, s.PlayerIndex) + // Wait for done signal from room + go func() { + <-room.Done + s.handler.detachRoom(room.ID) + }() } // Attach peerconnection to room. If PC is already in room, don't detach diff --git a/handler/handlers.go b/handler/handlers.go index e75483f0..9f9a3102 100644 --- a/handler/handlers.go +++ b/handler/handlers.go @@ -143,6 +143,11 @@ func (h *Handler) getRoom(roomID string) *room.Room { return room } +// detachRoom detach room from Handler +func (h *Handler) detachRoom(roomID string) { + delete(h.rooms, roomID) +} + // createNewRoom creates a new room // Return nil in case of room is existed func (h *Handler) createNewRoom(gameName string, roomID string, playerIndex int) *room.Room { @@ -168,5 +173,5 @@ func (h *Handler) isRoomRunning(roomID string) bool { return false } - return room.IsRunning() + return room.IsRunningSessions() } diff --git a/handler/room/media.go b/handler/room/media.go index 4daed757..1ae594ca 100644 --- a/handler/room/media.go +++ b/handler/room/media.go @@ -32,7 +32,7 @@ func (r *Room) startAudio() { log.Println("Warn: Room ", r.ID, " audio channel closed unexpectedly") return } - if r.Done { + if !r.IsRunning { log.Println("Room ", r.ID, " audio channel closed") return } @@ -85,7 +85,7 @@ func (r *Room) startVideo() { log.Println("Warn: Room ", r.ID, " video channel closed unexpectedly") return } - if r.Done { + if !r.IsRunning { log.Println("Room ", r.ID, " video channel closed") return } diff --git a/handler/room/room.go b/handler/room/room.go index 60c341cc..6bbf9a09 100644 --- a/handler/room/room.go +++ b/handler/room/room.go @@ -22,8 +22,9 @@ type Room struct { imageChannel chan *image.RGBA audioChannel chan float32 inputChannel chan int + IsRunning bool // Done channel is to fire exit event when there is no webRTC session running - Done bool + Done chan struct{} rtcSessions []*webrtc.WebRTC sessionsLock *sync.Mutex @@ -57,8 +58,10 @@ func NewRoom(roomID, gamepath, gameName string, onlineStorage *storage.Client) * rtcSessions: []*webrtc.WebRTC{}, sessionsLock: &sync.Mutex{}, director: director, - Done: false, + IsRunning: true, onlineStorage: onlineStorage, + + Done: make(chan struct{}), } go room.startVideo() @@ -121,7 +124,7 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int // might consider continue here } - if peerconnection.Done || !peerconnection.IsConnected() || r.Done { + if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning { return } @@ -178,7 +181,7 @@ func (r *Room) IsPCInRoom(w *webrtc.WebRTC) bool { } func (r *Room) Close() { - if r.Done { + if !r.IsRunning { return } @@ -187,7 +190,8 @@ func (r *Room) Close() { close(r.director.Done) log.Println("Closing input of room ", r.ID) close(r.inputChannel) - r.Done = true + close(r.Done) + r.IsRunning = true // Close here is a bit wrong because this read channel //close(r.imageChannel) //close(r.audioChannel) @@ -231,7 +235,7 @@ func (r *Room) LoadGame() error { return err } -func (r *Room) IsRunning() bool { +func (r *Room) IsRunningSessions() bool { // If there is running session for _, s := range r.rtcSessions { if s.IsConnected() {