ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Game network
programming
PotHix (Willian Molinari)
Diego Souza (marciano)
Why?
Game network programming
Skeleton Jigsaw: http://plaev.me/skeleton-jigsaw

              Shameless self promotion
To have fun, bro!
Agenda
¡ö Synchronous RTS
¡ö Peer to peer
¡ö Client / server
¡ö UDP
¡ö Quake strategy
¡ö TCP
¡ö Web and HTML5
Synchronous RTS
RTS's and Supreme commander
Peer to peer
Everything in sync
Two gameloops!
Latency
My life for the horde!
Player1   Player2   Player3


 tick 0ms



tick 100ms



tick 200ms



tick 300ms



tick 400ms
Player1   Player2   Player3


 tick 0ms



tick 100ms



tick 200ms



tick 300ms



tick 400ms
Player1   Player2   Player3


 tick 0ms



tick 100ms



tick 200ms



tick 300ms



tick 400ms
Player1   Player2   Player3


 tick 0ms



tick 100ms



tick 200ms



tick 300ms



tick 400ms
butterfly effect
So...
Player1   Player2   Player3


 tick 0ms



tick 100ms



tick 200ms



tick 300ms



tick 400ms
Player1        Player2   Player3


 tick 0ms



tick 100ms
                   UI OK!


tick 200ms



tick 300ms



tick 400ms
Desync
Age of empires
1500 archers with a 28.8 modem [1]
Client / Server
Authoritative server
Client   Server




                  pos (0,0)
Client   Server




                              pos (0,0)

pos (0,0)
Client   Server




                              pos (0,0)

pos (0,0)


move


pos (1,0)
Client   Server




                              pos (0,0)

pos (0,0)


move


pos (1,0)


                              pos (1,0)
Smartass
Client   Server




                  pos (0,0)
Client   Server




                              pos (0,0)

pos (0,0)
Client   Server




                                  pos (0,0)

pos (0,0)


move


pos (100,200)
Client   Server




                                      pos (0,0)

pos (0,0)


move


pos (100,200)


                                  pos (100,200)
Game network programming
Client   Server




                  pos (0,0)
Client   Server




                              pos (0,0)

pos (0,0)
Client   Server




                               pos (0,0)

pos (0,0)



move right


                               pos (1,0)
Client   Server




                               pos (0,0)

pos (0,0)



move right


                               pos (1,0)



 pos (1,0)
Victory!
Client side prediction
Client   Server




                  pos (0,0)
Client   Server




                              pos (0,0)

pos (0,0)
Client   Server




                               pos (0,0)

pos (0,0)



move right


                               pos (1,0)
Client   Server




                               pos (0,0)

pos (0,0)



move right


                               pos (1,0)



 pos (1,0)
Client   Server




                                  pos (0,0)

   pos (0,0)



   move right



Latency                           pos (1,0)



    pos (1,0)
Client   Server




                                           pos (0,0)

      pos (0,0)



       move right



Animation                                  pos (1,0)



        pos (1,0)




            Predicting
Client   Server




                                           pos (0,0)

      pos (0,0)



       move right



Animation                                  pos (1,0)



        pos (1,0)




            Predicting
Client   Server




                                      pos (0,0)

      pos (0,0)



       move right



Animation                             pos (2,0)



        pos (1,0)
Client   Server




                                      pos (0,0)

       pos (0,0)



       move right



Animation                             pos (2,0)



        pos (1,0)


Correction

        pos (2,0)
Quake
https://github.com/id-Software
UDP
datagrams, unreliable, unordered
rcv_buf >= snd_buf
All receive operations return only one packet
          watch out for MSG_TRUNC
auto ip_mtu_discover
 enabled by default
  EMSGSIZE signals packet too big
65k max theoretical packet
 limit (headers included)
    rfc defines the size header to 16bits
however, 1472 is likely the
max you may get, 576 to be
          sure
         MTU - headers
remember the TOS
throughput, reliability, lowdelay (etc.)
Quake world
https://github.com/id-Software/Quake
All credits to Fabien Sanglard: http://fabiensanglard.net/quakeSource/quakeSourceNetWork.php
        Code: https://github.com/id-Software/Quake/blob/master/QW/client/net_chan.c
Quake III arena
https://github.com/id-Software/Quake-III-Arena
Snapshots based
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
All credits to Fabien Sanglard: http://fabiensanglard.net/quake3/network.php
Open source code!
https://github.com/id-Software/Quake-III-Arena/blob/master/code/qcommon/msg.c
TCP
Streaming, reliable, connection oriented
TCP tuning
Long story short: use auto-tune
You probably just need to tune the maximum values
                   (system wide)
High-performance extensions
        refer to rfc1323
Bandwidth-delay product
data link's capacity (in bits per second) and its end-to-
                end delay (in seconds).
10Mbs x 1ms = 1.22 KB
10Mbs x 200ms = 244 KB
    rfc1323: huge buffers
CORK
don't send partial frames
NODELAY
disable nagle's algorithm
socket options
https://gist.github.com/4036204
benchmarking
Game network programming
Game network programming
BBG* and HTML5
  *Browser based games
The same thing...
     ...but not...
No TCP
   No UDP
No peer to peer
Chrome support!?
http://developer.chrome.com/apps/app_network.html
HTTP
Websockets
Mozilla multiplayer game: http://browserquest.mozilla.org
Blackberry
                                browser
           Chrome
                                             Opera
Mobile safari


                    Browsers
    IE
                                            Android browser
                     Firefox

  Opera mini
                                              Safari
Caniuse.com: websockets marketshare
Socket.io
Fallbacks all over the place
Bandwidth
We're back again
References
¡ö [1] Age of empires paper
¡ö Synchronous RTS engines
¡ö Client / server tricks
¡ö Quake code reviews
¡ö Improving bandwidth for websockets
¡ö Unreal networking architecture
¡ö http://tools.ietf.org/html/rfc1323
References
¡ö http://linux.die.net/man/7/udp
¡ö http://linux.die.net/man/7/ip
¡ö http://linux.die.net/man/7/socket
¡ö http://en.wikipedia.org/wiki/Nagle's_algorithm
¡ö http://en.wikipedia.org/wiki/Bandwidth-
  delay_product
References
¡ö http://tools.ietf.org/html/rfc1046
¡ö http://linux.die.net/man/7/tcp
Game over

More Related Content

Game network programming