Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed typo + added client example #31

Merged
merged 1 commit into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Add include path to the library `node_modules/enet.c/include` to your makefile/

## Installation (manually)

Dowload file [include/enet.h](https://raw.githubusercontent.com/zpl-c/enet/master/include/enet.h) and just add to your project.
Download file [include/enet.h](https://raw.githubusercontent.com/zpl-c/enet/master/include/enet.h) and just add to your project.

## Usage (Shared library)

Expand Down Expand Up @@ -132,7 +132,9 @@ In this case, library will be embedded to the project itself.

Make sure you add a define for `ENET_IMPLEMENTATION` exactly in one source file 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:
Here is a simple server and client demo, it will wait 1 second for events, and then exit if none were found:

Server:

```c
#define ENET_IMPLEMENTATION
Expand Down Expand Up @@ -207,6 +209,88 @@ int main() {

```

Client:

```c
#include <stdio.h>
#define ENET_IMPLEMENTATION
#include "enet.h"

int main() {
if (enet_initialize() != 0) {
fprintf(stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}

ENetHost* client = { 0 };
client = enet_host_create(NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
2 /* allow up 2 channels to be used, 0 and 1 */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
if (client == NULL) {
fprintf(stderr,
"An error occurred while trying to create an ENet client host.\n");
exit(EXIT_FAILURE);
}

ENetAddress address = { 0 };
ENetEvent event = { 0 };
ENetPeer* peer = { 0 };
/* Connect to some.server.net:1234. */
enet_address_set_host(&address, "127.0.0.1");
address.port = 7777;
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect(client, &address, 2, 0);
if (peer == NULL) {
fprintf(stderr,
"No available peers for initiating an ENet connection.\n");
exit(EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service(client, &event, 5000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT) {
puts("Connection to some.server.net:1234 succeeded.");
} else {
/* Either the 5 seconds are up or a disconnect event was */
/* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */
enet_peer_reset(peer);
puts("Connection to some.server.net:1234 failed.");
}

// Receive some events
enet_host_service(client, &event, 5000);

// Disconnect
enet_peer_disconnect(peer, 0);

uint8_t disconnected = false;
/* Allow up to 3 seconds for the disconnect to succeed
* and drop any packets received packets.
*/
while (enet_host_service(client, &event, 3000) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
puts("Disconnection succeeded.");
disconnected = true;
break;
}
}

// Drop connection, since disconnection didn't successed
if (!disconnected) {
enet_peer_reset(peer);
}

enet_host_destroy(client);
enet_deinitialize();
}
```

## Tutorials

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