diff --git a/include/enet.h b/include/enet.h index 4159b7c..e2f682e 100644 --- a/include/enet.h +++ b/include/enet.h @@ -614,7 +614,7 @@ extern "C" { ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024, ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024, ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000, - ENET_HOST_DEFAULT_MTU = 1400, + ENET_HOST_DEFAULT_MTU = 1392, ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024, ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024, @@ -987,12 +987,12 @@ extern "C" { ENET_API void enet_host_destroy(ENetHost *); ENET_API ENetPeer * enet_host_connect(ENetHost *, const ENetAddress *, size_t, enet_uint32); ENET_API int enet_host_check_events(ENetHost *, ENetEvent *); - ENET_API int enet_host_service(ENetHost *, ENetEvent *, enet_uint32); + ENET_API int enet_host_service(ENetHost *, ENetEvent *, enet_uint32); ENET_API int enet_host_send_raw(ENetHost *, const ENetAddress *, enet_uint8 *, size_t); ENET_API int enet_host_send_raw_ex(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t skipBytes, size_t bytesToSend); ENET_API void enet_host_set_intercept(ENetHost *, const ENetInterceptCallback); ENET_API void enet_host_flush(ENetHost *); - ENET_API void enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *); + ENET_API void enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *); ENET_API void enet_host_compress(ENetHost *, const ENetCompressor *); ENET_API void enet_host_channel_limit(ENetHost *, size_t); ENET_API void enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32); @@ -1392,8 +1392,8 @@ extern "C" { return packet; } - /** Attempts to resize the data in the packet to length specified in the - dataLength parameter + /** Attempts to resize the data in the packet to length specified in the + dataLength parameter @param packet packet to resize @param dataLength new size for the packet data @returns 0 on success, < 0 on failure @@ -1415,7 +1415,7 @@ extern "C" { memcpy(newData, packet->data, packet->dataLength); enet_free(packet->data); - + packet->data = newData; packet->dataLength = dataLength; @@ -4663,7 +4663,7 @@ extern "C" { } } - /** Sends raw data to specified address. Useful when you want to send unconnected data using host's socket. + /** Sends raw data to specified address. Useful when you want to send unconnected data using host's socket. * @param host host sending data * @param address destination address * @param data data pointer diff --git a/test/cli-server.c b/test/cli-server.c new file mode 100644 index 0000000..3cff8da --- /dev/null +++ b/test/cli-server.c @@ -0,0 +1,145 @@ +#include +#define ENET_IMPLEMENTATION +#include "enet.h" +// #define WIN32_LEAN_AND_MEAN +// #include + +void run_server(); +void run_client(); + +int main(int argc, char** argv) { + if (enet_initialize() < 0) { + printf("failed to initialize enet\n"); + }; + + bool server; + if (argc == 2 && !strcmp(argv[1], "-server")) { + server = true; + } else { + server = false; + } + + if (server) { + printf("launching server\n"); + run_server(); + } else { + printf("launching client\n"); + run_client(); + } + + printf("done\n"); + enet_deinitialize(); + + return 0; +} + +void run_server() { + ENetAddress address = {}; + address.host = ENET_HOST_ANY; + address.port = 1234; + + ENetHost* host = enet_host_create(&address, 4, 1, 0, 0); + + if (!host) { + printf("Failed to create server\n"); + } + + printf("Server started...\n"); + + while (true) { + ENetEvent event; + while (enet_host_service(host, &event, 2) > 0) { + switch (event.type) { + case ENET_EVENT_TYPE_CONNECT: { + printf("A client has connected!\n"); + } break; + case ENET_EVENT_TYPE_RECEIVE: { + printf("Message recieved! %s\n", event.packet->data); + enet_packet_destroy(event.packet); + } break; + case ENET_EVENT_TYPE_DISCONNECT: + case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: + printf("A client has disconnected\n"); + break; + case ENET_EVENT_TYPE_NONE: + break; + } + } + + ENetPeer* currentPeer; + // for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) { + // if (currentPeer->state != ENET_PEER_STATE_CONNECTED) { + // continue; + // } + + // ENetPacket * packet = enet_packet_create("HELLO WORLD", strlen("HELLO WORLD"), ENET_PACKET_FLAG_RELIABLE); + // enet_peer_send(currentPeer, 0, packet); + // } + + for (int i = 0; i < 4; i++) { + ENetPeer* peer = &host->peers[i]; + if (peer->state == ENET_PEER_STATE_CONNECTED) { + ENetPacket * packet = + enet_packet_create("HELLO WORLD", strlen("HELLO WORLD"), + ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(peer, 0, packet); + } + } + + usleep(16000); + } + + enet_host_destroy(host); +} + +void run_client() { + ENetAddress address = {}; + address.host = ENET_HOST_ANY; + address.port = 1234; + + ENetHost* host = enet_host_create(NULL, 1, 1, 0, 0); + + enet_address_set_host(&address, "127.0.0.1"); + address.port = 1234; + printf("Connecting to server\n"); + ENetPeer * peer = enet_host_connect(host, &address, 1, 0); + + if (!host) { + printf("Failed to create client\n"); + } + + bool connected = false; + + while (true) { + ENetEvent event; + while (enet_host_service(host, &event, 0) > 0) { + switch (event.type) { + case ENET_EVENT_TYPE_CONNECT: { + printf("Connected to server\n"); + connected = true; + } break; + case ENET_EVENT_TYPE_RECEIVE: { + printf("Message recieved! %s\n", event.packet->data); + enet_packet_destroy(event.packet); + } break; + case ENET_EVENT_TYPE_DISCONNECT: + case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: + printf("A client has disconnected\n"); + break; + } + } + + if (connected) { + if (peer->state == ENET_PEER_STATE_CONNECTED) { + ENetPacket *packet = + enet_packet_create("HELLO WORLD", strlen("HELLO WORLD"), + ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(peer, 0, packet); + } + } + + usleep(16000); + } + + enet_host_destroy(host); +}