Game Networking By Dummy - Hopefully Part 1 of N where N > 1


This is a writeup trying to demystify how "real-world" game networking is done, by someone who has very little experience in this part of programming.  I happen to come across Open RuneScape Classic few days ago, as it's one of the most successful MMO, I believe it's still a good "real-world" candidate despite the age.  The simplest way to start is to write some python script and poke the server (deployed locally, of course).  Before we jump into `import socket`, here's a rough idea of how the RSC client and the server communicates:


1. Message is packed into a packet/frame/whatever you call, simply described in C:

```
struct {
    int opCode;
    int length;
    void* data;
}
```

where the opCode is something both sides agree on, and the length denotes how big the data/payload is.

2. short - 1 byte, int - 2 bytes, long - 4 bytes

3. string: terminates with newline


With these, we have established a simple and effective way of communication.  As I'm working on the client side, naturally this writeup will focus on the client side.  To fetch a single message, we can simply do: `socket.recv(2)` to get the opCode and length, then do `socket.recv(length)` (usually the length is small enough to not need a buffer) to get the payload.  Finally, process the payload according to the opCode.  Wrap this with a while loop (and probably a network thread) and you get a nice working network foundation.  Forget TLS for now (or not, your choice), I'm sure you can already build something amazing with these.

So far, I know that the client will ask for a Server Configuration (name, welcome string, max level, etc.) and the server will close this connection.  After that, the client will start a connection by sending the user credentials over, and then the server will keep sending packets (probably to synchronize the game state) until timeout or logout occurs.  The client will send a heartbeat packet periodically if nothing has been done.  That's all I've "deciphered" for now.  

I am certainly looking forward to the next part which I will probably look into what the server actively sends and how to synchronize the game state.


10-11-2020 1747