eco2d/code/apps/client/source/network.c

114 lines
2.9 KiB
C
Raw Normal View History

2021-01-10 16:42:01 +00:00
#include "zpl.h"
2021-01-17 07:30:59 +00:00
#define ENET_IMPLEMENTATION
#include "enet.h"
2021-01-10 16:42:01 +00:00
#define LIBRG_IMPL
#define LIBRG_CUSTOM_ZPL
2021-05-09 19:12:11 +00:00
#define LIBRG_ENTITY_MAXCHUNKS 1
2021-01-10 16:42:01 +00:00
#include "librg.h"
2021-01-11 13:47:14 +00:00
#include "network.h"
2021-05-05 09:25:05 +00:00
#include "packet.h"
2021-05-10 06:28:56 +00:00
#include "world/world.h"
2021-01-17 07:30:59 +00:00
#define NETWORK_UPDATE_DELAY 0.100
static ENetHost *host = NULL;
static ENetPeer *peer = NULL;
static librg_world *world = NULL;
int32_t network_init() {
return enet_initialize() != 0;
}
int32_t network_destroy() {
enet_deinitialize();
return 0;
}
2021-05-06 15:30:38 +00:00
2021-01-17 07:30:59 +00:00
int32_t network_client_connect(const char *hostname, uint16_t port) {
ENetAddress address = {0}; address.port = port;
enet_address_set_host(&address, hostname);
host = enet_host_create(NULL, 1, 2, 0, 0);
peer = enet_host_connect(host, &address, 2, 0);
if (peer == NULL) {
zpl_printf("[ERROR] Cannot connect to specicied server: %s:%d\n", hostname, port);
return 1;
}
world = librg_world_create();
librg_world_userdata_set(world, peer);
2021-05-05 14:00:10 +00:00
#if 0
2021-01-17 07:30:59 +00:00
librg_event_set(world, LIBRG_READ_CREATE, client_read_create);
librg_event_set(world, LIBRG_READ_UPDATE, client_read_update);
librg_event_set(world, LIBRG_READ_REMOVE, client_read_remove);
2021-05-05 14:00:10 +00:00
#endif
2021-01-17 07:30:59 +00:00
return 0;
}
int32_t network_client_disconnect() {
enet_peer_disconnect_now(peer, 0);
enet_host_destroy(host);
librg_world_destroy(world);
peer = NULL;
host = NULL;
world = NULL;
return 0;
}
int32_t network_client_tick() {
ENetEvent event = {0};
while (enet_host_service(host, &event, 1) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: {
zpl_printf("[INFO] We connected to the server.\n");
} break;
case ENET_EVENT_TYPE_DISCONNECT:
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: {
zpl_printf("[INFO] We disconnected from server.\n");
} break;
case ENET_EVENT_TYPE_RECEIVE: {
2021-05-04 19:22:55 +00:00
if (!world_read(event.packet->data, event.packet->dataLength, NULL)) {
2021-05-03 23:57:19 +00:00
zpl_printf("[INFO] Server sent us an unsupported packet.\n");
}
2021-01-17 07:30:59 +00:00
/* Clean up the packet now that we're done using it. */
enet_packet_destroy(event.packet);
} break;
case ENET_EVENT_TYPE_NONE: break;
}
}
return 0;
}
bool network_client_is_connected() {
return peer ? enet_peer_get_state(peer) == ENET_PEER_STATE_CONNECTED : false;
}
2021-01-26 19:16:08 +00:00
2021-05-04 19:22:55 +00:00
static int32_t network_msg_send_raw(uint16_t peer_id, void *data, size_t datalen, uint32_t flags) {
2021-01-26 19:16:08 +00:00
ENetPacket *packet = enet_packet_create(data, datalen, flags);
2021-05-04 19:22:55 +00:00
return enet_peer_send(peer, 0, packet);
2021-01-26 19:16:08 +00:00
}
2021-05-04 19:22:55 +00:00
int32_t network_msg_send(void *data, size_t datalen) {
return network_msg_send_raw(0, data, datalen, ENET_PACKET_FLAG_RELIABLE);
2021-01-26 19:16:08 +00:00
}
2021-05-04 19:22:55 +00:00
int32_t network_msg_send_unreliable(void *data, size_t datalen) {
return network_msg_send_raw(0, data, datalen, 0);
2021-01-26 19:16:08 +00:00
}