Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlayerInfo functionality #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
20 changes: 20 additions & 0 deletions a2sPlayerRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package steam

import (
"github.com/golang/glog"
"bytes"
)

type A2SPlayerRequest struct {
}

func (A2SPlayerRequest) MarshalBinaryFromChallenge(c ChallengeResponse) ([]byte) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the A2SPlayerRequest should be instantiated with the ChallengeResponse, and not passed to this method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the marshalBinary part. But Why should A2SPlayerRequest know Challenge Response. It is built using a challenge Response. The request itself need not have the challenge as a separate entity. In fact the first implementation had something like

type A2SPlayerRequest struct {
c ChallengeResponse
}

buf := new(bytes.Buffer)

writeRequestPrefix(buf)
writeByte(buf, 'U')
buf.Write(c.GetChallangeNumber())

glog.V(2).Infof("a2SPlayerRequest buffer: %v", buf.Bytes())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all glog lines should have a steam: prefix

please have a look at how it has been done so far

return buf.Bytes()
}
60 changes: 60 additions & 0 deletions a2sPlayerResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package steam

import (
"fmt"
"github.com/golang/glog"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is goimports run on this code?

"errors"
"bytes"
)

type Player struct{
index byte
name string
score int32
duration float32
}

type A2SPlayersResponse struct{
playersCount byte
players []Player
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment problems. please run goimports

}

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{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting issues

return errors.New("steam: invalid header in the a2splayersresponse")
}

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 *A2SPlayersResponse) String() string{
str := fmt.Sprintf("players count: %v\n\n", a.playersCount)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very bad performance wise as strings are immutable. please use a buffer


for _, player := range a.players {
str += fmt.Sprintf("player index: %v\n", player.index)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a look at fmt.Fprintf()

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
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline missing from EOF

20 changes: 20 additions & 0 deletions challengeRequest.go
Original file line number Diff line number Diff line change
@@ -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(2).Infof("challengeRequest buffer: %v", buf.Bytes())
return buf.Bytes()
}
25 changes: 25 additions & 0 deletions challengeResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package steam
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats with the file name this is not java!?


import (
"fmt"
"github.com/golang/glog"
)

type ChallengeResponse struct {
data []byte
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have been simply:

type ChallengeResponse []byte

}

func (c *ChallengeResponse) GetChallangeNumber() (challenge []byte) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not java!

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
}
5 changes: 5 additions & 0 deletions comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions samples/sample.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"

"github.com/kidoman/go-steam"
Expand All @@ -15,13 +16,17 @@ var addresses = []string{
}

func main() {
flag.Parse()
for _, address := range addresses {
server := &steam.Server{Addr: address}
ping, err := server.Ping()
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)
}
}

Expand Down
47 changes: 46 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package steam

import (
"github.com/golang/glog"
"errors"
"time"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -74,18 +76,61 @@ 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()
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
}

return res, nil
}

func (s *Server) PLayerInfo() (*A2SPlayersResponse, 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}
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()
if err != nil {
return nil, err
}
glog.V(2).Infof("received data %v via socket", b)

a2sPlayerRes := new(A2SPlayersResponse)

if err := a2sPlayerRes.UnMarshalBinary(b); err != nil {
return nil, err
}

return a2sPlayerRes, nil
}
2 changes: 2 additions & 0 deletions socket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package steam

import (
"github.com/golang/glog"
"errors"
"net"
)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ 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 {
return float32(readLong(buf))
}


func readString(buf *bytes.Buffer) string {
bytes, err := buf.ReadBytes(0)
if err != nil {
Expand Down