I'm developing a multiplayer game that has one player assigned as the host, which all other players connect to. All game data is sent using Netstream.send.
I'm finding that when the game room is brand new, calling Netstream.send has no noticeable effect at all on CPU usage. But after about half an hour of gameplay with 4 or 5 players, and quite a few people have connecting and disconnecting in the meantime, send becomes very expensive on the hosts machine. Sending 5 messages a second increases the CPU usage by about 10% for every player connected to the Netstream. No effect when the room is new. Receiving messages is unaffected. Messages can be very small, just an RPC with no parameters and it still causes slowdown.
I've set up Scout to profile it, but it shows no increase in frame time at all. I was expecting to see the yellow (network) part of the bars get larger and larger as time went on, or perhaps a large increase in Actionscript time due to a coding mistake, but none of the frame time was affected at all. The CPU might be pushed to 60 - 70%, but the frame time was showing as requiring less than 10% of available time. The little red CPU bars did however increase as expected.
I've not yet managed to determine exactly what causes Send to become expensive, in a test environment I've connected and disconnected 50 times but that had no effect, and I've sent thousands of Send messages but that didn't make it become expensive either. Annoyingly it only seems to happen in real world deployment.
I'm hoping someone might have come across this before, or have advice on making sure to clean up properly after users disconnect. Or is it possible this is a bug in Flash Player? I've tried it in 11.2 Firefox on Linux x64, and 13.0 Standalone on Windows Server 2012 R2.
Clearly you can see the point where Netstream.send starts and stops being sent 5 times a second.
All help greatly appreciated!
Well, I got to the bottom of it. It was in fact being caused by players joining and leaving, and it was because the streams I set up to receive their actions weren't being cleaned up properly when they left. These were different Netstreams to the one I was doing Netstream.send on, but they were under the same NetConnection.
It would still be nice to know why this slowdown happens, and whether or not it's supposed to happen...
It means that when it comes to large games with lots of connected players, say 20+, the limiting factor isn't the host machines upload speed, but rather the massive amount of CPU required simply to send the data. I'm also very confused why Scout would show no increase at all in frame time.