]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker.go
Refactor TrackerRegistration Read to io.Reader interface
[rbdr/mobius] / hotline / tracker.go
index 4d4c8a601d645e3ece16f623a52008c0388849dd..a56059fa2f04ba3b19596070ba7ca970fd35b224 100644 (file)
@@ -4,49 +4,49 @@ import (
        "bufio"
        "encoding/binary"
        "fmt"
-       "github.com/jhalter/mobius/concat"
+       "io"
        "net"
+       "slices"
        "strconv"
        "time"
 )
 
 // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
 type TrackerRegistration struct {
-       Port        [2]byte // Server listening port number
+       Port        [2]byte // Server's listening TCP port number
        UserCount   int     // Number of users connected to this particular server
-       PassID      []byte  // Random number generated by the server
+       PassID      [4]byte // Random number generated by the server
        Name        string  // Server name
        Description string  // Description of the server
 }
 
-// TODO: reimplement as io.Reader
-func (tr *TrackerRegistration) Read() []byte {
+// Read implements io.Reader to write tracker registration payload bytes to slice
+func (tr *TrackerRegistration) Read(p []byte) (int, error) {
        userCount := make([]byte, 2)
        binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
 
-       return concat.Slices(
-               []byte{0x00, 0x01},
+       return copy(p, slices.Concat(
+               []byte{0x00, 0x01}, // Magic number, always 1
                tr.Port[:],
                userCount,
-               []byte{0x00, 0x00},
-               tr.PassID,
+               []byte{0x00, 0x00}, // Magic number, always 0
+               tr.PassID[:],
                []byte{uint8(len(tr.Name))},
                []byte(tr.Name),
                []byte{uint8(len(tr.Description))},
                []byte(tr.Description),
-       )
+       )[:]), io.EOF
 }
 
 func register(tracker string, tr *TrackerRegistration) error {
        conn, err := net.Dial("udp", tracker)
        if err != nil {
-               return err
+               return fmt.Errorf("failed to dial tracker: %w", err)
        }
+       defer conn.Close()
 
-       b := tr.Read()
-
-       if _, err := conn.Write(b); err != nil {
-               return err
+       if _, err := io.Copy(conn, tr); err != nil {
+               return fmt.Errorf("failed to write to connection: %w", err)
        }
 
        return nil
@@ -125,7 +125,10 @@ func GetListing(addr string) ([]ServerRecord, error) {
        for {
                scanner.Scan()
                var srv ServerRecord
-               _, _ = srv.Read(scanner.Bytes())
+               _, err = srv.Read(scanner.Bytes())
+               if err != nil {
+                       return nil, err
+               }
 
                servers = append(servers, srv)
                if len(servers) == totalSrv {
@@ -146,17 +149,29 @@ func GetListing(addr string) ([]ServerRecord, error) {
 // 00000050  4f 54 4c 2e 63 6f 6d 3a  35 35 30 30 2d 4f 3a b2  |OTL.com:5500-O:.|
 // 00000060  15 7c 00 00 00 00 08 53  65 6e 65 63 74 75 73 20  |.|.....Senectus |
 func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
+       // The name length field is the 11th byte of the server record.  If we don't have that many bytes,
+       // return nil token so the Scanner reads more data and continues scanning.
        if len(data) < 10 {
                return 0, nil, nil
        }
 
        // A server entry has two variable length fields: the name and description.
-       // To get the token length, we first need the name length from the 10th byte:
+       // To get the token length, we first need the name length from the 10th byte
        nameLen := int(data[10])
 
+       // The description length field is at the 12th + nameLen byte of the server record.
+       // If we don't have that many bytes, return nil token so the Scanner reads more data and continues scanning.
+       if len(data) < 11+nameLen {
+               return 0, nil, nil
+       }
+
        // Next we need the description length from the 11+nameLen byte:
        descLen := int(data[11+nameLen])
 
+       if len(data) < 12+nameLen+descLen {
+               return 0, nil, nil
+       }
+
        return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
 }