From 66cd0eb1658b304a0e80710c9fd59222bea8bc7e Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Sun, 1 Jun 2014 19:48:49 +0530 Subject: [PATCH 01/11] added glog statements --- comm.go | 5 +++++ server.go | 6 +++++- socket.go | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/comm.go b/comm.go index f5de99f..c7b5fb8 100644 --- a/comm.go +++ b/comm.go @@ -16,6 +16,7 @@ const ( type ServerType int func (st *ServerType) UnmarshalBinary(data []byte) error { + glog.V(3).Infof("unmarshalling binary %v for server type ", data) switch data[0] { case 'd': *st = STDedicated @@ -51,6 +52,7 @@ var serverTypeStrings = map[ServerType]string{ type Environment int func (e *Environment) UnmarshalBinary(data []byte) error { + glog.V(3).Infof("unmarshalling binary %v for env ", data) switch data[0] { case 'l': *e = ELinux @@ -86,6 +88,7 @@ var environmentStrings = map[Environment]string{ type Visibility int func (v *Visibility) UnmarshalBinary(data []byte) error { + glog.V(3).Infof("unmarshalling binary %v for visibility ", data) switch data[0] { case 0: *v = VPublic @@ -155,6 +158,7 @@ func (InfoRequest) MarshalBinary() ([]byte, error) { writeByte(buf, 'T') writeString(buf, "Source Engine Query") + glog.V(3).Infof("marshaled binary. buffer: %v", buf) return buf.Bytes(), nil } @@ -193,6 +197,7 @@ const ( ) func (r *InfoResponse) UnmarshalBinary(data []byte) (err error) { + glog.V(3).Infof("unmarshalling binary %v", data) defer func() { if e := recover(); e != nil { err = e.(parseError) diff --git a/server.go b/server.go index 2dccf8b..43143ee 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,7 @@ package steam import ( + "github.com/golang/glog" "errors" "time" ) @@ -53,6 +54,7 @@ func (s *Server) Ping() (time.Duration, error) { return 0, err } + glog.V(3).Infof("sending data %v via socket in ping", data) start := time.Now() s.socket.send(data) if _, err := s.socket.receive(); err != nil { @@ -74,6 +76,7 @@ func (s *Server) Info() (*InfoResponse, error) { return nil, err } + glog.V(3).Infof("sending data %v via socket in info", data) if err := s.socket.send(data); err != nil { return nil, err } @@ -81,7 +84,8 @@ func (s *Server) Info() (*InfoResponse, error) { if err != nil { return nil, err } - + glog.V(3).Infof("received data %v via socket", b) + res := new(InfoResponse) if err := res.UnmarshalBinary(b); err != nil { return nil, err diff --git a/socket.go b/socket.go index 318f905..3d18d85 100644 --- a/socket.go +++ b/socket.go @@ -1,6 +1,7 @@ package steam import ( + "github.com/golang/glog" "errors" "net" ) @@ -29,6 +30,7 @@ func (s *socket) close() { } func (s *socket) send(payload []byte) error { + glog.V(3).Infof("writing %v to UDP", payload) n, err := s.conn.WriteToUDP(payload, s.raddr) if err != nil { return err From 70b4039be09d8dead81f207e149235c948db5941 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Sun, 8 Jun 2014 23:56:48 +0530 Subject: [PATCH 02/11] added playerInfo functionality --- AS2PlayerRequest.go | 21 +++++++++++++++ a2sPlayerResponse.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ challengeRequest.go | 20 ++++++++++++++ challengeResponse.go | 15 +++++++++++ samples/sample.go | 3 +++ server.go | 43 +++++++++++++++++++++++++++++ wire.go | 25 +++++++++++++++++ 7 files changed, 191 insertions(+) create mode 100644 AS2PlayerRequest.go create mode 100644 a2sPlayerResponse.go create mode 100644 challengeRequest.go create mode 100644 challengeResponse.go diff --git a/AS2PlayerRequest.go b/AS2PlayerRequest.go new file mode 100644 index 0000000..626ec9e --- /dev/null +++ b/AS2PlayerRequest.go @@ -0,0 +1,21 @@ +package steam + +import ( + "github.com/golang/glog" + "bytes" +) + +type AS2PlayerRequest struct { + challengeRes ChallengeResponse +} + +func (a *AS2PlayerRequest) MarshalBinary() ([]byte) { + buf := new(bytes.Buffer) + + writeRequestPrefix(buf) + writeByte(buf, 'U') + buf.Write(a.challengeRes.GetChallangeNumber()) + + glog.V(3).Infof("AS2PlayerRequest buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) + return buf.Bytes() +} diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go new file mode 100644 index 0000000..5412128 --- /dev/null +++ b/a2sPlayerResponse.go @@ -0,0 +1,64 @@ +package steam + +import ( + "fmt" + "github.com/golang/glog" + "errors" + "bytes" +) + +type Player struct{ + index byte + name string + score int32 + duration float32 +} + +type AS2PlayersResponse struct{ + playersCount byte + players []Player +} + +func invalidAS2PlayersResponseHeader() error { + return errors.New("as2PlayersResponse: ") +} + +func (a *AS2PlayersResponse) UnMarshalData(data []byte) (err error) { + glog.V(3).Infof("unmarshalling data for AS2PlayersResponse: %v", data) + buf := bytes.NewBuffer(data) + + if header := readByte(buf); header != 0x44{ + return errors.New("steam: invalid header in the as2playersresponse") + } + + a.playersCount = readByte(buf) + glog.V(3).Infof("players count: %v", a.playersCount) + a.players = make([]Player, a.playersCount) + + for i := 0; i < int(a.playersCount); i++ { + p := &a.players[i] + p.index = readByte(buf) + glog.V(3).Infof("player index: %v", p.index) + p.name = readString(buf) + glog.V(3).Infof("player name: %v", p.name) + p.score = readLong(buf) + glog.V(3).Infof("player score: %v", p.score) + p.duration = readFloat(buf) + glog.V(3).Infof("player duration: %v", p.duration) + } + + return nil +} + +func (a *AS2PlayersResponse) String() string{ + str := fmt.Sprintf("players count: %v\n\n", a.playersCount) + + for _, player := range a.players { + str += fmt.Sprintf("player index: %v\n", player.index) + str += fmt.Sprintf("player name: %v\n", player.name) + str += fmt.Sprintf("player score: %v\n", player.score) + str += fmt.Sprintf("player duration: %v seconds\n\n", player.duration) + } + + return str +} \ No newline at end of file diff --git a/challengeRequest.go b/challengeRequest.go new file mode 100644 index 0000000..7892a21 --- /dev/null +++ b/challengeRequest.go @@ -0,0 +1,20 @@ +package steam + +import ( + "github.com/golang/glog" + "bytes" +) + +type ChallengeRequest struct { +} + +func (ChallengeRequest) MarshalBinary() ([]byte) { + buf := new(bytes.Buffer) + + writeRequestPrefix(buf) + writeByte(buf, 'U') + writeRequestPrefix(buf) + + glog.V(3).Infof("Challenge request buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) + return buf.Bytes() +} diff --git a/challengeResponse.go b/challengeResponse.go new file mode 100644 index 0000000..b433c03 --- /dev/null +++ b/challengeResponse.go @@ -0,0 +1,15 @@ +package steam + +import ( + "github.com/golang/glog" +) + +type ChallengeResponse struct { + data []byte +} + +func (c *ChallengeResponse) GetChallangeNumber() (challenge []byte) { + glog.V(3).Infof("getting challenge number from %v", c.data) + + return c.data[(len(c.data)-4):] +} diff --git a/samples/sample.go b/samples/sample.go index af0de88..4aff102 100644 --- a/samples/sample.go +++ b/samples/sample.go @@ -21,7 +21,10 @@ func main() { must(err) info, err := server.Info() must(err) + playerInfo, err := server.PLayerInfo() + must(err) fmt.Printf("%v: %v with ping %v\n", address, info, ping) + fmt.Printf("players info: %v\n", playerInfo) } } diff --git a/server.go b/server.go index 43143ee..d8e6458 100644 --- a/server.go +++ b/server.go @@ -93,3 +93,46 @@ func (s *Server) Info() (*InfoResponse, error) { return res, nil } + +func (s *Server) PLayerInfo() (*AS2PlayersResponse, error) { + if err := s.init(); err != nil { + return nil, err + } + + data := ChallengeRequest{}.MarshalBinary() + + glog.V(3).Infof("sending data %v via socket in info", data) + if err := s.socket.send(data); err != nil { + return nil, err + } + + b, err := s.socket.receive() + if err != nil { + return nil, err + } + glog.V(3).Infof("received data %v via socket", b) + + challengeRes := ChallengeResponse{b} + as2PlayerReq := AS2PlayerRequest{challengeRes} + + data = as2PlayerReq.MarshalBinary() + + glog.V(3).Infof("sending data %v via socket in info", data) + if err := s.socket.send(data); err != nil { + return nil, err + } + + b, err = s.socket.receive() + if err != nil { + return nil, err + } + glog.V(3).Infof("received data %v via socket", b) + + as2PlayerRes := new(AS2PlayersResponse) + + if err := as2PlayerRes.UnMarshalData(b); err != nil { + return nil, err + } + + return as2PlayerRes, nil +} diff --git a/wire.go b/wire.go index 7c864bb..840188c 100644 --- a/wire.go +++ b/wire.go @@ -94,6 +94,31 @@ func readLongLong(buf *bytes.Buffer) int64 { return int64(t[0] + t[1]<<8 + t[2]<<16 + t[3]<<24 + t[4]<<32 + t[5]<<40 + t[6]<<48 + t[7]<<56) } +func readLong(buf *bytes.Buffer) int32 { + var t [4]byte + n, err := buf.Read(t[:]) + if err != nil { + triggerError(errCouldNotReadData) + } + if n != 4 { + triggerError(errNotEnoughDataInResponse) + } + return int32(t[0] + t[1]<<8 + t[2]<<16 + t[3]<<24) +} + +func readFloat(buf *bytes.Buffer) float32 { + var t [4]byte + n, err := buf.Read(t[:]) + if err != nil { + triggerError(errCouldNotReadData) + } + if n != 4 { + triggerError(errNotEnoughDataInResponse) + } + return float32(int32(t[0] + t[1]<<8 + t[2]<<16 + t[3]<<24)) +} + + func readString(buf *bytes.Buffer) string { bytes, err := buf.ReadBytes(0) if err != nil { From 1148efdad0c3d604be6b6bfe8bed3dc78701670d Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 9 Jun 2014 00:13:18 +0530 Subject: [PATCH 03/11] refactor: use type casted readLong in readFloat --- wire.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/wire.go b/wire.go index 840188c..ad14616 100644 --- a/wire.go +++ b/wire.go @@ -107,15 +107,7 @@ func readLong(buf *bytes.Buffer) int32 { } func readFloat(buf *bytes.Buffer) float32 { - var t [4]byte - n, err := buf.Read(t[:]) - if err != nil { - triggerError(errCouldNotReadData) - } - if n != 4 { - triggerError(errNotEnoughDataInResponse) - } - return float32(int32(t[0] + t[1]<<8 + t[2]<<16 + t[3]<<24)) + return float32(readLong(buf)) } From 0c75ff71a4dee52f2ef01c0394bff43452814e13 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 9 Jun 2014 09:35:27 +0530 Subject: [PATCH 04/11] correcting typos --- AS2PlayerRequest.go => a2sPlayerRequest.go | 6 +++--- a2sPlayerResponse.go | 14 +++++--------- server.go | 12 ++++++------ 3 files changed, 14 insertions(+), 18 deletions(-) rename AS2PlayerRequest.go => a2sPlayerRequest.go (64%) diff --git a/AS2PlayerRequest.go b/a2sPlayerRequest.go similarity index 64% rename from AS2PlayerRequest.go rename to a2sPlayerRequest.go index 626ec9e..58d9aa1 100644 --- a/AS2PlayerRequest.go +++ b/a2sPlayerRequest.go @@ -5,17 +5,17 @@ import ( "bytes" ) -type AS2PlayerRequest struct { +type A2SPlayerRequest struct { challengeRes ChallengeResponse } -func (a *AS2PlayerRequest) MarshalBinary() ([]byte) { +func (a *A2SPlayerRequest) MarshalBinary() ([]byte) { buf := new(bytes.Buffer) writeRequestPrefix(buf) writeByte(buf, 'U') buf.Write(a.challengeRes.GetChallangeNumber()) - glog.V(3).Infof("AS2PlayerRequest buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) + glog.V(3).Infof("A2SPlayerRequest buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) return buf.Bytes() } diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index 5412128..29e2c4f 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -14,21 +14,17 @@ type Player struct{ duration float32 } -type AS2PlayersResponse struct{ +type A2SPlayersResponse struct{ playersCount byte players []Player } -func invalidAS2PlayersResponseHeader() error { - return errors.New("as2PlayersResponse: ") -} - -func (a *AS2PlayersResponse) UnMarshalData(data []byte) (err error) { - glog.V(3).Infof("unmarshalling data for AS2PlayersResponse: %v", data) +func (a *A2SPlayersResponse) UnMarshalData(data []byte) (err error) { + glog.V(3).Infof("unmarshalling data for A2SPlayersResponse: %v", data) buf := bytes.NewBuffer(data) if header := readByte(buf); header != 0x44{ - return errors.New("steam: invalid header in the as2playersresponse") + return errors.New("steam: invalid header in the a2splayersresponse") } a.playersCount = readByte(buf) @@ -50,7 +46,7 @@ func (a *AS2PlayersResponse) UnMarshalData(data []byte) (err error) { return nil } -func (a *AS2PlayersResponse) String() string{ +func (a *A2SPlayersResponse) String() string{ str := fmt.Sprintf("players count: %v\n\n", a.playersCount) for _, player := range a.players { diff --git a/server.go b/server.go index d8e6458..059a252 100644 --- a/server.go +++ b/server.go @@ -94,7 +94,7 @@ func (s *Server) Info() (*InfoResponse, error) { return res, nil } -func (s *Server) PLayerInfo() (*AS2PlayersResponse, error) { +func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { if err := s.init(); err != nil { return nil, err } @@ -113,9 +113,9 @@ func (s *Server) PLayerInfo() (*AS2PlayersResponse, error) { glog.V(3).Infof("received data %v via socket", b) challengeRes := ChallengeResponse{b} - as2PlayerReq := AS2PlayerRequest{challengeRes} + a2sPlayerRequest := A2SPlayerRequest{challengeRes} - data = as2PlayerReq.MarshalBinary() + data = a2sPlayerRequest.MarshalBinary() glog.V(3).Infof("sending data %v via socket in info", data) if err := s.socket.send(data); err != nil { @@ -128,11 +128,11 @@ func (s *Server) PLayerInfo() (*AS2PlayersResponse, error) { } glog.V(3).Infof("received data %v via socket", b) - as2PlayerRes := new(AS2PlayersResponse) + a2sPlayerRes := new(A2SPlayersResponse) - if err := as2PlayerRes.UnMarshalData(b); err != nil { + if err := a2sPlayerRes.UnMarshalData(b); err != nil { return nil, err } - return as2PlayerRes, nil + return a2sPlayerRes, nil } From 57ee3d8b174975fb17ecb9d89a4d9821797b4e1e Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 9 Jun 2014 10:37:05 +0530 Subject: [PATCH 05/11] changes to a2sPlayerResponse --- a2sPlayerRequest.go | 7 +++---- a2sPlayerResponse.go | 4 ++-- challengeRequest.go | 2 +- challengeResponse.go | 12 +++++++++++- samples/sample.go | 2 ++ server.go | 8 +++----- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/a2sPlayerRequest.go b/a2sPlayerRequest.go index 58d9aa1..d825f85 100644 --- a/a2sPlayerRequest.go +++ b/a2sPlayerRequest.go @@ -6,16 +6,15 @@ import ( ) type A2SPlayerRequest struct { - challengeRes ChallengeResponse } -func (a *A2SPlayerRequest) MarshalBinary() ([]byte) { +func (A2SPlayerRequest) MarshalBinaryFromChallenge(c ChallengeResponse) ([]byte) { buf := new(bytes.Buffer) writeRequestPrefix(buf) writeByte(buf, 'U') - buf.Write(a.challengeRes.GetChallangeNumber()) + buf.Write(c.GetChallangeNumber()) - glog.V(3).Infof("A2SPlayerRequest buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) + glog.V(2).Infof("a2SPlayerRequest buffer: %v", buf.Bytes()) return buf.Bytes() } diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index 29e2c4f..eb0f681 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -19,8 +19,8 @@ type A2SPlayersResponse struct{ players []Player } -func (a *A2SPlayersResponse) UnMarshalData(data []byte) (err error) { - glog.V(3).Infof("unmarshalling data for A2SPlayersResponse: %v", data) +func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { + glog.V(2).Infof("unmarshalling binary for A2SPlayersResponse: %v", data) buf := bytes.NewBuffer(data) if header := readByte(buf); header != 0x44{ diff --git a/challengeRequest.go b/challengeRequest.go index 7892a21..9fa0332 100644 --- a/challengeRequest.go +++ b/challengeRequest.go @@ -15,6 +15,6 @@ func (ChallengeRequest) MarshalBinary() ([]byte) { writeByte(buf, 'U') writeRequestPrefix(buf) - glog.V(3).Infof("Challenge request buffer: %v string: %v", buf.Bytes(), string(buf.Bytes())) + glog.V(2).Infof("challengeRequest buffer: %v", buf.Bytes()) return buf.Bytes() } diff --git a/challengeResponse.go b/challengeResponse.go index b433c03..2f7ea3c 100644 --- a/challengeResponse.go +++ b/challengeResponse.go @@ -1,6 +1,7 @@ package steam import ( + "fmt" "github.com/golang/glog" ) @@ -9,7 +10,16 @@ type ChallengeResponse struct { } func (c *ChallengeResponse) GetChallangeNumber() (challenge []byte) { - glog.V(3).Infof("getting challenge number from %v", c.data) + glog.V(2).Infoln(c) return c.data[(len(c.data)-4):] } + +func (c *ChallengeResponse) String() string { + str := fmt.Sprint("challengeResponse: [") + for i := 0; i < len(c.data); i++ { + str += fmt.Sprintf("%x ", c.data[i]) + } + str += fmt.Sprint("]") + return str +} diff --git a/samples/sample.go b/samples/sample.go index 4aff102..358e052 100644 --- a/samples/sample.go +++ b/samples/sample.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "github.com/kidoman/go-steam" @@ -15,6 +16,7 @@ var addresses = []string{ } func main() { + flag.Parse() for _, address := range addresses { server := &steam.Server{Addr: address} ping, err := server.Ping() diff --git a/server.go b/server.go index 059a252..7c6186c 100644 --- a/server.go +++ b/server.go @@ -113,9 +113,7 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { glog.V(3).Infof("received data %v via socket", b) challengeRes := ChallengeResponse{b} - a2sPlayerRequest := A2SPlayerRequest{challengeRes} - - data = a2sPlayerRequest.MarshalBinary() + data = A2SPlayerRequest{}.MarshalBinaryFromChallenge(challengeRes) glog.V(3).Infof("sending data %v via socket in info", data) if err := s.socket.send(data); err != nil { @@ -126,11 +124,11 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { if err != nil { return nil, err } - glog.V(3).Infof("received data %v via socket", b) + glog.V(2).Infof("received data %v via socket", b) a2sPlayerRes := new(A2SPlayersResponse) - if err := a2sPlayerRes.UnMarshalData(b); err != nil { + if err := a2sPlayerRes.UnMarshalBinary(b); err != nil { return nil, err } From 682c6dae04d7241462a9d847ee332b927f07cc86 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 9 Jun 2014 10:44:54 +0530 Subject: [PATCH 06/11] removing unwanted glogs --- a2sPlayerResponse.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index eb0f681..ecf8421 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -28,19 +28,14 @@ func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { } a.playersCount = readByte(buf) - glog.V(3).Infof("players count: %v", a.playersCount) a.players = make([]Player, a.playersCount) for i := 0; i < int(a.playersCount); i++ { p := &a.players[i] p.index = readByte(buf) - glog.V(3).Infof("player index: %v", p.index) p.name = readString(buf) - glog.V(3).Infof("player name: %v", p.name) p.score = readLong(buf) - glog.V(3).Infof("player score: %v", p.score) p.duration = readFloat(buf) - glog.V(3).Infof("player duration: %v", p.duration) } return nil From 3c00ff808a99430dc063a7508e61c336f658ac05 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Wed, 11 Jun 2014 10:57:00 +0530 Subject: [PATCH 07/11] refactoring code --- a2sPlayerRequest.go | 9 +++++---- a2sPlayerResponse.go | 20 +++++++++++--------- challengeRequest.go | 7 ++++--- challengeResponse.go | 15 +++++++-------- server.go | 12 +++++++----- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/a2sPlayerRequest.go b/a2sPlayerRequest.go index d825f85..edfb8c9 100644 --- a/a2sPlayerRequest.go +++ b/a2sPlayerRequest.go @@ -1,20 +1,21 @@ package steam import ( - "github.com/golang/glog" "bytes" + + "github.com/golang/glog" ) type A2SPlayerRequest struct { } -func (A2SPlayerRequest) MarshalBinaryFromChallenge(c ChallengeResponse) ([]byte) { +func (A2SPlayerRequest) MarshalBinaryFromChallenge(c ChallengeResponse) []byte { buf := new(bytes.Buffer) writeRequestPrefix(buf) writeByte(buf, 'U') - buf.Write(c.GetChallangeNumber()) + buf.Write(c.GetChallange()) - glog.V(2).Infof("a2SPlayerRequest buffer: %v", buf.Bytes()) + glog.V(2).Infof("steam: a2SPlayerRequest buffer: %v", buf.Bytes()) return buf.Bytes() } diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index ecf8421..115e566 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -1,22 +1,24 @@ package steam import ( + "bytes" + "errors" "fmt" + "os" + "github.com/golang/glog" - "errors" - "bytes" ) -type Player struct{ - index byte - name string - score int32 +type Player struct { + index byte + name string + score int32 duration float32 } -type A2SPlayersResponse struct{ +type A2SPlayersResponse struct { playersCount byte - players []Player + players []Player } func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { @@ -52,4 +54,4 @@ func (a *A2SPlayersResponse) String() string{ } return str -} \ No newline at end of file +} diff --git a/challengeRequest.go b/challengeRequest.go index 9fa0332..9797438 100644 --- a/challengeRequest.go +++ b/challengeRequest.go @@ -1,20 +1,21 @@ package steam import ( - "github.com/golang/glog" "bytes" + + "github.com/golang/glog" ) type ChallengeRequest struct { } -func (ChallengeRequest) MarshalBinary() ([]byte) { +func (ChallengeRequest) MarshalBinary() []byte { buf := new(bytes.Buffer) writeRequestPrefix(buf) writeByte(buf, 'U') writeRequestPrefix(buf) - glog.V(2).Infof("challengeRequest buffer: %v", buf.Bytes()) + glog.V(2).Infof("steam: challengeRequest buffer: %v", buf.Bytes()) return buf.Bytes() } diff --git a/challengeResponse.go b/challengeResponse.go index 2f7ea3c..c66d2f4 100644 --- a/challengeResponse.go +++ b/challengeResponse.go @@ -2,23 +2,22 @@ package steam import ( "fmt" + "github.com/golang/glog" ) -type ChallengeResponse struct { - data []byte -} +type ChallengeResponse []byte -func (c *ChallengeResponse) GetChallangeNumber() (challenge []byte) { +func (c ChallengeResponse) GetChallange() (challenge []byte) { glog.V(2).Infoln(c) - return c.data[(len(c.data)-4):] + return c[(len(c) - 4):] } -func (c *ChallengeResponse) String() string { +func (c ChallengeResponse) String() string { str := fmt.Sprint("challengeResponse: [") - for i := 0; i < len(c.data); i++ { - str += fmt.Sprintf("%x ", c.data[i]) + for i := 0; i < len(c); i++ { + str += fmt.Sprintf("%x ", c[i]) } str += fmt.Sprint("]") return str diff --git a/server.go b/server.go index 7c6186c..74f3ef9 100644 --- a/server.go +++ b/server.go @@ -1,9 +1,10 @@ package steam import ( - "github.com/golang/glog" "errors" "time" + + "github.com/golang/glog" ) // Server represents a Source server. @@ -85,7 +86,7 @@ func (s *Server) Info() (*InfoResponse, error) { return nil, err } glog.V(3).Infof("received data %v via socket", b) - + res := new(InfoResponse) if err := res.UnmarshalBinary(b); err != nil { return nil, err @@ -94,10 +95,11 @@ func (s *Server) Info() (*InfoResponse, error) { return res, nil } +// PlayerInfo retrieves players information on the server. func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { if err := s.init(); err != nil { return nil, err - } + } data := ChallengeRequest{}.MarshalBinary() @@ -112,7 +114,7 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { } glog.V(3).Infof("received data %v via socket", b) - challengeRes := ChallengeResponse{b} + challengeRes := ChallengeResponse(b) data = A2SPlayerRequest{}.MarshalBinaryFromChallenge(challengeRes) glog.V(3).Infof("sending data %v via socket in info", data) @@ -124,7 +126,7 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { if err != nil { return nil, err } - glog.V(2).Infof("received data %v via socket", b) + glog.V(2).Infof("received data %v via socket", b) a2sPlayerRes := new(A2SPlayersResponse) From 15279eab9b23acd0da6f0f155b382b22c8c09ad7 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Wed, 11 Jun 2014 11:13:49 +0530 Subject: [PATCH 08/11] changed glogs to be consistent --- a2sPlayerResponse.go | 17 ++++++++--------- challengeResponse.go | 2 +- comm.go | 10 +++++----- server.go | 2 +- socket.go | 5 +++-- wire.go | 1 - 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index 115e566..1794853 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "os" "github.com/golang/glog" ) @@ -22,10 +21,10 @@ type A2SPlayersResponse struct { } func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { - glog.V(2).Infof("unmarshalling binary for A2SPlayersResponse: %v", data) + glog.V(2).Infof("steam: unmarshalling binary for A2SPlayersResponse: %v", data) buf := bytes.NewBuffer(data) - if header := readByte(buf); header != 0x44{ + if header := readByte(buf); header != 0x44 { return errors.New("steam: invalid header in the a2splayersresponse") } @@ -43,14 +42,14 @@ func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { return nil } -func (a *A2SPlayersResponse) String() string{ - str := fmt.Sprintf("players count: %v\n\n", a.playersCount) +func (a *A2SPlayersResponse) String() string { + str := fmt.Sprintf("players count: %v\n\n", a.playersCount) for _, player := range a.players { - str += fmt.Sprintf("player index: %v\n", player.index) - str += fmt.Sprintf("player name: %v\n", player.name) - str += fmt.Sprintf("player score: %v\n", player.score) - str += fmt.Sprintf("player duration: %v seconds\n\n", player.duration) + str += fmt.Sprintf("player index: %v\n", player.index) + str += fmt.Sprintf("player name: %v\n", player.name) + str += fmt.Sprintf("player score: %v\n", player.score) + str += fmt.Sprintf("player duration: %v seconds\n\n", player.duration) } return str diff --git a/challengeResponse.go b/challengeResponse.go index c66d2f4..c9f2d1f 100644 --- a/challengeResponse.go +++ b/challengeResponse.go @@ -9,7 +9,7 @@ import ( type ChallengeResponse []byte func (c ChallengeResponse) GetChallange() (challenge []byte) { - glog.V(2).Infoln(c) + glog.V(2).Infof("steam: getting challenge from %v", c) return c[(len(c) - 4):] } diff --git a/comm.go b/comm.go index c7b5fb8..ff142f8 100644 --- a/comm.go +++ b/comm.go @@ -16,7 +16,7 @@ const ( type ServerType int func (st *ServerType) UnmarshalBinary(data []byte) error { - glog.V(3).Infof("unmarshalling binary %v for server type ", data) + glog.V(3).Infof("steam: unmarshalling binary %v for server type ", data) switch data[0] { case 'd': *st = STDedicated @@ -52,7 +52,7 @@ var serverTypeStrings = map[ServerType]string{ type Environment int func (e *Environment) UnmarshalBinary(data []byte) error { - glog.V(3).Infof("unmarshalling binary %v for env ", data) + glog.V(3).Infof("steam: unmarshalling binary %v for env ", data) switch data[0] { case 'l': *e = ELinux @@ -88,7 +88,7 @@ var environmentStrings = map[Environment]string{ type Visibility int func (v *Visibility) UnmarshalBinary(data []byte) error { - glog.V(3).Infof("unmarshalling binary %v for visibility ", data) + glog.V(3).Infof("steam: unmarshalling binary %v for visibility ", data) switch data[0] { case 0: *v = VPublic @@ -158,7 +158,7 @@ func (InfoRequest) MarshalBinary() ([]byte, error) { writeByte(buf, 'T') writeString(buf, "Source Engine Query") - glog.V(3).Infof("marshaled binary. buffer: %v", buf) + glog.V(3).Infof("steam: marshaled binary. buffer: %v", buf) return buf.Bytes(), nil } @@ -197,7 +197,7 @@ const ( ) func (r *InfoResponse) UnmarshalBinary(data []byte) (err error) { - glog.V(3).Infof("unmarshalling binary %v", data) + glog.V(3).Infof("steam: unmarshalling binary %v", data) defer func() { if e := recover(); e != nil { err = e.(parseError) diff --git a/server.go b/server.go index 74f3ef9..183f11d 100644 --- a/server.go +++ b/server.go @@ -55,7 +55,7 @@ func (s *Server) Ping() (time.Duration, error) { return 0, err } - glog.V(3).Infof("sending data %v via socket in ping", data) + glog.V(3).Infof("steam: sending data %v via socket in ping", data) start := time.Now() s.socket.send(data) if _, err := s.socket.receive(); err != nil { diff --git a/socket.go b/socket.go index 3d18d85..c533b20 100644 --- a/socket.go +++ b/socket.go @@ -1,9 +1,10 @@ package steam import ( - "github.com/golang/glog" "errors" "net" + + "github.com/golang/glog" ) type socket struct { @@ -30,7 +31,7 @@ func (s *socket) close() { } func (s *socket) send(payload []byte) error { - glog.V(3).Infof("writing %v to UDP", payload) + glog.V(3).Infof("steam: writing %v to UDP", payload) n, err := s.conn.WriteToUDP(payload, s.raddr) if err != nil { return err diff --git a/wire.go b/wire.go index ad14616..2980190 100644 --- a/wire.go +++ b/wire.go @@ -110,7 +110,6 @@ func readFloat(buf *bytes.Buffer) float32 { return float32(readLong(buf)) } - func readString(buf *bytes.Buffer) string { bytes, err := buf.ReadBytes(0) if err != nil { From 5baf94a783c2777760e9db9d295ef8cbcb25447a Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Wed, 11 Jun 2014 11:20:16 +0530 Subject: [PATCH 09/11] pulled out send/receive data into a method --- server.go | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/server.go b/server.go index 183f11d..f49739f 100644 --- a/server.go +++ b/server.go @@ -77,15 +77,10 @@ func (s *Server) Info() (*InfoResponse, error) { return nil, err } - glog.V(3).Infof("sending data %v via socket in info", data) - if err := s.socket.send(data); err != nil { - return nil, err - } - b, err := s.socket.receive() + b, err := s.sendAndRecieve(data) if err != nil { return nil, err } - glog.V(3).Infof("received data %v via socket", b) res := new(InfoResponse) if err := res.UnmarshalBinary(b); err != nil { @@ -103,30 +98,18 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { data := ChallengeRequest{}.MarshalBinary() - glog.V(3).Infof("sending data %v via socket in info", data) - if err := s.socket.send(data); err != nil { - return nil, err - } - - b, err := s.socket.receive() + b, err := s.sendAndRecieve(data) if err != nil { return nil, err } - glog.V(3).Infof("received data %v via socket", b) challengeRes := ChallengeResponse(b) data = A2SPlayerRequest{}.MarshalBinaryFromChallenge(challengeRes) - glog.V(3).Infof("sending data %v via socket in info", data) - if err := s.socket.send(data); err != nil { - return nil, err - } - - b, err = s.socket.receive() + b, err = s.sendAndRecieve(data) if err != nil { return nil, err } - glog.V(2).Infof("received data %v via socket", b) a2sPlayerRes := new(A2SPlayersResponse) @@ -136,3 +119,17 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { return a2sPlayerRes, nil } + +func (s *Server) sendAndRecieve(data []byte) ([]byte, error) { + glog.V(3).Infof("steam: sending data %v via socket in info", data) + if err := s.socket.send(data); err != nil { + return nil, err + } + + b, err := s.socket.receive() + if err != nil { + return nil, err + } + glog.V(3).Infof("steam: received data %v via socket", b) + return b, nil +} From 1b0cb1ff3d463b394449bc61b7d52a07becc3b37 Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 16 Jun 2014 09:13:02 +0530 Subject: [PATCH 10/11] changes to as2PlayerRequest --- a2sPlayerRequest.go | 5 +++-- server.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/a2sPlayerRequest.go b/a2sPlayerRequest.go index edfb8c9..7799244 100644 --- a/a2sPlayerRequest.go +++ b/a2sPlayerRequest.go @@ -7,14 +7,15 @@ import ( ) type A2SPlayerRequest struct { + c ChallengeResponse } -func (A2SPlayerRequest) MarshalBinaryFromChallenge(c ChallengeResponse) []byte { +func (a A2SPlayerRequest) MarshalBinary() []byte { buf := new(bytes.Buffer) writeRequestPrefix(buf) writeByte(buf, 'U') - buf.Write(c.GetChallange()) + buf.Write(a.c.GetChallange()) glog.V(2).Infof("steam: a2SPlayerRequest buffer: %v", buf.Bytes()) return buf.Bytes() diff --git a/server.go b/server.go index f49739f..5fc9ba2 100644 --- a/server.go +++ b/server.go @@ -104,7 +104,7 @@ func (s *Server) PLayerInfo() (*A2SPlayersResponse, error) { } challengeRes := ChallengeResponse(b) - data = A2SPlayerRequest{}.MarshalBinaryFromChallenge(challengeRes) + data = A2SPlayerRequest{challengeRes}.MarshalBinary() b, err = s.sendAndRecieve(data) if err != nil { From 7e245b8a2fa7bd7991f112e8662b5898bfb92c0d Mon Sep 17 00:00:00 2001 From: kunalpowar Date: Mon, 16 Jun 2014 10:02:25 +0530 Subject: [PATCH 11/11] changed String() methods to be more efficient --- a2sPlayerResponse.go | 14 +++++++------- challengeResponse.go | 12 ++++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/a2sPlayerResponse.go b/a2sPlayerResponse.go index 1794853..4f1b95d 100644 --- a/a2sPlayerResponse.go +++ b/a2sPlayerResponse.go @@ -43,14 +43,14 @@ func (a *A2SPlayersResponse) UnMarshalBinary(data []byte) (err error) { } func (a *A2SPlayersResponse) String() string { - str := fmt.Sprintf("players count: %v\n\n", a.playersCount) + buf := new(bytes.Buffer) + writeString(buf, fmt.Sprintf("players count: %v\n\n", a.playersCount)) for _, player := range a.players { - str += fmt.Sprintf("player index: %v\n", player.index) - str += fmt.Sprintf("player name: %v\n", player.name) - str += fmt.Sprintf("player score: %v\n", player.score) - str += fmt.Sprintf("player duration: %v seconds\n\n", player.duration) + writeString(buf, fmt.Sprintf("player index: %v\n", player.index)) + writeString(buf, fmt.Sprintf("player name: %v\n", player.name)) + writeString(buf, fmt.Sprintf("player score: %v\n", player.score)) + writeString(buf, fmt.Sprintf("player duration: %v seconds\n\n", player.duration)) } - - return str + return buf.String() } diff --git a/challengeResponse.go b/challengeResponse.go index c9f2d1f..2c18981 100644 --- a/challengeResponse.go +++ b/challengeResponse.go @@ -1,6 +1,7 @@ package steam import ( + "bytes" "fmt" "github.com/golang/glog" @@ -15,10 +16,13 @@ func (c ChallengeResponse) GetChallange() (challenge []byte) { } func (c ChallengeResponse) String() string { - str := fmt.Sprint("challengeResponse: [") + buf := new(bytes.Buffer) + + writeString(buf, fmt.Sprint("challengeResponse: [")) for i := 0; i < len(c); i++ { - str += fmt.Sprintf("%x ", c[i]) + writeString(buf, fmt.Sprintf("%x ", c[i])) } - str += fmt.Sprint("]") - return str + writeString(buf, fmt.Sprint("]")) + + return buf.String() }