From 4e7e44fcd943b0f5ad4bdb33e82a222bf6fabfc9 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Mon, 22 Apr 2019 02:22:17 +0800 Subject: [PATCH] Fix data race --- ui/director.go | 6 ++++++ ui/gameview.go | 34 +++++++++++++++++++++------------- ws.go | 2 +- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ui/director.go b/ui/director.go index d0469bb5..e3c2fc58 100644 --- a/ui/director.go +++ b/ui/director.go @@ -44,6 +44,10 @@ func (d *Director) SetView(view *GameView) { d.timestamp = float64(time.Now().Nanosecond()) / float64(time.Second) } +func (d *Director) UpdateInput(input int) { + d.view.UpdateInput(input) +} + func (d *Director) Step() { timestamp := float64(time.Now().Nanosecond()) / float64(time.Second) dt := timestamp - d.timestamp @@ -68,6 +72,8 @@ L: select { // if there is event from close channel => the game is ended + case input := <-d.inputChannel: + d.UpdateInput(input) case <-d.Done: break L default: diff --git a/ui/gameview.go b/ui/gameview.go index 805bc8c5..1f829413 100644 --- a/ui/gameview.go +++ b/ui/gameview.go @@ -3,6 +3,7 @@ package ui import ( "image" + "github.com/giongto35/cloud-game/nes" ) @@ -29,9 +30,9 @@ const ( const NumKeys = 8 type GameView struct { - console *nes.Console - title string - hash string + console *nes.Console + title string + hash string // equivalent to the list key pressed const above keyPressed [NumKeys * 2]bool @@ -53,22 +54,30 @@ func NewGameView(console *nes.Console, title, hash string, imageChannel chan *im inputChannel: inputChannel, } - go gameview.ListenToInputChannel() return gameview } // ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session -func (view *GameView) ListenToInputChannel() { - for { - keysInBinary := <-view.inputChannel - for i := 0; i < NumKeys*2; i++ { - b := ((keysInBinary & 1) == 1) - view.keyPressed[i] = (view.keyPressed[i] && b) || b - keysInBinary = keysInBinary >> 1 - } +func (view *GameView) UpdateInput(keysInBinary int) { + for i := 0; i < NumKeys*2; i++ { + b := ((keysInBinary & 1) == 1) + view.keyPressed[i] = (view.keyPressed[i] && b) || b + keysInBinary = keysInBinary >> 1 } } +// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session +//func (view *GameView) ListenToInputChannel() { +//for { +//keysInBinary := <-view.inputChannel +//for i := 0; i < NumKeys*2; i++ { +//b := ((keysInBinary & 1) == 1) +//view.keyPressed[i] = (view.keyPressed[i] && b) || b +//keysInBinary = keysInBinary >> 1 +//} +//} +//} + // Enter enter the game view. func (view *GameView) Enter() { // Always reset game @@ -152,5 +161,4 @@ func (view *GameView) updateControllers() { view.console.Controller1.SetButtons(player1Keys) view.console.Controller2.SetButtons(player2Keys) - } diff --git a/ws.go b/ws.go index d6a169a5..187846d5 100644 --- a/ws.go +++ b/ws.go @@ -88,7 +88,7 @@ func (c *Client) receive(id string, f func(response WSPacket) (request WSPacket) if err != nil { log.Println("[!] json marshal error:", err) } - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + //c.conn.SetWriteDeadline(time.Now().Add(writeWait)) c.sendLock.Lock() c.conn.WriteMessage(websocket.TextMessage, resp) c.sendLock.Unlock()