maluoi / warm-sock

A single header high-level socket/networking library for building ad-hoc server/client multi-user experiences.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

warm-sock

warm_sock logo

A single header high-level socket/networking library for building server/client multi-user experiences.

Still in development, more info to follow! This is designed to be the networking component of StereoKit, so it needs to be able to handle networking scenarios common to shared Mixed Reality experiences.

Features

  • Simple API
  • Server/Client architecture
  • LAN discovery
  • Non-blocking core loop
  • Send to one/send to all
  • Connect/disconnect events
  • Easy send/receive structs
  • Server can be transferred to a client
  • Send large data

Example usage

For full example, see example.cpp. But here's a quick taste of the core experience!

// Set up our callback functions
sock_on_connection(on_connection);
sock_on_receive([](sock_header_t header, const void *data) {
    switch (header.data_id) {
    case sock_hash_type(test_data_t): {

        const test_data_t *test = (const test_data_t *)data;
        printf("test_data_t: [%.2f, %.2f, %.2f], [%.2f, %.2f, %.2f]\n",
            test->position [0], test->position [1], test->position [2],
            test->direction[0], test->direction[1], test->direction[2]);

    }
    default: break;
    }
});

// Set up warm_sock
if (!sock_init(sock_hash("Warm sock example"), 27015)) return 0;

// Connect to a server if we find one. If not, make our own server!
char addr[32];
if (sock_find_server(addr, sizeof(addr))) {
    printf("Connecting to server at %s\n", addr);
    if (!sock_start_client(addr)) return 0;
} else {
    printf("Starting a server!\n");
    if (!sock_start_server()) return 0;
}

// Poll for network events until it crashes or we get bored!
while (sock_poll()) {
    if (_kbhit() != 0) {

        // Send a message using a struct
        test_data_t test = { {1, 2, 3}, {3, 2, 1} };
        sock_send(sock_hash_type(test_data_t), sizeof(test), &test);

    }
    Sleep(1);
}

sock_shutdown();

License

MIT or Public Domain. See bottom of warm_sock.h for details.

About

A single header high-level socket/networking library for building ad-hoc server/client multi-user experiences.


Languages

Language:C 87.7%Language:C++ 12.3%