Count bytes coming in/out of player
This commit is contained in:
parent
ad9d6c1026
commit
b131b4bc53
35
player.go
35
player.go
@ -6,6 +6,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxMessageSize = 1024
|
||||||
|
|
||||||
type player struct {
|
type player struct {
|
||||||
ws *websocket.Conn
|
ws *websocket.Conn
|
||||||
Robots []*Robot
|
Robots []*Robot
|
||||||
@ -17,8 +19,12 @@ type player struct {
|
|||||||
func (p *player) sender() {
|
func (p *player) sender() {
|
||||||
log.Printf("%s: sender launched", p.Id)
|
log.Printf("%s: sender launched", p.Id)
|
||||||
for things := range p.send {
|
for things := range p.send {
|
||||||
enc := json.NewEncoder(p.ws)
|
b, err := json.Marshal(things)
|
||||||
err := enc.Encode(things)
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// XXX: send the count to our bandwidth analyzer ...
|
||||||
|
_, err = p.ws.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -28,17 +34,22 @@ func (p *player) sender() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) recv() {
|
func (p *player) recv() {
|
||||||
|
buff := make([]byte, maxMessageSize)
|
||||||
|
var msgs map[string]Instruction
|
||||||
for {
|
for {
|
||||||
// XXX: need to mark myself as having received something, also binding
|
n, err := p.ws.Read(buff)
|
||||||
// such action to a particular game turn ID
|
|
||||||
var msgs map[string]Instruction
|
|
||||||
dec := json.NewDecoder(p.ws)
|
|
||||||
err := dec.Decode(&msgs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: perhaps we could be a bit more precise in the handling of
|
log.Printf("%s: problem reading from player: %s", p.Id, err)
|
||||||
// this 'error' by selecting on some kill signal channel and this
|
break
|
||||||
// json read?
|
}
|
||||||
log.Printf("%s: problem receiving JSON from player: %s", p.Id, err)
|
// 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
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +61,6 @@ func (p *player) recv() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Printf("%v", msg.FireAt)
|
|
||||||
|
|
||||||
if msg.Repair != nil && *msg.Repair == true {
|
if msg.Repair != nil && *msg.Repair == true {
|
||||||
r.ActiveScan = false
|
r.ActiveScan = false
|
||||||
r.TargetSpeed = 0
|
r.TargetSpeed = 0
|
||||||
|
34
spectator.go
34
spectator.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go.net/websocket"
|
"code.google.com/p/go.net/websocket"
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,7 +15,12 @@ type Spectator struct {
|
|||||||
func (s *Spectator) sender() {
|
func (s *Spectator) sender() {
|
||||||
log.Printf("%s: sender launched", s.Id)
|
log.Printf("%s: sender launched", s.Id)
|
||||||
for things := range s.send {
|
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 {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -24,13 +30,33 @@ func (s *Spectator) sender() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Spectator) recv() {
|
func (s *Spectator) recv() {
|
||||||
|
var msg interface{}
|
||||||
|
buff := make([]byte, maxMessageSize)
|
||||||
for {
|
for {
|
||||||
var msg interface{}
|
n, err := s.ws.Read(buff)
|
||||||
err := websocket.JSON.Receive(s.ws, &msg)
|
|
||||||
if err != nil {
|
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
|
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)
|
log.Printf("%s: recv close", s.Id)
|
||||||
s.ws.Close()
|
s.ws.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user