RobustToolbox icon indicating copy to clipboard operation
RobustToolbox copied to clipboard

Client crashes when you run around

Open h3half opened this issue 9 years ago • 19 comments

If you load up the client in VS and monitor process memory, you'll see that it will quickly (10-15sec) reach 2GB if you're in-game running around.

image

Image is what's shown on the client's command window before crash. Once that starts being reported, the client turns all black and never recovers. Every movement input is responded to with another line of Failed to create an OpenGL context for this window.

Eventually VS brings up SpriteExt.Draw as the source of the error, saying System.AccessViolationException on the one line inside SpriteExt.Draw.

My quick guess is something to do with player movement sprites. You don't actually have to leave the initial box area to crash the game, and it seems to happen faster if you run.

h3half avatar May 23 '17 02:05 h3half

~~@Silvertorch5 found the issue: This code he wrote to un-fuck some SFML thing where SFML decides to vertically flip textures every even frame.~~

~~ROBUSTENGINE will fix this, someday.~~

This is probably totally wrong, as disabling lighting didn't fix the issue

h3half avatar May 23 '17 03:05 h3half

Visual Studio says something funky in here is happening.

toStream does not seem to be getting garbage collected.

Pangogie avatar May 23 '17 06:05 Pangogie

This is related to a memory leak in the BsDiffLib library, which is a .NET wrapper of a binary file patching utility. I still want to look at it more, but this seems to be a very odd bug up to now.

d34d10cc avatar May 24 '17 13:05 d34d10cc

Jesus christ.

So apparently the way sending state to clients works is by serializing two gamestate objects, diffing their binary result, and sending that to the client.

Of course this diffing library is now shitting the bed because this a ridiculously retarded way to handle network syncing of data.

This entire thing needs a complete rewrite.

PJB3005 avatar May 24 '17 19:05 PJB3005

https://github.com/space-wizards/space-station-14/blob/master/SS14.Client/State/States/GameScreen.cs#L1226

This portion of the code is responsible for the System.AccessViolation in SFML.

I have disabled lighting and shadows in https://github.com/space-wizards/space-station-14/commit/0730545fbd201596f5e4f679a5c55e3b3ea5abcf

The opengl context issue i am still investigating.

ZoldorfTheWizard avatar May 26 '17 02:05 ZoldorfTheWizard

@Silvertorch5 It's already being worked on. The OpenGL issue is just the graphics system going haywire due to memory overflow.

d34d10cc avatar May 27 '17 12:05 d34d10cc

because this a ridiculously retarded way to handle network syncing of data.

What? This is a robust and extremely simple way of handling this for a prototype. It's a trivial implementation of delta encoding that worked really well... In VS2010, when I wrote it. Fix the diff lib or build a better one, but don't waste time rewriting the whole state sync system until you actually have a working game. It's heartbreaking watching people flail like this.

volundr- avatar May 27 '17 13:05 volundr-

It's not robust. It's inefficient in every way and sure not what I would've come up with had I been writing this originally. And I'd wager the system I came up with would still be really simple and tons more efficient.

You do have a point with "for a prototype" though. While this is something we have ton rewrite, if we can band aid it to work for now we should.

@Quantomicus listen to the man.

PJB3005 avatar May 27 '17 13:05 PJB3005

Worst case scenario we re-use the work you've already done.

PJB3005 avatar May 27 '17 13:05 PJB3005

welp

I don't know what or how much you know @PJB3005, so maybe this will just be for the benefit of all the people who read this shit in the future wondering what the fuck its all about.

Problem:

How do you keep clients reliably in sync with an authoritative server over some arbitrary network?

Naive answer: use TCP and just send messages about everything that changes, it's fucking simple!

Correct answer: use UDP and send just the right information to keep the client up to date whenever it receives one of your messages successfully.

Why is TCP bad? Go google "Why is TCP bad for game networking" but the gist is that TCP is built for getting data from point A to point B reliably. It doesn't really care about whether you're sending data that needs to arrive in real time, or that you just wanted to send a few bits. It's going to do shit like retransmission, nagles, etc which makes your nice realtime game experience go to absolute shit the second you get any ping or packet loss issues whatsoever.

So we use UDP. UDP is basically like closing your eyes and throwing a message at your receiver. You don't give a flying fuck if it gets there or not, because you're already writing the next message and closing your eyes to throw it. Each message contains the most recent information, and is guaranteed to contain everything the receiver needs to know, regardless of if they've received the previous messages.

Question: What can we send via UDP that will:

  • keep the client up to date
  • not matter if a packet is lost
  • not matter if a packet arrives out of order

Answer: The game state! Just send the state of the world every tick! :D yaaaaaay

NO.

real answer

The server ticks 60 times per second, and it keeps track of what tick it's on by incrementing a fucking integer every tick.

When a client is connected, the server is sending it some state information (tagged with the tick number) every tick. Every time it successfully receives and decodes the state, it says to the server, "look at me i received tick number X arent i special".

The server keeps track of what the last tick number the client reported receiving. Each tick, the server does some fucking magic that calculates exactly the right shit to send that client to make them aware of everything that happened between tick X and right now.

When the client gets an update, it does some more fucking magic to apply the update to the past (numbered) state the server delta'ed against to create and propagate the most recent state.

Stupid fucking questions:

Q: Oh fuck, what if one of the packets goes to fucking china by accident before being forwarded to the client 30 seconds late?

A: Client will throw it in the fucking trash

Q: Shit, what if the client only gets half of the data and the last part of the packet is lost?

A: Client will throw it in the fucking trash

Q: Ahaaaaaaaaa! I got you now you stupid old fuck, what if the client gets a packet delta'ded against tick #12300 but it has already updated to tick #12301???!?1

A: Server tells the client what it knows it knows. Client keeps all state deltas and a buffer of past states until it knows for sure it can safely throw them out. This usually results in the client hanging on to a few deltas and states, most of which are a couple hundred bytes. That lets the client still extract the meaningful information that the server just sent and end up with the very most recent state anyway! fuck yea, science,

Q: Why the hell would you do this, (some other stupid shit) is so much better, I read about it on twatter!

A: Go code it then. This delta bullshit is what shittons of successful games have done. The basic ideas are 100% copied from successful multiplayer games. I'm a completely unoriginal piece of shit.

Q: Oh my god this diff shit is so retardeddddddddddd who would even do this shittttt

A: It was easy as fuck to write and it's a prototype. It worked fine until you fuckers upgraded to mono or AIDS or whatever the fuck disease of a compiler stack you're using now. Of course binary fucking diffs of serialized state information is a stupid way to do it, but that wasn't the point - the point is that the some fucking magic I described above is the least important part of the entire concept. I see more stupid bullshit code on an hourly basis at my current job than you've probably seen in your entire life, but you know what? The simple stupid shit works fine until you're passing a couple billion people's shitty thoughts per hour through it, so fuck it. That binary diff bullshit is fast enough to run the game with thousands of entities, because most of them are never moving or doing jack shit.

Q: Why do you sound so bitter, old man?

A: I ran out of time to make games. It became less of a priority in my life so I open sourced this pile of shitcode you're looking at. I wish I had time to work on it still, because I had a lot of ideas about how to make little balls of shit like this delta encoding better. I ran out of time. If you keep poring over the code and saying, "Oh, this is shit, we should rewrite it and this other thing and all this shit" then your attempt will end up just like SS13 remake number 32 - dead.

make the fucking game, and stop worrying about how pretty the code is or how much you hate this stupid IOC container or why dont we use this shiny new DI poo here?

I'm only 33 actually I'm not old

volundr- avatar May 28 '17 00:05 volundr-

ayyy

volundr- avatar May 28 '17 00:05 volundr-

There is still a use of SYN/ACK (not necessarily TCP) in games, just want to point that out But yes due to the nature of TCP you'd want to use it as little as possible

gbasood avatar May 28 '17 00:05 gbasood

I'm aware of all of this but all I was proposing was a different way to generate deltas. The concept and general system would be the same.

PJB3005 avatar May 28 '17 03:05 PJB3005

I still insist we refactor IoC though. Right now all IoC is doing is cause a headache. Move it to a proper dependency injection system and we're fucking rolling.

PJB3005 avatar May 28 '17 17:05 PJB3005

Specifically, if you've got a bunch of factories and a giant supply web, if one factory goes down most of the rest will be sort of fine. If the highways all go down nobody's fine.

IoC is pretty integral to the entire project structure and while I get what you're saying and I may be delusional it's gonna fucking suck to refactor this later on.

PJB3005 avatar May 28 '17 17:05 PJB3005

IoC was the almost-sole contribution of Nyxation, and it drove a lot of people mad, so kill it with fire. DI is fine, there are probably nice libraries these days to do it.

For the state synchronization, it should be enough to snapshot the world every tick (there are efficient ways to do this, where you only generate a new state struct for a component or entity changes, otherwise reuse the previously generated one - dirty flag). Then use a comparison walker to traverse Scurrent and Slast and generate a concise diff. You could even cull the diff to contain only objects near the player in this stage. Then use DEFLATE or something similarly fast to compress the payload and send it to the client. This of course requires more code than the previous bullshit, but games are made of code and tears, and you have the fingers.

volundr- avatar May 30 '17 13:05 volundr-

My plan right now was to keep IoC, but heavily remove the dependency on it and enforce constructor based DI.

I guess I might look into using Simple Injector or whatever again but the issue is the locking on first use.

PJB3005 avatar May 30 '17 13:05 PJB3005

It's rendering.

Disabling the render code on the main gamescreen, and there's no leak, even though the client is still syncing states and moving around.

PJB3005 avatar Jul 12 '17 21:07 PJB3005

@Silvertorch5 RenderPlayerVisionMap() is the cause of this leak, if you disable bPlayerVision with F7 the leak goes away entirely.

Mind investigating?

PJB3005 avatar Jul 12 '17 21:07 PJB3005