guard input channel

This commit is contained in:
giongto35 2019-05-09 01:56:07 +08:00
parent 0eea6cfcc8
commit 1ac81bdb43
3 changed files with 16 additions and 9 deletions

View file

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

View file

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

View file

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