Skip to content

Commit

Permalink
Shorten too large referee messages
Browse files Browse the repository at this point in the history
If the message gets too large, other clients can not parse it anymore, as they often use fixed-sized buffers.
  • Loading branch information
g3force committed May 11, 2024
1 parent 18de9e1 commit 0394392
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions internal/app/publish/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,39 @@ func (p *Publisher) SendMessage(refereeMsg *state.Referee) {
if len(p.conns) == 0 && !p.connect() {
return
}

bytes, err := marshalRefereeMessage(refereeMsg)
for _, conn := range p.conns {
bytes, err := proto.Marshal(refereeMsg)
if err != nil {
log.Printf("Could not marshal referee message: %v\nError: %v", refereeMsg, err)
return
}
_, err = conn.Write(bytes)
if err != nil {
log.Println("Could not write referee message:", err)
p.disconnect()
}
}
}

func marshalRefereeMessage(refereeMsg *state.Referee) ([]byte, error) {
bytes, err := proto.Marshal(refereeMsg)
if err != nil {
log.Printf("Could not marshal referee message: %v\nError: %v", refereeMsg, err)
return nil, err
}

if len(bytes) > maxDatagramSize {
log.Printf("Referee message is too large (%d bytes) to send in one packet. Shortening message.", len(bytes))

shortenedRefereeMsg := proto.Clone(refereeMsg).(*state.Referee)
shortenedRefereeMsg.GameEvents = []*state.GameEvent{}
shortenedRefereeMsg.GameEventProposals = []*state.GameEventProposalGroup{}

bytes, err = proto.Marshal(shortenedRefereeMsg)
if err != nil {
log.Printf("Could not marshal referee message: %v\nError: %v", shortenedRefereeMsg, err)
return nil, err
}

return bytes, nil
}

return bytes, nil
}

0 comments on commit 0394392

Please sign in to comment.