fixing up some outbox race conditions

master
Jordan Orelli 4 years ago
parent 38e293caf4
commit e36d7bdde2

@ -50,7 +50,7 @@ func (p *player) start(c chan Request, conn *websocket.Conn, r *room) {
welcome.Players[p.name] = wp welcome.Players[p.name] = wp
} }
p.Info("sending welcome to outbox") p.Info("sending welcome to outbox")
p.outbox <- wire.Response{Re: 1, Body: welcome} p.send(wire.Response{Re: 1, Body: welcome})
p.Info("sent welcome, starting loops") p.Info("sent welcome, starting loops")
p.stop = make(chan bool, 1) p.stop = make(chan bool, 1)
go p.readLoop(c, conn) go p.readLoop(c, conn)
@ -115,6 +115,20 @@ func (p *player) runLoop(conn *websocket.Conn) {
} }
} }
func (p *player) send(res wire.Response) bool {
select {
case p.outbox <- res:
return true
default:
select {
case <-p.outbox:
return p.send(res)
default:
return false
}
}
}
func sendResponse(conn *websocket.Conn, res wire.Response) error { func sendResponse(conn *websocket.Conn, res wire.Response) error {
payload, err := json.Marshal(res) payload, err := json.Marshal(res)
if err != nil { if err != nil {

@ -193,9 +193,9 @@ func (w *world) tick(d time.Duration) {
res := req.Wants.exec(w, r, p, req.Seq) res := req.Wants.exec(w, r, p, req.Seq)
if res.reply != nil { if res.reply != nil {
p.outbox <- wire.Response{Re: req.Seq, Body: res.reply} p.send(wire.Response{Re: req.Seq, Body: res.reply})
} else { } else {
p.outbox <- wire.Response{Re: req.Seq, Body: wire.OK{}} p.send(wire.Response{Re: req.Seq, Body: wire.OK{}})
} }
} }
} }
@ -214,7 +214,7 @@ func (w *world) tick(d time.Duration) {
} }
for _, p := range r.players { for _, p := range r.players {
p.outbox <- wire.Response{Body: frame} p.send(wire.Response{Body: frame})
} }
} }
} }

Loading…
Cancel
Save