Skip to content

Commit

Permalink
Add missing options and methods. (#1)
Browse files Browse the repository at this point in the history
Added MasterServer.Ping() method.
Added timeout option to Query() method.
Added local address option to Query() method.
  • Loading branch information
TheKigen authored Nov 13, 2022
1 parent 12a9789 commit bf764be
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
40 changes: 30 additions & 10 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func (g *GameServer) Ping() time.Duration {
return g.ping
}

func (g *GameServer) QueryTime() time.Time {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.queryTime
}

func (g *GameServer) Name() string {
g.mutex.RLock()
defer g.mutex.RUnlock()
Expand Down Expand Up @@ -173,24 +179,38 @@ func (g *GameServer) Players() []Player {
return g.players
}

func (g *GameServer) Query() error {
g.mutex.Lock()
defer g.mutex.Unlock()
func (g *GameServer) Query(timeout time.Duration, localAddress string) error {
if timeout == 0 {
timeout = 5 * time.Second
}

var localAddr *net.UDPAddr
var err error

s, err := net.ResolveUDPAddr("udp4", g.address)
if len(localAddress) != 0 {
localAddr, err = net.ResolveUDPAddr("udp4", localAddress)
if err != nil {
return err
}
}

remoteAddr, err := net.ResolveUDPAddr("udp4", g.address)
if err != nil {
return err
}

g.ip = s.IP
g.port = s.Port
g.mutex.Lock()
defer g.mutex.Unlock()

g.ip = remoteAddr.IP
g.port = remoteAddr.Port
g.numTeams = 0
g.numPlayers = 0
g.maxPlayers = 0
g.teams = nil
g.players = nil

c, err := net.DialUDP("udp4", nil, s)
c, err := net.DialUDP("udp4", localAddr, remoteAddr)
if err != nil {
return err
}
Expand All @@ -214,7 +234,7 @@ func (g *GameServer) Query() error {
}

readBuffer := make([]byte, 2048)
err = c.SetDeadline(time.Now().Add(5 * time.Second))
err = c.SetDeadline(time.Now().Add(timeout))
if err != nil {
return err
}
Expand All @@ -225,8 +245,8 @@ func (g *GameServer) Query() error {

g.ping = time.Since(g.queryTime)

if !addr.IP.Equal(s.IP) || addr.Port != s.Port {
return fmt.Errorf("t1net.GameServer.Query: Reply address mismatch: %s != %s", s.String(), addr.String())
if !addr.IP.Equal(remoteAddr.IP) || addr.Port != remoteAddr.Port {
return fmt.Errorf("t1net.GameServer.Query: Reply address mismatch: %s != %s", remoteAddr.String(), addr.String())
}

if n < 20 {
Expand Down
2 changes: 1 addition & 1 deletion game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestGameServer(t *testing.T) {
}
}()

err = game.Query()
err = game.Query(0, "")
if err != nil {
t.Fatal(err)
}
Expand Down
46 changes: 36 additions & 10 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ type MasterServer struct {
totalPackets int
}

func (m *MasterServer) Ping() time.Duration {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.ping
}

func (m *MasterServer) QueryTime() time.Time {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.queryTime
}

func (m *MasterServer) Name() string {
m.mutex.RLock()
defer m.mutex.RUnlock()
Expand All @@ -64,21 +76,35 @@ func (m *MasterServer) Servers() []string {
return m.servers
}

func (m *MasterServer) Query() error {
m.mutex.Lock()
defer m.mutex.Unlock()
func (m *MasterServer) Query(timeout time.Duration, localAddress string) error {
if timeout == 0 {
timeout = 5 * time.Second
}

var localAddr *net.UDPAddr
var err error

if len(localAddress) != 0 {
localAddr, err = net.ResolveUDPAddr("udp4", localAddress)
if err != nil {
return err
}
}

s, err := net.ResolveUDPAddr("udp4", m.address)
remoteAddr, err := net.ResolveUDPAddr("udp4", m.address)
if err != nil {
return err
}

m.ip = s.IP
m.port = s.Port
m.mutex.Lock()
defer m.mutex.Unlock()

m.ip = remoteAddr.IP
m.port = remoteAddr.Port
m.serverCount = 0
m.servers = nil

c, err := net.DialUDP("udp4", nil, s)
c, err := net.DialUDP("udp4", localAddr, remoteAddr)
if err != nil {
return err
}
Expand All @@ -105,7 +131,7 @@ func (m *MasterServer) Query() error {
recvBuf := make([]byte, 1024)
m.totalPackets = 1
for p := 0; p < m.totalPackets; p++ {
err := c.SetDeadline(time.Now().Add(5 * time.Second))
err := c.SetDeadline(time.Now().Add(timeout))
if err != nil {
return err
}
Expand All @@ -114,8 +140,8 @@ func (m *MasterServer) Query() error {
return err
}

if !addr.IP.Equal(s.IP) || addr.Port != s.Port {
return fmt.Errorf("t1net.MasterServer.Query: Reply address mismatch: %s != %s", s.String(), addr.String())
if !addr.IP.Equal(remoteAddr.IP) || addr.Port != remoteAddr.Port {
return fmt.Errorf("t1net.MasterServer.Query: Reply address mismatch: %s != %s", remoteAddr.String(), addr.String())
}

if !pingCalculated {
Expand Down
2 changes: 1 addition & 1 deletion master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestMasterServer(t *testing.T) {
}
}()

err = master.Query()
err = master.Query(0, "")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit bf764be

Please sign in to comment.