Basics Of NetWorking
Game networking generally follows two paths depending on the platform and the "twitch" requirements of the gameplay.
1. WebSockets
- Best for: Web-based games, real-time UI data, and persistent chatrooms.
- The Catch: WebSockets run on top of TCP. They are great for reliability and getting through firewalls (since they use standard HTTP ports), but the overhead makes them less ideal for high-speed action.
2. UDP & TCP
- TCP (Transmission Control Protocol): The "Safe" path. It handles packet loss and ordering for you, but it’s slow. In fast games, we only use this for things that must arrive exactly as sent, like chat messages, inventory transactions, or login data.
- UDP (User Datagram Protocol): The "Fast" path. Used by shooters like CS:GO and Call of Duty. It’s "fire and forget"—it doesn't care if a packet gets lost or arrives out of order. This makes it incredibly fast but dangerous to use for critical data.
What is Reliable UDP (RUDP)?
Most modern games use what you might call "Protected" or Reliable UDP. This is a custom layer built on top of raw UDP that selectively adds the safety of TCP back in, without the massive latency penalties.
How do I learn it?
Since RUDP is just a logic layer on top of a UDP socket, you must start with a solid foundation in raw UDP.
Key concepts to master:
- Sequence Numbers: Labeling every packet so the receiver knows the order.
- ACKs (Acknowledgments): The receiver sends a small packet back saying "I got packet #42."
- Data Validation: Using Checksums to ensure the data wasn't corrupted during the "fire and forget" phase.
- Packet Re-sending: If the sender doesn't get an ACK for a critical packet (like "I fired my gun"), it sends it again.
Recommendation: Don't Reinvent the Wheel
Building a robust RUDP layer from scratch is an "Engine-Level" task. If you want to see how it's done or build on top of a proven system, look at these low-level libraries:
- ENet: A classic, lightweight C library that provides a solid RUDP layer. It’s a great place to start reading source code to see how "protection" is actually implemented.
- GameNetworkingSockets: Valve's internal (Steam) networking layer. It's open-source and handles everything from reliability to encryption.
- Lidgren.Network: The gold standard for C#/.NET developers looking for RUDP.
Engine-Specific Implementations
If you aren't building your own low-level socket handler with ENet, you’ll likely be using the built-in systems for Unreal or Unity. Both engines use UDP by default but provide a "Reliable" layer for critical data.
1. Unreal Engine (C++)
Unreal’s networking is built into the core of the engine via the NetDriver. It uses a "Bunch" system to handle reliability.
- The System: Replication and RPCs.
- Reliable vs. Unreliable: When you define an RPC in C++, you specify its reliability:
UFUNCTION(Server, Reliable) // Acts like TCP (Guaranteed delivery/order)
void Server_SubmitScore(int32 Score);
UFUNCTION(NetMulticast, Unreliable) // Raw UDP (Fast, might vanish)
void Multicast_PlayExplosionEffect();
- The "Why": Unreal uses Bit-level serialization and a unique "Channel" system. Each Actor has its own channel. If a "Reliable Bunch" is lost, the channel pauses until that specific data is re-sent, but other channels (other actors) can keep moving. This prevents a single lost packet from lagging the entire game state.
2. Unity (C#)
Unity’s modern networking revolves around the Unity Transport Package (UTP) and Netcode for GameObjects.
-
The System: Unity Transport (UTP).
-
The Pipeline: Unity uses a concept called Pipelines. You don't just "send a packet"; you send it through a pipeline that adds the "Protection" layer.
- ReliableSequencedPipelineStage: This is Unity's RUDP. It adds sequence numbers and ACKs.
- UnreliableSequencedPipelineStage: UDP speed, but ensures older packets don't overwrite newer data if they arrive out of order.
-
The "Why": Unity Transport is Data-Oriented (DOTS compatible). It is designed to be incredibly lean and allows you to create custom "Stages" (like a stage for encryption or a stage for compression) to sit on top of your UDP socket.
Summary Table: Networking Choice
| Feature | Unreal Engine | Unity (UTP) |
|---|---|---|
| Language | C++ | C# |
| Default Protocol | UDP (with custom RUDP layer) | UDP (via Unity Transport) |
| Reliability Method | Reliable RPCs / Property Replication | Reliable Pipelines |
| Best For | AAA shooters, massive worlds | Mobile, cross-platform, modularity |
| Low-Level Access | Direct Socket access via ISocketSubsystem |
Low-level NetworkDriver |
References & Further Reading
- Unreal: Netcode Deep Dive by Cyrex - An excellent look at how Unreal handles bunches and channels.
- Unity: Unity Transport Documentation - Official breakdown of the transport layer and pipelines.
- General: Gaffer on Games- The "Bible" for anyone wanting to build their own RUDP layer from scratch.