From b131b4bc53c0d202a06a831a253d43f672e01887 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sun, 16 Feb 2014 23:59:17 -0800 Subject: [PATCH] Count bytes coming in/out of player --- player.go | 35 ++++++++++++++++++++++------------- spectator.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/player.go b/player.go index 0882ffa..c7c655c 100644 --- a/player.go +++ b/player.go @@ -6,6 +6,8 @@ import ( "log" ) +const maxMessageSize = 1024 + type player struct { ws *websocket.Conn Robots []*Robot @@ -17,8 +19,12 @@ type player struct { func (p *player) sender() { log.Printf("%s: sender launched", p.Id) for things := range p.send { - enc := json.NewEncoder(p.ws) - err := enc.Encode(things) + b, err := json.Marshal(things) + if err != nil { + break + } + // XXX: send the count to our bandwidth analyzer ... + _, err = p.ws.Write(b) if err != nil { break } @@ -28,17 +34,22 @@ func (p *player) sender() { } func (p *player) recv() { + buff := make([]byte, maxMessageSize) + var msgs map[string]Instruction for { - // XXX: need to mark myself as having received something, also binding - // such action to a particular game turn ID - var msgs map[string]Instruction - dec := json.NewDecoder(p.ws) - err := dec.Decode(&msgs) + n, err := p.ws.Read(buff) if err != nil { - // TODO: perhaps we could be a bit more precise in the handling of - // this 'error' by selecting on some kill signal channel and this - // json read? - log.Printf("%s: problem receiving JSON from player: %s", p.Id, err) + log.Printf("%s: problem reading from player: %s", p.Id, err) + break + } + // XXX: send n to our bandwidth analyzer ... + if n == len(buff) { + log.Printf("%s: read buffer overfull: %s", p.Id, string(buff)) + break + } + err = json.Unmarshal(buff[:n], &msgs) + if err != nil { + log.Printf("%s: problem reading from player: %s", p.Id, err) break } @@ -50,8 +61,6 @@ func (p *player) recv() { continue } - // log.Printf("%v", msg.FireAt) - if msg.Repair != nil && *msg.Repair == true { r.ActiveScan = false r.TargetSpeed = 0 diff --git a/spectator.go b/spectator.go index d109309..30ac598 100644 --- a/spectator.go +++ b/spectator.go @@ -2,6 +2,7 @@ package main import ( "code.google.com/p/go.net/websocket" + "encoding/json" "log" ) @@ -14,7 +15,12 @@ type Spectator struct { func (s *Spectator) sender() { log.Printf("%s: sender launched", s.Id) for things := range s.send { - err := websocket.JSON.Send(s.ws, things) + b, err := json.Marshal(things) + if err != nil { + break + } + // XXX: send the count to our bandwidth analyzer ... + _, err = s.ws.Write(b) if err != nil { break } @@ -24,13 +30,33 @@ func (s *Spectator) sender() { } func (s *Spectator) recv() { + var msg interface{} + buff := make([]byte, maxMessageSize) for { - var msg interface{} - err := websocket.JSON.Receive(s.ws, &msg) + n, err := s.ws.Read(buff) if err != nil { - log.Printf("%s: problem receiving JSON from spectator: %s", s.Id, err) + log.Printf("%s: problem reading from player: %s", s.Id, err) break } + // XXX: send n to our bandwidth analyzer ... + log.Println(string(buff[:n])) + if n == len(buff) { + log.Printf("%s: read buffer overfull: %s", s.Id, string(buff)) + break + } + err = json.Unmarshal(buff[:n], &msg) + log.Println(n) + if err != nil { + log.Printf("%s: problem reading from player: %s", s.Id, err) + break + } + // After the first bit of handshaking, the rest of the messages should + // only be "{}" for spectators, and the following could hold true: + // + // if string(buff[:n]) != "{}" { + // log.Printf("protocol breach!!") + // break + // } } log.Printf("%s: recv close", s.Id) s.ws.Close()