KronkNet 1.0
Loading...
Searching...
No Matches
README

Contributors Forks Stargazers Issues License LinkedIn


Kronk Corp logo     KronkNet logo

KronkNet

A simple and fast asynchrone network library
Explore the usage ยป

View Examples · Report Bug · Request Feature
Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Contact

About The Project

KronkNet is a high-performance asynchronous TCP network library.

Built around the poll() system call and an event-driven architecture, it allows for the management of numerous simultaneous connections on a single thread. KronkNet integrates an internal ring buffer system to prevent OS blockage during massive data transfers (Fast Path/Slow Path mechanism with POLLOUT).

Key Features:

  • 100% Non-Blocking: The server never freezes the main game loop.
  • O(1) Binding: Dual-pointer (user_data) system for lookup-free interaction between the network engine and application entities.
  • Multi-protocol support: Currently, kronknet supports both TCP and UDP. In TCP mode, connections are stored in a dynamic array, whereas in UDP mode, they are kept in a hash table, offering O(1) lookup complexity in most cases.
  • IPv4 and IPv6: A server takes both on a single dual-stack socket (IPv4 only on a host without IPv6), and a client connects to either: knClient_connect(client, "::1", 4242). IPv4 clients keep showing as IPv4 (127.0.0.1).

(back to top)

Built With

  • C

(back to top)

Getting Started

To get a local copy up and running, follow these steps.

Prerequisites

CMake and a C compiler:

sudo apt install cmake build-essential

Installation

  1. Clone the repo
    git clone https://github.com/GauthierMalfilatre/kronknet.git
    cd kronknet/
  2. Build with the provided tools.sh script
    ./tools.sh -r
  3. Install the library
    cd Release/
    sudo make install

Windows

kronknet builds on Windows (Vista or later) with MSVC or MinGW:

cmake -S . -B build
cmake --build build --config Release

You get kronknet.dll (with its import library) and a static library (kronknet_static.lib with MSVC, libkronknet.a with MinGW). The static library needs ws2_32: with CMake, linking the kronknet-static or kronknet-shared target takes care of everything.

The unit tests need fork(), so Windows only runs the smoke test (-DBUILD_TESTS=ON, then ctest -C Release).

(back to top)

Usage

Creating a server

A server is a port, a protocol, and up to four callbacks. KronkNet does the polling, buffering and accepting; you only react to events.

#include <kronknet/server/server.h>
#include <kronknet/connection/connection.h>
#include <kronknet/macros/errdef.h>
#include <stdio.h>
static int on_connect(knServer *server, knConnection *conn)
{
(void)server;
printf("+ %s:%hu\n", knConnection_getIp(conn), knConnection_getPort(conn));
return KNEVTOK;
}
static int on_read(knConnection *conn, const void *data, size_t size)
{
return knConnection_send(conn, data, size); // echo it back
}
static int on_disconnect(knServer *server, knConnection *conn)
{
(void)server;
printf("- %s:%hu\n", knConnection_getIp(conn), knConnection_getPort(conn));
return KNEVTOK;
}
int main(void)
{
knServer *server = knServer_create(4242, knTCP);
if (!server)
return 1;
knServer_setOnConnect(server, on_connect);
knServer_setOnRead(server, on_read);
knServer_setOnDisconnect(server, on_disconnect);
knServer_run(server); // blocks until knServer_stop()
knServer_destroy(server);
return 0;
}

Build and try it:

gcc echo.c -o echo -lkronknet
./echo &
nc localhost 4242
  • Pass knUDP instead of knTCP to knServer_create for a UDP server.
  • Callbacks return KNEVTOK on success, or KNEVTKICK to drop the connection.
  • Need to tick the server inside your own loop (a game loop, for instance)? Use knServer_runOnce(server, timeoutMs) instead of knServer_run.
  • Attach your own data with knServer_setUserPtr and knConnection_setUserPtr, and get it back in any callback.

More complete examples: chat, hangman and the test server.

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". The full guidelines (setup, tests, branches, commit style) are in CONTRIBUTING.md. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/amazing-feature dev)
  3. Commit your Changes (git commit -m '[ADD] Add some amazing feature')
  4. Push to the Branch (git push origin feat/amazing-feature)
  5. Open a Pull Request against dev

(back to top)

Top contributors:

contrib.rocks image

License

Distributed under the MIT license. See LICENSE for more information.

(back to top)

Contact

Gauthier Malfilatre - @GauthierMalfilatre - gauth.nosp@m.ier..nosp@m.malfi.nosp@m.latr.nosp@m.e@epi.nosp@m.tech.nosp@m..eu

Project Link: https://github.com/GauthierMalfilatre/kronknet

(back to top)