Skip to content

Map server high level explanation

L3nn0x edited this page Oct 8, 2018 · 1 revision

Map server high level explanation

This wiki page was written when I needed to do refactoring on the project to get a better understanding of the whole code base. It is probably not very well detailed and assumes a lot of prior knowledge, but it might come in handy so I'll leave it. I'll try to get back to it at a later stage.

We have a class ThreadManager that holds onto the threads, spawns them at startup and cleans up when killing the process. It also has a separateMap(int id) function that separates a map into its own process.

We have a class Thread that is responsible for managing the map. It holds onto its own version of the class Config, the class Logger and a int mapId. It implements the void run() function that is the main game loop of each map. It holds onto a class Map that implement the logic of the game.

We have a class Map that implements the logic of the game for a particular map. It has an internal map of class CMapClient to socketId that allow us to keep track of the correct CMapClient inside it. It has a void recvPacket(int socketFrom, class Packet) that receives a packet from the network in a thread safe manner. It has a void sendPacket(int socketTo, class Packet) that sends a packet on a socket. It has a void processPackets() that processes the packets and a void update(double dt) that updates the whole map. It holds onto a class PacketQueue, a class SystemManager, a class IdManager, and a class EntitySystem.

  • class EntitySystem : just a vector of class Entity. Contains the data for each entity on the map.
  • class IdManager: manages the internal ids. Has a uint16_t getId() and a void releaseId(uint16_t id).
  • class PacketQueue: holds onto the non processed packets. Is thread safe. Has a void push(int socketFrom, class Packet) and a <int, classPacket> pop().
  • class SystemManager: holds onto the systems. Registers the systems with the packet type for easy packet delivery. Has a void processPacket(class Entity, class Packet), a void update(double dt).

We have different class System that implement the different logic aspects of the game. Each class should aim to be as dependent of each other as possible. Avoid side effects if possible.

Explanation of all the ids

  • socketId: networking related id.
  • class Entity: an entity, can be a character, an NPC, an item and so on
  • internalId: The id that is sent to the client when describing selectable data (characters, NPCs, items and so on).

The class Map has a few maps to link all the different ids:

  • map<socketId, CMapClient>
  • map<CMapClient, socketId>
  • map<socketId, Entity>
  • map<Entity, socketId>
  • map<internalId, Entity>

Last but not least: class Networking this is a thread pool that receives packets from the network and sends it to the class Map.

Clone this wiki locally