From 31ec2d6e4c0e9cacb8b87656ff2b337c1f1fb041 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Thu, 9 May 2019 01:56:07 +0800 Subject: [PATCH] guard input channel --- emulator/director.go | 3 ++- emulator/gameview.go | 5 ++++- handler/room/room.go | 17 ++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/emulator/director.go b/emulator/director.go index 753be87f..7f63cdf4 100644 --- a/emulator/director.go +++ b/emulator/director.go @@ -84,7 +84,7 @@ L: //case input := <-d.inputChannel: //d.UpdateInput(input) case <-d.Done: - log.Println("Closed Director") + log.Println("Closing Director") break L default: } @@ -92,6 +92,7 @@ L: d.Step() } d.SetView(nil) + log.Println("Closed Director") } func (d *Director) PlayGame(path string) { diff --git a/emulator/gameview.go b/emulator/gameview.go index a14c2f75..22f5ce8e 100644 --- a/emulator/gameview.go +++ b/emulator/gameview.go @@ -86,7 +86,10 @@ func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan // ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session func (view *GameView) ListenToInputChannel() { for { - keysInBinary := <-view.inputChannel + keysInBinary, ok := <-view.inputChannel + if !ok { + return + } for i := 0; i < NumKeys*2; i++ { b := ((keysInBinary & 1) == 1) view.keyPressed[i] = (view.keyPressed[i] && b) || b diff --git a/handler/room/room.go b/handler/room/room.go index 8bffbc13..f4a2ae45 100644 --- a/handler/room/room.go +++ b/handler/room/room.go @@ -125,7 +125,11 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int // encode frame if peerconnection.IsConnected() { - input := <-peerconnection.InputChannel + input, ok := <-peerconnection.InputChannel + if !ok { + continue + // might return here + } // the first 8 bits belong to player 1 // the next 8 belongs to player 2 ... // We standardize and put it to inputChannel (16 bits) @@ -169,13 +173,12 @@ func (r *Room) removeSession(w *webrtc.WebRTC) { func (r *Room) Close() { log.Println("Closing room", r) - r.director.Done <- struct{}{} close(r.Done) - // Not close fan input channel here, close in writer - //close(r.inputChannel) - // Close fan output channel - close(r.imageChannel) - close(r.audioChannel) + close(r.director.Done) + close(r.inputChannel) + // Close here is a bit wrong because this read channel + //close(r.imageChannel) + //close(r.audioChannel) } func (r *Room) SaveGame() error {