From e342c45806d3eeb1ec560f0bcaec81de0980463e Mon Sep 17 00:00:00 2001 From: scatterflower Date: Tue, 25 Aug 2020 01:51:30 -0500 Subject: [PATCH] implement more packets --- include/aoclient.h | 2 ++ include/server.h | 2 ++ src/advertiser.cpp | 5 +++-- src/aoclient.cpp | 2 ++ src/aopacket.cpp | 6 +++++- src/server.cpp | 54 ++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index b15115f..06a745a 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -17,6 +17,8 @@ public: QString getIpid(); QHostAddress remote_ip; + QString password; + bool joined; private: QString hwid; diff --git a/include/server.h b/include/server.h index 631a255..c08ef46 100644 --- a/include/server.h +++ b/include/server.h @@ -4,6 +4,7 @@ #include "include/aopacket.h" #include "include/aoclient.h" +#include #include #include #include @@ -28,6 +29,7 @@ public slots: private: void handlePacket(AOPacket packet, QTcpSocket* socket); QTcpSocket* getClient(QString ipid); + void broadcast(AOPacket packet); QTcpServer* server; diff --git a/src/advertiser.cpp b/src/advertiser.cpp index 05e1b8c..1e908b3 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -29,7 +29,7 @@ void Advertiser::readData() // The information coming back from the MS isn't very useful // However, it can be useful to see it when debugging // TODO: master network debug switch - qDebug() << "From MS:" << socket->readAll(); + // qDebug() << "From MS:" << socket->readAll(); } void Advertiser::socketConnected() @@ -48,7 +48,8 @@ void Advertiser::socketConnected() socket->write(data); // TODO: master network debug switch - qDebug() << "To MS:" << data; + // should be a separate one for MS as well + // qDebug() << "To MS:" << data; socket->flush(); } diff --git a/src/aoclient.cpp b/src/aoclient.cpp index 8cc8441..5f74e8e 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -2,6 +2,8 @@ AOClient::AOClient(QHostAddress p_remote_ip) { + joined = false; + password = ""; remote_ip = p_remote_ip; } diff --git a/src/aopacket.cpp b/src/aopacket.cpp index 06d97bc..d8dd737 100644 --- a/src/aopacket.cpp +++ b/src/aopacket.cpp @@ -13,18 +13,22 @@ AOPacket::AOPacket(QString p_packet) // The header is encrypted with FantaCrypt // The server always uses the same key for FantaCrypt // That way, we can just hardcode FantaCrypted headers + // TODO: replace this with a key/value map? packet_contents.removeFirst(); if(packet_contents[0] == "48E0") header = "HI"; else if(packet_contents[0] == "493F") header = "ID"; + else if(packet_contents[0] == "615810BC07D12A5A") + header = "askchaa"; else header = packet_contents[0]; // If no known decryption exists, just leave the packet as-is } else { header = packet_contents[0]; } - packet_contents.removeFirst(); + packet_contents.removeFirst(); // Remove header + packet_contents.removeLast(); // Remove anything trailing after delimiter contents = packet_contents; } diff --git a/src/server.cpp b/src/server.cpp index 2c79b01..e62e02a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -50,26 +50,25 @@ 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() { if(QTcpSocket* client = dynamic_cast(sender())){ qDebug() << client->peerAddress() << "disconnected"; + if(clients.value(client)->joined) + player_count--; + delete clients.value(client); 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; + //qDebug() << "From" << client->peerAddress() << ":" << data; if(is_partial) { data = partial_packet + data; @@ -78,13 +77,21 @@ void Server::clientData() is_partial = true; } - AOPacket packet(data); - handlePacket(packet, client); + QStringList all_packets = data.split("%"); + all_packets.removeLast(); // Remove the entry after the last delimiter + + for(QString single_packet : all_packets) + { + AOPacket packet(single_packet); + handlePacket(packet, client); + } } } void Server::handlePacket(AOPacket packet, QTcpSocket* socket) { + qDebug() << "Received packet:" << packet.header << ":" << packet.contents; + AOClient* client = clients.value(socket); // Lord forgive me if(packet.header == "HI"){ AOClient* client = clients.value(socket); @@ -119,12 +126,36 @@ void Server::handlePacket(AOPacket packet, QTcpSocket* socket) AOPacket response("SM", {"~stop.mp3"}); socket->write(response.toUtf8()); } else if(packet.header == "RD") { + player_count++; + client->joined = true; + AOPacket response_cc("CharsCheck", {"0", "0"}); AOPacket response_op("OPPASS", {"DEADBEEF"}); AOPacket response_done("DONE", {}); socket->write(response_cc.toUtf8()); socket->write(response_op.toUtf8()); socket->write(response_done.toUtf8()); + } else if(packet.header == "PW") { + client->password = packet.contents[0]; + } else if(packet.header == "CC") { + // TODO: properly implement this when adding characters + qDebug() << client->getIpid() << "chose character" << packet.contents[1] << "using password" << client->password; + + AOPacket response("PV", {"271828", "CID", packet.contents[1]}); + socket->write(response.toUtf8()); + } else if(packet.header == "MS") { + // TODO: validate, validate, validate + broadcast(packet); + } else if(packet.header == "CT") { + // TODO: commands + // TODO: zalgo strip + broadcast(packet); + } else if(packet.header == "CH") { + // Why does this packet exist + AOPacket response("CHECK", {}); + socket->write(response.toUtf8()); + } else if(packet.header == "what") { + AOPacket response("CT", {"Made with love", "by scatterflower and windrammer"}); } else { qDebug() << "Unimplemented packet:" << packet.header; qDebug() << packet.contents; @@ -132,6 +163,15 @@ void Server::handlePacket(AOPacket packet, QTcpSocket* socket) socket->flush(); } +void Server::broadcast(AOPacket packet) +{ + for(QTcpSocket* client : clients.keys()) + { + client->write(packet.toUtf8()); + client->flush(); + } +} + QTcpSocket* Server::getClient(QString ipid) { for(QTcpSocket* client : clients.keys())