No Description

Vladyslav Hrytsenko 3d85ddbc31 updated changelog, cmake 6 years ago
include eedc42c8fd One less malloc and free per packet. 6 years ago
misc 3d85ddbc31 updated changelog, cmake 6 years ago
test b5bddc7895 Timeout disconnect notification 6 years ago
.editorconfig c769e3badb moved src files to a separate dir 6 years ago
.gitignore dda7302da7 added modules to ignore 6 years ago
.travis.yml 8c8cc837f2 removed compression 6 years ago
CMakeLists.txt 3d85ddbc31 updated changelog, cmake 6 years ago
LICENSE e5ce1d59f8 added new single header file format 6 years ago
README.md e744a9b3b0 updated header 6 years ago
package-lock.json e5ce1d59f8 added new single header file format 6 years ago
package.json c6fd74cd38 cleaned up modules 6 years ago

README.md

ENet


Build status NPM version Discord server license


ENet - Simple, lightweight and reliable UDP networking library written on pure C
Brought to you by @lsalzman, @inlife, @zaklaus and other contributors!

Disclaimer

This is a fork of the original library lsalzman/enet. While original repo offers a stable, time-tested wonderful library, we are trying to change some things, things, which can't be reflected on the main repo, like:

  • NPM package distribution
  • Single-Header style code
  • Removed some config/make files
  • Project/Code cleanup
  • And other various changes

Description

ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets.

ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.

Installation (via npm)

Install library using (omit --save if you dont have npm project initilized)

$ npm install enet.c --save

Add include path to the library node_modules/enet.c/include to your makefile/

Installation (manually)

Dowload file include/enet.h and just add to your project.

Usage

Make sure you add a define for ENET_IMPLEMENTATION before including the enet.h.

Here is a simple server demo, it will wait 1 second for events, and then exit if none were found:

#define ENET_IMPLEMENTATION
#include <enet.h>

int main() {
    if (enet_initialize () != 0) {
        printf("An error occurred while initializing ENet.\n");
        return 1;
    }

    ENetAddress address = {0};

    address.host = ENET_HOST_ANY; /* Bind the server to the default localhost.     */
    address.port = 7777; /* Bind the server to port 7777. */

    #define MAX_CLIENTS 32

    /* create a server */
    ENetHost * server = enet_host_create(&address, MAX_CLIENTS, 2, 0, 0);

    if (server == NULL) {
        printf("An error occurred while trying to create an ENet server host.\n");
        return 1;
    }

    printf("Started a server...\n");

    ENetEvent event;

    /* Wait up to 1000 milliseconds for an event. (WARNING: blocking) */
    while (enet_host_service(server, &event, 1000) > 0) {
        switch (event.type) {
            case ENET_EVENT_TYPE_CONNECT:
                printf("A new client connected from %x:%u.\n",  event.peer->address.host, event.peer->address.port);
                /* Store any relevant client information here. */
                event.peer->data = "Client information";
                break;
            case ENET_EVENT_TYPE_RECEIVE:
                printf("A packet of length %lu containing %s was received from %s on channel %u.\n",
                        event.packet->dataLength,
                        event.packet->data,
                        event.peer->data,
                        event.channelID);

                /* Clean up the packet now that we're done using it. */
                enet_packet_destroy (event.packet);
                break;

            case ENET_EVENT_TYPE_DISCONNECT:
                printf ("%s disconnected.\n", event.peer->data);
                /* Reset the peer's client information. */
                event.peer->data = NULL;
                break;

            case ENET_EVENT_TYPE_NONE: break;
        }
    }

    enet_host_destroy(server);
    enet_deinitialize();
    return 0;
}

Tutorials

More information, examples and tutorials can be found at the official site: http://enet.bespin.org/