Shared code on console server and graphical client

This topic is 1775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have read a lot about handling network communication. What I have not read is how to structure objects. I am thinking for c++ specifically.

for instance a server and client share the same entity behavior. Some may be disabled if it is considered authoritative or not. Which the client could be authoritative in single player or when hosting a game, but not when connecting to another server. Also some entities may be predictive like player where it will still run the authorative code. The behavior part is easy enough to account for.

An entity will of course contain a graphical part and a behavior part.

A console server will only need the behavior part.

I can only think of 2 ways.

Have preprocessor  statements in every entity to remove all graphics code. Which is a bit annoying as I will need to access model animations and it's transformations.

The other is have completely separate server and client code and if they run in the same app have the server and client code run.  Problem being is there will be some duplication of information and also any predictive code will need to be written in the client object as well as the server. I suppose it could inherit the code from the server side.

So I was just wondering what methods you have seen used and which s the best and why.

I am pretty tired so sorry if this seems a bit silly. I was just wondering if there is a better way.

Share on other sites

Another way to do it is to always have a server process which is authoritative, even when running a single-player game locally. You can even continue communicating over sockets between the two, by using the local loopback address. IIRC, quake worked this way. You also have plenty of other communication channels available locally -- IPC, named pipes, or just adding a layer of abstraction between the "client" and the "server"--one implementation could implement the logic locally, and other could just be a conduit to an actual server across the network.

This isn't a direct answer to your question, but would allow you to re-use your code without resorting to conditionally-enabling or disabling code statements, and it should make using the authoritative code in two different apps (the client and stand-alone server) easier (via static library or DLL) easier if you go that route.

Share on other sites

This is easy to solve with a little bit of composition. I'll demonstrate two options in a single source snippet:

// SharedLogic.h
class Berzerker
{
public:
Matrix4x4 Transform;
unsigned Health;

private:
bool IsAngry;

void MakeAngry()
{
IsAngry = true;
Health += 10;
}

friend class ServerLogic;

public:
bool CheckAngry() const
{
return IsAngry;
}
};

// Server.cpp
#include "SharedLogic.h"

class ServerLogic
{
public:
void Run()
{

Berzerker b;

while(ServerActive())
{
if(PlayerShotBerzerker(b))
b.MakeAngry();

// etc.
}

}
};

int main()
{
ServerLogic sl;
sl.Run();
}

// Client.cpp
#include "SharedLogic.h"

template<typename T>
class Renderable
{
private:
T SharedLogicObject;

public:
T& GetObject() { return SharedLogicObject; }
const T& GetObject() const  { return SharedLogicObject; }

public:
void Draw(const Model& model)
{
GraphicsApi::DrawModelWithTransform(model, SharedLogicObject.Transform);
}
};

class ServerLogic
{
public:
static void Update(Berzerker& b)
{
if(NetworkSaysBerzerkerIsAngry(b))
b.MakeAngry();
}
};

int main()
{
Model happyBerzerker("some.mesh");
Model angryBerzerker("other.mesh");

Renderable<Berzerker> rb;

while(ClientActive())
{
if(PlayerFiredWeapon())
NotifyServer();

CollectServerResponse();

ServerLogic::Update(rb);

if(rb.GetObject().CheckAngry())
rb.Draw(angryBerzerker);
else
rb.Draw(happyBerzerker);
}
}

Option One

Your shared logic lives in a file that both client and server can access. Stats which are visible to both can be public; stats which need to be purely server-authoritative can be private with access controlled via friendship. Your ServerLogic class depends on which code you build; on the server, it's the actual authoritative logic, whereas on the client, it simply polls the network for updated state.

Option Two

The way I deal with separating the rendering shows a similar but alternative form of composition. In this case, you build the special logic in terms of the different pieces the client or server needs to work with. In this example, the client has a Renderable<> template which holds the shared logic object as well as enables drawing via a model. The template isn't strictly necessary, but it shows how you can apply broad swaths of rendering logic to all of your shared "entity" code with a simple construct.

Basically neither option is inherently superior, and as above, a blend may work nicely as well. But in general, the idea is you should build your code in small, reusable chunks, and then use (or reuse) them where appropriate on client and server. No need for #ifdefs or other evilness :-)

Share on other sites

Ravyne That method is the second method I mentioned. I suppose writing it that way is easiest in that it always works the same as server client, or the one. Though will double up on memory and resources that could be shared. Like models and there animations

ApochPiQ I do like that the only problem is animation needs to be available both for client and server. The server may need it for collision detection stuff.

I suppose the completely separate client & server model might be the simplest method and where prediction is needed just inherit for the server. Even if there is some duplication.

Share on other sites
How is that a problem? Just put your animation system in the shared section, done :-)

Share on other sites
If the "server code" and "client code" within the game process shares the same asset manager object, then you won't be duplicating the memory use. If you split "server" and "client" into separate processes even for single player, then that's harder to do, although with the magic of memory mapped read-only files, you could actually still make that happen (!)