]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker.go
Fix io.Reader implementations and wrap more errors
[rbdr/mobius] / hotline / tracker.go
index 6ee2a9f502010ccd7479f430284feb75a152b1c3..8f8ddd15e259fd925c14523833e0d3de5d36f346 100644 (file)
@@ -18,6 +18,8 @@ type TrackerRegistration struct {
        PassID      [4]byte // Random number generated by the server
        Name        string  // Server Name
        Description string  // Description of the server
+
+       readOffset int // Internal offset to track read progress
 }
 
 // Read implements io.Reader to write tracker registration payload bytes to slice
@@ -25,7 +27,7 @@ func (tr *TrackerRegistration) Read(p []byte) (int, error) {
        userCount := make([]byte, 2)
        binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
 
-       return copy(p, slices.Concat(
+       buf := slices.Concat(
                []byte{0x00, 0x01}, // Magic number, always 1
                tr.Port[:],
                userCount,
@@ -35,7 +37,16 @@ func (tr *TrackerRegistration) Read(p []byte) (int, error) {
                []byte(tr.Name),
                []byte{uint8(len(tr.Description))},
                []byte(tr.Description),
-       )[:]), io.EOF
+       )
+
+       if tr.readOffset >= len(buf) {
+               return 0, io.EOF // All bytes have been read
+       }
+
+       n := copy(p, buf[tr.readOffset:])
+       tr.readOffset += n
+
+       return n, nil
 }
 
 func register(tracker string, tr *TrackerRegistration) error {