Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object-oriented networking

I've written a number of networking systems and have a good idea of how networking works. However I always end up having a packet receive function which is a giant switch statement. This is beginning to get to me. I'd far rather a nice elegant object-oriented way to handle receiving packets but every time I try to come up with a good solution I always end up coming up short.

For example lets say you have a network server. It is simply waiting there for responses. A packet comes in and the server needs to validate the packet and then it needs to decide how to handle it.

At the moment I have been doing this by switching on the packet id in the header and then having a huge bunch of function calls that handle each packet type. With complicated networking systems this results in a monolithic switch statement and I really don't like handling it this way. One way I've considered is to use a map of handler classes. I can then pass the packet to the relevant class and handle the incoming data. The problem I have with this is that I need some way to "register" each packet handler with the map. This means, generally, I need to create a static copy of the class and then in the constructor register it with the central packet handler. While this works it really seems like an inelegant and fiddly way of handling it.

Edit: Equally it would be ideal to have a nice system that works both ways. ie a class structure that easily handles sending the same packet types as receiving them (through different functions obviously).

Can anyone point me towards a better way to handle incoming packets? Links and useful information are much appreciated!

Apologies if I haven't described my problem well as my inability to describe it well is also the reason I've never managed to come up with a solution.

like image 768
Goz Avatar asked Feb 08 '11 19:02

Goz


People also ask

What do we mean by object oriented?

An object-oriented system allows the user to focus completely on tasks rather than tools. Examples of object-oriented programming languages include C++ and Smalltalk.

What is an object network?

Network Objects are defined segments of your network that you can reuse throughout multiple responses. Use the Network Objects feature to centralize data entry so that you only need to change the network object instead of each instance of the data.


2 Answers

About the way to handle the packet type: for me the map is the best. However I'd use a plain array (or a vector) instead of a map. It would make access time constant if you enumerate your packet types sequentially from 0.

As to the class structure. There are libraries that already do this job: Available Game network protocol definition languages and code generation. E.g. Google's Protocol Buffer seems to be promising. It generates a storage class with getters, setters, serialization and deserialization routines for every message in the protocol description. The protocol description language looks more or less rich.

like image 56
ssmir Avatar answered Sep 22 '22 15:09

ssmir


A map of handler instances is pretty much the best way to handle it. Nothing inelegant about it.

like image 22
Blrfl Avatar answered Sep 23 '22 15:09

Blrfl