Add handlePacket function to server and commit ClientConnection class
parent
c608e2336d
commit
0639c3b655
|
@ -90,6 +90,9 @@ add_executable(zeus
|
|||
zeus/server/Server.cpp
|
||||
zeus/server/Server.h
|
||||
zeus/client/ClientPacket.cpp
|
||||
zeus/client/ClientPacket.h zeus/server/ClientConnection.cpp zeus/server/ClientConnection.h zeus/server/Packet.cpp zeus/server/Packet.h)
|
||||
zeus/server/ClientConnection.cpp
|
||||
zeus/server/ClientConnection.h
|
||||
zeus/server/Packet.cpp
|
||||
zeus/server/Packet.h)
|
||||
|
||||
target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl)
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
//
|
||||
// Created by aurailus on 10/01/19.
|
||||
//
|
||||
|
||||
#include "ClientConnection.h"
|
||||
|
||||
ClientConnection::ClientConnection() = default;
|
||||
|
||||
ClientConnection::ClientConnection(asio::ip::udp::endpoint* endpoint) {
|
||||
this->endpoint = endpoint;
|
||||
}
|
||||
|
||||
ClientConnection::~ClientConnection() {
|
||||
delete this->endpoint;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// Created by aurailus on 10/01/19.
|
||||
//
|
||||
|
||||
#ifndef ZEUS_CLIENTCONNECTION_H
|
||||
#define ZEUS_CLIENTCONNECTION_H
|
||||
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
class ClientConnection {
|
||||
public:
|
||||
ClientConnection();
|
||||
explicit ClientConnection(asio::ip::udp::endpoint* endpoint);
|
||||
|
||||
asio::ip::udp::endpoint* endpoint;
|
||||
long lastAliveTime;
|
||||
|
||||
~ClientConnection();
|
||||
};
|
||||
|
||||
|
||||
#endif //ZEUS_CLIENTCONNECTION_H
|
|
@ -30,9 +30,9 @@ public:
|
|||
static void encodeInt(std::vector<PacketByte> &target, int num);
|
||||
static int decodeInt(PacketByte* intStart);
|
||||
public:
|
||||
const static PacketType HANDSHAKE = 0;
|
||||
const static PacketType AUTHENTICATE = 1;
|
||||
const static PacketType KEEPALIVE = 2;
|
||||
const static PacketType HANDSHAKE = 0;
|
||||
const static PacketType REQCHUNKS = 1;
|
||||
const static PacketType KEEPALIVE = 2;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ void Server::start() {
|
|||
}
|
||||
|
||||
void Server::loop() {
|
||||
Timer t("Loop time");
|
||||
|
||||
//Collect incoming packets
|
||||
while (server_socket->available() > 0) {
|
||||
|
@ -32,11 +33,29 @@ void Server::loop() {
|
|||
if (packet.length > 0) handlePacket(packet, remote_endpoint);
|
||||
}
|
||||
|
||||
long sleep_for = 16L*1000000L;
|
||||
long sleep_for = 16L*1000000L - t.elapsedNs();
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(sleep_for));
|
||||
}
|
||||
|
||||
void Server::handlePacket(Packet &packet, udp::endpoint* endpoint) {
|
||||
std::string identifier = endpoint->address().to_string() + ":" + std::to_string(endpoint->port());
|
||||
|
||||
if (packet.type == Packet::HANDSHAKE) {
|
||||
if (connections.count(identifier) == 0) {
|
||||
std::cout << "Added " << identifier << " to client connections." << std::endl;
|
||||
auto conn = new ClientConnection(endpoint);
|
||||
connections.insert(std::pair<std::string, ClientConnection*>(identifier, conn));
|
||||
}
|
||||
else {
|
||||
std::cout << identifier << " is already added to client connections." << std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (connections.count(identifier) == 0) {
|
||||
std::cout << "Non-handshake packet from identifier " << identifier << "." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << packet.type << ", " << Packet::decodeInt(&packet.data[0]) << std::endl;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue