Skip to content

A C++ network library using TCP sockets, using ASIO and Protobuf.

License

Notifications You must be signed in to change notification settings

mwthinker/CppProtoNetwork

Repository files navigation

net::CppProtoNetwork CI build

About

A network library using TCP sockets. Handles server and clients. Uses standalone Asio (no boost) library. Data is serialized using Protobuf.

It uses C++20 and the C++ standard library.

Requirements

vcpkg

Either define CMAKE_TOOLCHAIN_FILE in cmake to use the one provided by vcpkg or define a enviromental variable VCPKG_ROOT to the vcpkg install directory. Or create a CMakeUserPresets.json and use that one.

Install following packages:

# Inside project dir (assuming unix host)
mkdir build
cmake --preset=unix ..

Example code

Proto file

syntax = "proto3";

package message;

message Wrapper {
    string text = 1;
}

Server code

#include <net/server.h>
#include <message.pb.h> // Generated code by protobuf.

net::IoContext ioContext;

// Some code ...

auto server = Server::create(ioContext);
// Must setup connections handlers before server is connected.
// Data is called from the server's internal thread, data Therefore need to be
// protected.
server->setConnectHandler([](RemoteClientPtr remoteClientPtr) {
    std::cout << "New Connection\n";

    remoteClientPtr->setReceiveHandler<message::Wrapper>(
        [](const message::Wrapper& wrapper, std::error_code ec) {
        
        std::cout << wrapper.text() << "\n";
    });

    remoteClientPtr->setDisconnectHandler([](std::error_code ec) {
        std::cout << "Disconnected\n";
    });
});

// Some code ...

// Start listening to incomming connections on port 5012
server->connect(5012);

// Some code ...

// Set some data
message::Wrapper wrapper;
wrapper.set_text("Hellor world");

// Send to all conencted clients.
server->sendToAll(wrapper);

// Start running, blocks everything.
ioContext.run();

// ...

// Some code to send data to a specific client
void sendToSpecificClient(RemoteClientPtr remoteClientPtr) {
    message::Wrapper wrapper;
    wrapper.set_text("Hello remote client!");

    remoteClientPtr->send(wrapper);
}

Client code

#include <net/client.h>
#include <message.pb.h> // Generated code by protobuf.

// Some code ...

net::IoContext ioContext;

auto client = Client::create(ioContext);
// Must setup connections handlers before client is connected.
// Data is called from the clients's internal thread, data Therefore need to be
// protected.
client->setReceiveHandler<message::Wrapper>(
    [](const message::Wrapper& message, std::error_code ec) {
        std::cout << message.text() << "\n";
    }
);

client->setConnectHandler([](std::error_code ec) {
    if (ec) {
        std::cout << ec.message() << "\n";
    } else {
        std::cout << "Jippi! Is connected to server!" << "\n";
    }
});

client->setDisconnectHandler([](std::error_code ec) {
    std::cout << "Disconnected: " << ec.message() << "\n";
});

// Some code ...

// Connect to server.
client->connect("127.0.0.1", 5012);

// Some code ...

// Set some data
message::Wrapper wrapper;
wrapper.set_text("Hellor world");

// Send to server
client->send(wrapper);

// Start running, blocks everything.
ioContext.run();

Open source

The project is under the MIT license (see LICENSE.md).

About

A C++ network library using TCP sockets, using ASIO and Protobuf.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published