|
@@ -1,8 +1,7 @@
|
|
-#include "libs/enet/include/enet.h"
|
|
|
|
|
|
+#include "network/Server.h"
|
|
|
|
|
|
#include "data/HashMap.h"
|
|
#include "data/HashMap.h"
|
|
#include "network/ENet.h"
|
|
#include "network/ENet.h"
|
|
-#include "network/Server.h"
|
|
|
|
#include "utils/Logger.h"
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
static_assert(sizeof(enet_uint16) == sizeof(Server::Port),
|
|
static_assert(sizeof(enet_uint16) == sizeof(Server::Port),
|
|
@@ -16,7 +15,9 @@ static Server::OnDisconnect onDisconnect = [](Server::Client) {};
|
|
static Server::OnPacket onPacket = [](Server::Client, InPacket&) {};
|
|
static Server::OnPacket onPacket = [](Server::Client, InPacket&) {};
|
|
|
|
|
|
Error Server::start(Port port, int maxClients) {
|
|
Error Server::start(Port port, int maxClients) {
|
|
- if(server != nullptr) {
|
|
|
|
|
|
+ if(maxClients <= 0) {
|
|
|
|
+ return {"invalid max client amount"};
|
|
|
|
+ } else if(server != nullptr) {
|
|
return {"already started"};
|
|
return {"already started"};
|
|
} else if(ENet::add()) {
|
|
} else if(ENet::add()) {
|
|
return {"cannot initialize enet"};
|
|
return {"cannot initialize enet"};
|
|
@@ -27,7 +28,8 @@ Error Server::start(Port port, int maxClients) {
|
|
address.host = ENET_HOST_ANY;
|
|
address.host = ENET_HOST_ANY;
|
|
address.port = port;
|
|
address.port = port;
|
|
|
|
|
|
- server = enet_host_create(&address, maxClients, 3, 0, 0);
|
|
|
|
|
|
+ server = enet_host_create(&address, static_cast<unsigned int>(maxClients),
|
|
|
|
+ 3, 0, 0);
|
|
if(server == nullptr) {
|
|
if(server == nullptr) {
|
|
ENet::remove();
|
|
ENet::remove();
|
|
return {"cannot create enet server host"};
|
|
return {"cannot create enet server host"};
|
|
@@ -47,13 +49,13 @@ void Server::stop() {
|
|
ENet::remove();
|
|
ENet::remove();
|
|
}
|
|
}
|
|
|
|
|
|
-void writeId(ENetPeer* peer, Server::Client id) {
|
|
|
|
|
|
+static void writeId(ENetPeer* peer, Server::Client id) {
|
|
static_assert(sizeof(peer->data) >= sizeof(id),
|
|
static_assert(sizeof(peer->data) >= sizeof(id),
|
|
"private data not big enough for id");
|
|
"private data not big enough for id");
|
|
memcpy(&(peer->data), &id, sizeof(id));
|
|
memcpy(&(peer->data), &id, sizeof(id));
|
|
}
|
|
}
|
|
|
|
|
|
-Server::Client getId(ENetPeer* peer) {
|
|
|
|
|
|
+static Server::Client getId(ENetPeer* peer) {
|
|
Server::Client id = -1;
|
|
Server::Client id = -1;
|
|
memcpy(&id, &(peer->data), sizeof(id));
|
|
memcpy(&id, &(peer->data), sizeof(id));
|
|
return id;
|
|
return id;
|
|
@@ -75,7 +77,7 @@ static void handlePacket(ENetEvent& e) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
Server::Client id = getId(e.peer);
|
|
Server::Client id = getId(e.peer);
|
|
- InPacket in(e.packet->data, e.packet->dataLength);
|
|
|
|
|
|
+ InPacket in(e.packet->data, static_cast<int>(e.packet->dataLength));
|
|
onPacket(id, in);
|
|
onPacket(id, in);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -113,13 +115,15 @@ void Server::tick() {
|
|
static ENetPacket* fromBuffer(const Buffer& buffer, int index) {
|
|
static ENetPacket* fromBuffer(const Buffer& buffer, int index) {
|
|
constexpr enet_uint32 flags[] = {ENET_PACKET_FLAG_RELIABLE, 0,
|
|
constexpr enet_uint32 flags[] = {ENET_PACKET_FLAG_RELIABLE, 0,
|
|
ENET_PACKET_FLAG_UNSEQUENCED};
|
|
ENET_PACKET_FLAG_UNSEQUENCED};
|
|
- return enet_packet_create(buffer, buffer.getLength(), flags[index]);
|
|
|
|
|
|
+ return enet_packet_create(
|
|
|
|
+ buffer, static_cast<unsigned int>(buffer.getLength()), flags[index]);
|
|
}
|
|
}
|
|
|
|
|
|
void Server::send(const OutPacket& p, PacketSendMode mode) {
|
|
void Server::send(const OutPacket& p, PacketSendMode mode) {
|
|
if(server != nullptr) {
|
|
if(server != nullptr) {
|
|
int index = static_cast<int>(mode);
|
|
int index = static_cast<int>(mode);
|
|
- enet_host_broadcast(server, index, fromBuffer(p.buffer, index));
|
|
|
|
|
|
+ enet_host_broadcast(server, static_cast<enet_uint8>(index),
|
|
|
|
+ fromBuffer(p.buffer, index));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -131,7 +135,8 @@ void Server::send(Server::Client client, const OutPacket& p,
|
|
ENetPeer** peer = clients.search(client);
|
|
ENetPeer** peer = clients.search(client);
|
|
if(peer != nullptr) {
|
|
if(peer != nullptr) {
|
|
int index = static_cast<int>(mode);
|
|
int index = static_cast<int>(mode);
|
|
- enet_peer_send(*peer, index, fromBuffer(p.buffer, index));
|
|
|
|
|
|
+ enet_peer_send(*peer, static_cast<enet_uint8>(index),
|
|
|
|
+ fromBuffer(p.buffer, index));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|