diff --git a/include/aoclient.h b/include/aoclient.h index 6dbf3c7..4e928b1 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -6,7 +6,10 @@ class AOClient { public: - AOClient(); + AOClient(QString p_remote_ip); + + QString hdid; + QString remote_ip; private: diff --git a/include/server.h b/include/server.h index c2d11be..0a21ecb 100644 --- a/include/server.h +++ b/include/server.h @@ -4,7 +4,9 @@ #include "include/aopacket.h" #include "include/aoclient.h" +#include #include +#include #include #include #include @@ -24,6 +26,8 @@ public slots: void clientData(); private: + void handlePacket(AOPacket packet, QTcpSocket* socket); + QTcpServer* server; int port; @@ -32,6 +36,8 @@ private: QMap clients; QString partial_packet; bool is_partial; + + int player_count; }; #endif // SERVER_H diff --git a/src/aoclient.cpp b/src/aoclient.cpp index 0d82eff..092d0a8 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -1,6 +1,6 @@ #include "include/aoclient.h" -AOClient::AOClient() +AOClient::AOClient(QString p_remote_ip) { - + remote_ip = p_remote_ip; } diff --git a/src/aopacket.cpp b/src/aopacket.cpp index 76499d1..06d97bc 100644 --- a/src/aopacket.cpp +++ b/src/aopacket.cpp @@ -16,6 +16,10 @@ AOPacket::AOPacket(QString p_packet) packet_contents.removeFirst(); if(packet_contents[0] == "48E0") header = "HI"; + else if(packet_contents[0] == "493F") + header = "ID"; + else + header = packet_contents[0]; // If no known decryption exists, just leave the packet as-is } else { header = packet_contents[0]; diff --git a/src/server.cpp b/src/server.cpp index d91dacb..db7581f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -8,6 +8,8 @@ Server::Server(int p_port, int p_ws_port, QObject* parent) port = p_port; ws_port = p_ws_port; + + player_count = 0; } void Server::start() @@ -34,7 +36,7 @@ void Server::start() void Server::clientConnected() { QTcpSocket* client = server->nextPendingConnection(); - AOClient ao_client; + AOClient ao_client(client->peerAddress().toString()); clients.insert(client, ao_client); connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(client, SIGNAL(readyRead()), this, SLOT(clientData())); @@ -43,6 +45,8 @@ void Server::clientConnected() client->write(decryptor.toUtf8()); qDebug() << client->peerAddress().toString() << "connected"; + // TODO: only increment this once someone actually enters the coutroom + player_count++; } void Server::clientDisconnected() @@ -50,11 +54,13 @@ void Server::clientDisconnected() if(QTcpSocket* client = dynamic_cast(sender())){ qDebug() << client->peerAddress() << "disconnected"; clients.remove(client); + player_count--; } } void Server::clientData() { + // TODO: deal with more than one packet on wire if(QTcpSocket* client = dynamic_cast(sender())){ QString data = QString::fromUtf8(client->readAll()); qDebug() << "From" << client->peerAddress() << ":" << data; @@ -67,5 +73,31 @@ void Server::clientData() } AOPacket packet(data); + handlePacket(packet, client); } } + +void Server::handlePacket(AOPacket packet, QTcpSocket* socket) +{ + // Lord forgive me + if(packet.header == "HI"){ + AOPacket response("ID", {"271828", "akashi", QApplication::applicationVersion()}); + socket->write(response.toUtf8()); + } else if (packet.header == "ID"){ + QSettings config("config.ini", QSettings::IniFormat); + config.beginGroup("Options"); + QString max_players = config.value("max_players").toString(); + config.endGroup(); + + QStringList feature_list = {"noencryption"}; + + AOPacket response_pn("PN", {QString::number(player_count), max_players}); + AOPacket response_fl("FL", feature_list); + socket->write(response_pn.toUtf8()); + socket->write(response_fl.toUtf8()); + } else { + qDebug() << "Unimplemented packet:" << packet.header; + qDebug() << packet.contents; + } + socket->flush(); +}