BeamMP / BeamMP-Server

Server for the multiplayer mod BeamMP for BeamNG.drive
https://beammp.com
GNU Affero General Public License v3.0
116 stars 49 forks source link

Download Refactoring #337

Open lionkor opened 2 weeks ago

lionkor commented 2 weeks ago

Is your feature request related to a problem? Please describe. The current download protocol uses two TCP sockets in two threads (one thread per socket) to send mod files in chunks of some size (like 100 MB or so) per thread. When $M$ people join, two threads exist per player, which leads to $M$ additional threads spawned. In total, there are $2M$ threads, each allocating a chunk of size $C$, resulting in memory usage of $2MC$ bytes. For example 10 players joining, at a 100 MB chunk size, results in $10*2\100=2000$ bytes, or 2 GB, of memory usage. Correct my math if I'm wrong, or if I misunderstand the code.

Not only is downloading across two sockets not necessarily more efficient than one socket, but also the code is not thread safe (at least on the launcher side).

This whole thing is being replaced with the new protocol anyways, where we just use sendfile() to send the file. We can adopt this approach (see the new-protocol branch), or send in chunks if we want to keep track of progress.

Describe the solution you'd like

Describe alternatives you've considered HTTP based download, but that's not trivial to do over the same socket. All other alternatives were probably considered and likely are gonna be implemented for the new protocol.

Additional context The current implementation is probably the oldest part of the codebase. Here be dragons and so on.