A simple library to use in your networked projects.
- Support for TCP and UDP
- Support for IPv4
- Safe and easy to use
- Cross-platform
- Tested with GCC and Visual Studio
- Tested with Valgrind
The library has a simple API, which is divided into two parts: the Client
and the Server
.
They both have pretty much the same API, but some methods are exclusive.
The client and server are designed to exchange Packets.
A Packet just a simple structure containing base information and a payload. The payload can be any type of data.
Since the data transferred over the network needs to be converted to a byte array, the library provides a SerializerTrait
trait to do that.
Where the library deliver all its power is in the SerializerTrait
trait. Indeed, this trait can be specialized to serialize user defined types.
As long as you provide a serializer/deserializer for your type, you can send it over the network.
The SessionStore
is a simple class that stores the sessions of the clients connected to the server.
This SessionStore
can be shared across a UDP and a TCP server. This allows the clients to keep their session between the two servers.
To do so, it generates an authentication token for each client. This token is then sent to the client and is used to identify the client on the other server and safely attribute the same session.
The OpId
is a simple identifier for a packet. It is used to identify the packet type and call the appropriate callback after casting the received byte stream into a header + payload form.
A DTO type can have multiple OpId
associated with it. This allows you to have multiple callbacks for the same type.
Example:
struct SampleDto {
static constexpr OpId opIdXPos = 0;
static constexpr OpId opIdYPos = 1;
int value;
};
// Registering a callback for the X position
client.registerReceiveHandler<SampleDto>(SampleDto::opIdXPos, [](const PacketHeader &header, const SampleDto &payload) {
std::cout << "Client received X position: " << payload.value << std::endl;
return true;
});
client.registerReceiveHandler<SampleDto>(SampleDto::opIdYPos, [](const PacketHeader &header, const SampleDto &payload) {
std::cout << "Client received Y position: " << payload.value << std::endl;
return true;
});
There is two ways to add the library to your project:
- Add the library as a submodule
- Use the CMake FetchContent module
git submodule add https://github.com/PolymorphEngine/NetworkLibrary.git
You will then need to compile it or, if you are using CMake, add it to your project.
add_subdirectory(NetworkLibrary)
You will need to download the FinderPolymorphNetworkLibrary.cmake (also available in releases)
include(path/to/FindPolymorphNetworkLibrary.cmake)
Then, you will have to reload the CMake cache and the library will be downloaded and the headers will be available.
See the TCP and UDP to see how to use the library in your project.
See the DTO page to see how to define your DTOs for your protocol.