Rewrite TCP Receive Loop
jlouis: "I'd do a simple
gen_tcp:recv(Socket, PacketLength, ?TIMEOUT)
on the socket and then just wait for all data to arrive directly like this, or time out after a while. Then go again and wait if {error, timeout} occurs. It is simpler, pushes work to the C layer, and is probably going to run a faster route than this."
Why would it have been implemented like this in the first place? Where there maybe in the past size limits in gen_tcp:recv, or any such?
I don't really know, but I can only think of lack of knowledge. If we know that we need N bytes, we can decide for how long we are willing to wait on that to arrive and then get them all in one go. If we then get a timeout on the receive, we can choose to pick a smaller receive size to see if there is any progress on the socket at all and loop it a bit at a time, but I would rather just calculate a maximum receive timeout and then handle it that way.