Making Networked Games
Making Networked Games
Introduction
XNA Framework 1.0 had no networking support
Use other solutions (System.NET) on Windows No network access at all on Xbox
2.0 adds a new high level networking API Game oriented Built on Xbox LIVE and Games for Windows - LIVE Up to 31 players per session
Coordinating lobby <-> gameplay transitions Reliable UDP protocol Voice just works Host migration (partly: see later) Network latency and packet loss simulation
Peer-to-peer architecture
Peer-to-peer architecture
Peer-to-peer architecture
Peer-to-peer
Uses less network bandwidth Workload is distributed more evenly across machines No lag for local player movement Easier to support host migration
Bandwidth
Bandwidth
Bandwidth
Bandwidth
How much is available?
Assume 64 kilobits (8 kilobytes) per second Some players will have more Often more downstream than upstream
If you send a single bool to one other player, 60 times per second, this requires
60 x 1 byte of payload data = 60 bytes 60 x 50 bytes of packet header = 3000 bytes Bandwidth usage: 3 kilobytes per second 98% overhead
Example
8 players (each sending to 7 others) Transmit 10 times per second 64 bytes of game data per packet Bandwidth usage: (64 + 50) * 7 * 10 = 7.8 kilobytes per second 44% overhead
Voice bandwidth
Voice data is ~500 bytes per second By default, all players can talk to all others In a 16 player game, talking to all 15 other players
500 * 15 = 7.3 kilobytes per second Yikes
LocalNetworkGamer.EnableSendVoice
Only talk to players on your team Only talk to people near you in the world But avoid changing this too often!
Compression
Generalized compression algorithms are not much use
Packets are typically too small to provide a meaningful data window
Prioritize data
Send less important things less often Update further away objects less often Dont bother synchronizing objects that are behind you
Compression: quantization
float rotation; // in radians packetWriter.Write(rotation);
Compression: bitfields
bool isAlive, isRespawning, isFiring, hasPowerup; packetWriter.Write(isAlive); packetWriter.Write(isRespawning); packetWriter.Write(isFiring); packetWriter.Write(hasPowerup);
byte bitfield = 0; if if if if (isAlive) (isRespawning) (isFiring) (hasPowerup) bitfield bitfield bitfield bitfield |= |= |= |= 1; 2; 4; 8;
packetWriter.Write(bitfield);
packetWriter.Write(angle); packetWriter.Write(speed);
HalfSingle packedAngle = new HalfSingle(angle); HalfSingle packedSpeed = new HalfSingle(speed); packetWriter.Write(packedAngle.PackedValue); packetWriter.Write(packedSpeed.PackedValue);
int seed = (int)Stopwatch.GetTimestamp(); packetWriter.Write(seed); Random random = new Random(seed); foreach (Star star in starField) { star.Position = new Vector2((float)random.NextDouble(), (float)random.NextDouble()); }
Latency
Latency
Speed of light = 186282 miles per second Nothing can travel faster than this Some distances
Seattle to Vancouver: 141 miles = 0.8 milliseconds Seattle to New York: 2413 miles = 13 milliseconds Seattle to England: 4799 miles = 26 milliseconds
Latency
Its actually worse than that Network data does not travel through a vacuum
Speed of light in fiber or copper slows to 60%
Latency
So how bad can it get?
Xbox games are expected to work with latencies up to 200 milliseconds
Packet Loss
Packet loss
Traditionally, games had to worry about
Packets never being delivered Packets being delivered in the wrong order Corrupted packet data Packets being tampered with by cheaters Accidentally reading packets from some other program Packet data being examined in transit
Packet loss
Traditionally, games had to worry about
Packets never being delivered - reliable UDP (optional) Packets being delivered in the wrong order - in-order delivery (optional) Corrupted packet data - secure packets Packets being tampered with by cheaters - secure packets Accidentally reading packets from some other program - secure packets Packet data being examined in transit - secure packets
Packet loss
To avoid packets being delivered in the wrong order
SendDataOptions.InOrder This is very cheap Once a later packet has been received, earlier ones are simply discarded
Recommendation
Use SendDataOptions.InOrder for most game data
Packet loss
How bad can it get?
Xbox games are expected to work with packet loss up to 10%
THE END
QUESTIONS?