Detach room from handler

This commit is contained in:
giongto35 2019-05-16 02:00:23 +08:00
parent cd0e408854
commit b964a387bd
4 changed files with 23 additions and 9 deletions

View file

@ -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

View file

@ -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()
}

View file

@ -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
}

View file

@ -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() {