| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <enet/enet.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
ENetPeer* mypeers[2] = {0, 0}; |
| 7 |
ENetAddress clientAddresses[2]; |
| 8 |
int numpeers = 0; |
| 9 |
|
| 10 |
int main(int argc, char* argv[]) |
| 11 |
{ |
| 12 |
fprintf(stderr, "starting enet dedicated server\n"); |
| 13 |
|
| 14 |
if (enet_initialize() != 0) |
| 15 |
{ |
| 16 |
fprintf(stderr, "An error occurred while initializing ENet.\n"); |
| 17 |
return EXIT_FAILURE; |
| 18 |
} |
| 19 |
atexit(enet_deinitialize); |
| 20 |
|
| 21 |
ENetAddress address; |
| 22 |
ENetHost* server; |
| 23 |
/* Bind the server to the default localhost. */ |
| 24 |
/* A specific host address can be specified by */ |
| 25 |
/* enet_address_set_host (& address, "x.x.x.x"); */ |
| 26 |
address.host = ENET_HOST_ANY; |
| 27 |
/* Bind the server to port 1234. */ |
| 28 |
address.port = 1234; |
| 29 |
server = enet_host_create(&address /* the address to bind the server host to */, |
| 30 |
32 /* allow up to 32 clients and/or outgoing connections */, |
| 31 |
2 /* allow up to 2 channels to be used, 0 and 1 */, |
| 32 |
0 /* assume any amount of incoming bandwidth */, |
| 33 |
0 /* assume any amount of outgoing bandwidth */); |
| 34 |
if (server == NULL) |
| 35 |
{ |
| 36 |
fprintf(stderr, |
| 37 |
"An error occurred while trying to create an ENet server host.\n"); |
| 38 |
exit(EXIT_FAILURE); |
| 39 |
} |
| 40 |
|
| 41 |
ENetEvent event; |
| 42 |
|
| 43 |
/* Wait up to 10000000 milliseconds for an event. */ |
| 44 |
while (enet_host_service(server, &event, 10000000) > 0) |
| 45 |
{ |
| 46 |
switch (event.type) |
| 47 |
{ |
| 48 |
case ENET_EVENT_TYPE_CONNECT: |
| 49 |
char clientname[1024]; |
| 50 |
enet_address_get_host(&event.peer->address, clientname, 1024); |
| 51 |
printf("A new client connected from %s:%u.\n", |
| 52 |
clientname, |
| 53 |
event.peer->address.port); |
| 54 |
/* Store any relevant client information here. */ |
| 55 |
event.peer->data = (char*)"Client information"; |
| 56 |
if (numpeers < 2) |
| 57 |
{ |
| 58 |
clientAddresses[numpeers] = event.peer->address; |
| 59 |
mypeers[numpeers] = event.peer; |
| 60 |
} |
| 61 |
numpeers++; |
| 62 |
if (numpeers == 2) |
| 63 |
{ |
| 64 |
printf("exchanging addresses for NAT punchthrough\n"); |
| 65 |
//exchange the address info |
| 66 |
for (int i = 0; i < 2; i++) |
| 67 |
{ |
| 68 |
int sz = sizeof(ENetAddress); |
| 69 |
/* Create a reliable packet of size 7 containing "packet\0" */ |
| 70 |
ENetPacket* packet = enet_packet_create(&clientAddresses[i], |
| 71 |
sz, |
| 72 |
ENET_PACKET_FLAG_RELIABLE); |
| 73 |
enet_peer_send(mypeers[1 - i], 0, packet); |
| 74 |
} |
| 75 |
//prepare for the next pair of clients to connect/NAT punchthrough |
| 76 |
numpeers = 0; |
| 77 |
} |
| 78 |
|
| 79 |
break; |
| 80 |
case ENET_EVENT_TYPE_RECEIVE: |
| 81 |
printf("A packet of length %u containing %s was received from %s on channel %u.\n", |
| 82 |
event.packet->dataLength, |
| 83 |
event.packet->data, |
| 84 |
event.peer->data, |
| 85 |
event.channelID); |
| 86 |
/* Clean up the packet now that we're done using it. */ |
| 87 |
enet_packet_destroy(event.packet); |
| 88 |
|
| 89 |
break; |
| 90 |
|
| 91 |
case ENET_EVENT_TYPE_DISCONNECT: |
| 92 |
printf("%s disconnected.\n", event.peer->data); |
| 93 |
/* Reset the peer's client information. */ |
| 94 |
event.peer->data = NULL; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
enet_host_destroy(server); |
| 99 |
printf("server exited, press <enter> key\n"); |
| 100 |
getchar(); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |