Browse Source

Count bytes coming in/out of player

master
Stephen McQuay 6 years ago
parent
commit
b131b4bc53
2 changed files with 52 additions and 17 deletions
  1. +22
    -13
      player.go
  2. +30
    -4
      spectator.go

+ 22
- 13
player.go View File

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

+ 30
- 4
spectator.go View File

@ -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 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 receiving JSON from spectator: %s", s.Id, err)
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()

Loading…
Cancel
Save