backend - How do servers talk to clients? -
i'm trying understand how server-client networking works live multiplayer games.
suppose i'm building real-time multiplayer game fps.
if player a shoots player b, backend server
needs tell player b got shot.
i know how make player a tell backend server
fired shot, send request server
, how 1 make backend server
tell player b
got shot?
does player b have checking backend server
every 0.1 seconds see if happened, or is there more efficient way?
the how-part less important, efficient-way cardinal:
let's jump few months forward. game has several million downloads , server
part has cope huge herd of keen gamers online 24/7/365.
this landscape show core importance of distributed-system design properties , pain low-latency controlled software ( gaming being out of question such case ) put on table.
why latency? game design strives create , maintain realistic user-experience. let's take shooting example - if player a achieves destroy target x, target y , target z within last few moments, player c has shot player a "before" player a started shoot, while due poor ( un-equal ) latency of end-to-end game connection delays, "bullets" c reached server
"late" , got distributed "after" targets x, y , z confirmed destroyed - kill a , respawn x, y, z or leave x, y, z exploded , let a die later, under blue sky without warning or let him/her go ahead if not having been killed player c? - platform gaming-experience become unfair not 1 of players c, x, y, z involved. users hate this. flames follow fast.
latency variability in time ( latency jitter ) problem -- remarkable platforms, il2-sturmovik such case name some, experienced "jumping" in-game objects due type of un-handled latency-related distributed game-engine controls. gaming community realised handicap , black-mod-ers spawned many mods, weakness explited unfair benefit (cheating) players, benefited blocking packets, distributed in-game reality updates , using hack planes become stealth ( un-hit-able attackers' bullets ) , ufo-ed in 3d-scene, due late/very-late arrivals of plane's in-game 3d-coordinates' updates.
if realise, 0.1 sec
not going above 10 fps
of in-game reality updates, while contemporary in-game reality strives have 50 - 80 - 120 fps high-fidelity user-experience, pain face on grows higher.
so, how attack issue?
zeromq
wonderfull framework distributed systems. mastering tool out of question learn lot non-blocking, efficient end-to-end messaging ( updates of kind, controls, signalling ) , learn, how reduce network traffic ( imagine 10 gamers v/s 1.000 gamers v/s 100.000 gamers network-segment loads ).
zeromq
include work-load balancing among multiple server-side machines, increasing back-end capabilities player counts increase - zeromq
supports linear scalability, worth pain during one's learning curve, isn't it?
the server side has typically many duties handle concurrently. over- simplified core-loop of in-game distributed messaging framework event-processing engine looks similar this:
... # setup while true: # ||||||||||||||||||||||| danger-zone vvvvvvvvvvvvvvv fast aninterruptsegmentedcdown = 60 * 100 # 100 * 10[ms] dual-poll-overhead -> aminutecounter #----------------------------------------------------------- while aninterruptsegmentedcdown > 0: # cdown-loop-[aminutecounter ~ 1 minute 6000-loops 10 [ms] each ]-----------100x [sec] 6000x [min] aninterruptsegmentedcdown -= 1 #-------------------------------# sigsport >>> avoid warm-up delay @ end of story, sig_exit needs delivered >>> https://stackoverflow.com/a/33435244/3666197 try: #----------.send()--------# noblock-# .send() amsg, may throw exc ... except: ... finally: ... #-------------------------------# ctrlport if ( 0 != actrlsocket.poll( 0 ) ): # if .poll( 1 ) - fast .poll() ... #-------------------------------# recvport if ( 0 == arecvsocket.poll( 9 ) ): # if !.poll( 9 ) - relax & wait amsg ... 10 [ms] reduced waiting, due need keep sigsport heart-beat 100 ms avoid idle/warmup on socket once sig_exit delivered ... else: # else = [msg] here, # go job done ~~~~~~~ ai-zone <<<< [msg] ... # ~~~~~~~ here # # magics # # happen continue # |||||||||||||||||||||||||||||||||||||||||| danger-zone ^^^^^^^^^^^^^^^ fast
the best next step?
you might interested in more details -- what difference between websockets , zeromq
however for further questions might want see bigger picture on subject >>> more arguments, simple signalling-plane / messaging-plane illustration , direct link must-read book pieter hintjens.