Blog

The Broadcaster: from one 56,000-line switch to a class every game shares

15 November 2026

Every feature of an online game has two halves that talk: the party window on the player's screen and the party rules on the server, the bank's slots and the bank's database rows. Somebody has to decide which code gets which packet. In Helbreath that somebody is called the Broadcaster today. It did not exist at the start. This is how it came to be, from the server that was handed down to the one Helbreath Empire runs now.

1. One class, one switch

The original Helbreath server is one class, CGame, and most of it lives in a single Game.cpp of 56,100 lines. Every packet begins with a 32-bit message id, and one switch sends each id to a member function of CGame:

dwpMsgID = (DWORD *)(pData + DEF_INDEX4_MSGID);
switch (*dwpMsgID) {

case MSGID_REQUEST_RETRIEVEITEM:
    RequestRetrieveItemHandler(iClientH, pData);
    break;

case MSGID_REQUEST_TELEPORT:
    RequestTeleportHandler(iClientH, pData);
    break;

case MSGID_REQUEST_CREATENEWGUILD:
    RequestCreateNewGuildHandler(iClientH, pData, dwMsgSize);
    break;

case MSGID_REQUEST_DISBANDGUILD:
    RequestDisbandGuildHandler(iClientH, pData, dwMsgSize);
    break;
// ... and so on, for every message the game has

Every handler gets the same three things: the player's slot number, a raw pointer to the bytes, and sometimes their length. What the bytes mean is up to each handler to find out by itself.

Parties came later (the code says // New 07/05/2004) and got a single id, MSGID_PARTYOPERATION. What kind of party operation it was went in a 16-bit number after it, typed by hand. This is how the server asks for a new party:

dwp = (DWORD *)cp;
*dwp = MSGID_PARTYOPERATION;
cp += 4;
wp = (WORD*)cp;
*wp = 1; // 1, request
cp += 2;

wp = (WORD *)cp;
*wp = iClientH;
cp += 2;

memcpy(cp, m_pClientList[iClientH]->m_cCharName, 10);
cp += 10;

SendMsgToGateServer(MSGID_PARTYOPERATION, iClientH, cData);

The 1 means "request", 4 elsewhere means "leaving", and the comment next to the number is the only place that says so. The sending function then picks the size from the id, iSendMsg(pData, 50), whatever was written. The idea of a family of operations under one id is already here, though. It just has no shape yet.

2. 2017: the Broadcaster is born

Helbreath Empire's network was rewritten in August 2017, and the commit that did it is called "Packet, Broadcaster and party rewrite". It gave that shape a name. A Broadcaster is a class that owns one family of packets. On the wire every packet of the family reads <MainOp><Op><fields...>: the main operation says which family it belongs to (Party, Guild, Bank, Minimap), and the operation, an enum class of the family's own, says what to do:

template<typename Op>
class Broadcaster : packet::Impl
{
public:
    Broadcaster(const MainOp main_op) : packet::Impl(main_op) {}

    bool FetchOperation(InPacket * in)
    {
        _op = in->ReadAs<Op>();
        if (in->IsOverflow())
            return false;

        return true;
    }

    template<typename ... Args>
    OutPacketPtr Pkt(Op op, Args && ... args)
    {
        return OutPacket::Create(_main_op, op, forward<Args>(args)...);
    }

    template <typename ... Args>
    void Send(Client * client, Op op, Args &&...args)
    {
        SendPacket(client, Pkt(op, forward<Args>(args)...));
    }

private:
    Op _op;
};

The magic 1 became PartyMsg::Join. The main id went in front by itself, because the broadcaster knows which family it is. The party code moved out of the giant class into a PartyMgr of its own, which derives from Broadcaster<PartyMsg>, and the game's main switch only had to hand it the packet:

case Msg::Party:
    g_ptmgr->NetOperation(p, in);
    break;

Inside, the party read its own operation and switched on it:

void PartyMgr::NetOperation(Client * p, InPacket * in)
{
    if (!FetchOperation(in))
        return;

    switch (GetOperation())
    {
    case Op::Join:
        NetReq_Join(p, in);
        break;

    case Op::Kick:
        NetReq_Kick(p, in);
        break;
    // ...
    }
}

One switch over every message in the game became a short switch per feature, each feature in its own files, sending its own packets with one call.

3. The next day: the base calls you

Every broadcaster started its NetOperation the same way: fetch the operation, give up if the packet was too short, then switch. One day later, in "Broadcaster and minimap fixes", that part moved into the base. The base now does the fetching and calls a virtual function, and a feature only writes the switch:

void NetOperation(InOperation & opt)
{
    // the operation fetched, the player and packet kept ...
    ProcessOperation();
}

virtual void ProcessOperation() = 0;

This is the template method pattern, and it is the reason the name stuck. A broadcaster is not something you call to send; it is something the network calls when its family's packet arrives. In March 2020 it left the shared global.h for headers of its own, Broadcaster.h in the client and in the server.

4. Less to write in every handler

Over the following years the boilerplate around it kept shrinking. What the dispatch hands a broadcaster became one small struct, the player, its network peer and the packet, which the server's broadcaster inherits, so a handler finds them as members:

template <class Player, class Peer>
struct InOperation
{
    Player * _p = nullptr;
    Peer * _peer = nullptr;
    lib::IPkt * _in = nullptr;
};

Reads became typed tuples (the story of the previous post), and the switch itself became one line per group of operations, through small macros that write case Op::Store: NetOp_Store(); break; for each name. This is the whole of Helbreath Empire's bank dispatch today:

void Bank::ProcessOperation()
{
    // Howard on the map (hb_lib npc/service_map.h, both servers)
    if (!_p->AtServiceNpc(hbnpc::service::Service::Warehouse))
        return;

    switch (_op)
    {
    // DEPRECATED: drained no-op so a stray legacy packet doesn't desync.
    _OP(UpgradePurchase);
    _OPS((), Store, Retrieve, Move, Section, DefaultSection);
    }
}

void Bank::NetOp_Store()
{
    auto [idx, sect_idx, tile_idx, cnt] = As<ItemIdx, SectionIdx, bank::TileIdx, Cnt>();
    // ...
}

A rule that holds for the whole family, here "you must be standing at the warehouse keeper", is written once, above the switch, instead of in every handler. The original server repeated checks like that in each of its Request...Handler functions.

NetOperation also answers which operation it ran, a single byte, so the game's main dispatch can tell the packet reports what came in. Every operation enum is checked at compile time to fit in that byte:

static_assert(lib::PktOperationFitsByte<Op>(), "an Op's values fit a byte, below kNoOperation");

5. 2026: one Broadcaster for every game

For years the Broadcaster belonged to Helbreath Empire alone. Helbreath Millennium, the edition players log into every day, kept its own ways. In October 2026 it was split into three layers, so that both games, and anything else I build, run the same one:

template <class N>
concept ServerNet = lib::PktOperation<typename N::MainOp> &&
    requires(lib::OPkt & out, typename N::Player * const p, typename N::Peer * const peer)
    {
        { N::kSizing } -> std::convertible_to<lib::PktSizing>;
        N::Send(p, out);
        N::Send(peer, out);
        { N::PeerIP(peer) } -> std::convertible_to<std::string>;
    };

Helbreath Empire's whole server side of it is now this:

struct ServerNet : broadcaster::ServerNetBase<::Player, ::MainOp>
{
    static constexpr auto kSizing = lib::PktSizing::Exact;

    static void Send(Player * const p, OPkt & out)
    {
        global::SendPacket(p, out);
    }

    static void Send(Peer * const peer, OPkt & out)
    {
        global::SendPacket(peer, out);
    }
};

template <typename Op = BroadcasterDummyOp>
struct Broadcaster : broadcaster::Server<ServerNet, Op>
{
    using broadcaster::Server<ServerNet, Op>::Server;
};

The client's side is the same size, with one thing the original never needed: the client talks to more than one server (the game, the login, the map editor), so its hooks name its connections, and a broadcaster says which one its packets go on.

Because Millennium is live, it went first: every broadcaster packet both games share is played end to end with two test clients by an automated routine, and Millennium runs the shared code in front of real players. Empire, which is not open yet, will open on code that has already been proven.

In the end: asking to join a party, then and now

The original server, asking for a party:

dwp = (DWORD *)cp;
*dwp = MSGID_PARTYOPERATION;
cp += 4;
wp = (WORD*)cp;
*wp = 1; // 1, request
cp += 2;
// ... the slot, the name, then
SendMsgToGateServer(MSGID_PARTYOPERATION, iClientH, cData);

Helbreath Empire's client today:

Send(Op::Join, a);

And on the server, the party's broadcaster receives it already sorted. The family, the operation, the player and the packet are handed to it, and the request is read as one typed struct:

case Op::Join:
    return RequestJoin(p, in->ReadAs<JoinAsk>()._name);

The idea was in the original from 2004: a family of operations under one message id. It took a name in 2017, learned to call its features instead of being called, lost its boilerplate one piece at a time, and today it is one piece of code that both games share.

The projects behind this post

Helbreath Empire

My most important project technically: an online MMORPG whose client and server I rewrote into my own engine. Millennium runs live, Empire is the next generation: OpenGL and GLSL, TCP/UDP networking off the frame, multi-threading, SSE2, an in-house UI, sprite and map editors, in-game Twitch streaming, VoIP, screen recording and more.

C++23, OpenGL / GLSL, ENet (UDP), MySQL, SFML, ImGui, FFmpeg, kazilib

kazilib

My own C++ library, under every game on this page: packets and networking on ENet, threads and parallel jobs, time, an SFML 3 and ImGui layer, screen recording, WebAssembly builds for the browser, the Testerino test bridge and one FASTBuild runner. Helbreath Millennium, Helbreath Empire and my prototypes share it, so a fix in one reaches them all.

C++23, ENet, SFML 3, Dear ImGui, FFmpeg, WebAssembly, FASTBuild