aboutsummaryrefslogtreecommitdiff
path: root/src/demoserver.cpp
diff options
context:
space:
mode:
authorCrystalwarrior <Varsash@Gmail.com>2022-07-30 19:42:22 +0300
committerGitHub <noreply@github.com>2022-07-30 18:42:22 +0200
commit7b88d4be954b415e069ee0d612e4df3793c61756 (patch)
treeeb8b93c10d369c0d0c5d6629e61dc661a0863481 /src/demoserver.cpp
parentcf91cc03f849bba498cd8d91505bf8db04f8b1f9 (diff)
Never send an unencoded packet to the server (#719)
* never send an unencoded packet to the server * oops * Improve packet validation to remove segfaults * WARNING: commit breaks connecting to servers, need help start fixing omniwhy caused by single fuckin string packets (AAAAAAAAAAAAAAAAA) * Fix failed connections to servers (Thanks to @Iuvee for helping me figure this out!) * Fix demoserver * who the fuck still uses goto Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * ANOTHER GOTO???? Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * braces Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * good bot Update src/packet_distribution.cpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix demoserver harder * Improve demo logging * Fix memory leakage by deleting the packet Fix useless demoserver wait packet creation when none of that packet is used Co-authored-by: stonedDiscord <Tukz@gmx.de> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Salanto <62221668+Salanto@users.noreply.github.com>
Diffstat (limited to 'src/demoserver.cpp')
-rw-r--r--src/demoserver.cpp74
1 files changed, 44 insertions, 30 deletions
diff --git a/src/demoserver.cpp b/src/demoserver.cpp
index 0aefc0cc..feca7c6d 100644
--- a/src/demoserver.cpp
+++ b/src/demoserver.cpp
@@ -80,36 +80,35 @@ void DemoServer::recv_data()
QString in_data = QString::fromUtf8(client_sock->readAll());
// Copypasted from NetworkManager
- if (!in_data.endsWith("%")) {
- partial_packet = true;
- temp_packet += in_data;
- return;
- }
-
- else {
- if (partial_packet) {
- in_data = temp_packet + in_data;
- temp_packet = "";
- partial_packet = false;
- }
- }
-
- QStringList packet_list =
-#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
- in_data.split("%", QString::SplitBehavior(QString::SkipEmptyParts));
-#else
- in_data.split("%", Qt::SkipEmptyParts);
-#endif
+ const QStringList packet_list = in_data.split("%", Qt::SkipEmptyParts);
for (const QString &packet : packet_list) {
- AOPacket ao_packet(packet);
- handle_packet(ao_packet);
+ QStringList f_contents;
+ // Packet should *always* end with #
+ if (packet.endsWith("#")) {
+ f_contents = packet.chopped(1).split("#");
+ }
+ // But, if it somehow doesn't, we should still be able to handle it
+ else {
+ f_contents = packet.split("#");
+ }
+ // Empty packets are suspicious!
+ if (f_contents.isEmpty()) {
+ qWarning() << "WARNING: Empty packet received from server, skipping...";
+ continue;
+ }
+ // Take the first arg as the command
+ QString command = f_contents.takeFirst();
+ // The rest is contents of the packet
+ AOPacket *f_packet = new AOPacket(command, f_contents);
+ // Ship it to the server!
+ handle_packet(f_packet);
}
}
-void DemoServer::handle_packet(AOPacket packet)
+void DemoServer::handle_packet(AOPacket *p_packet)
{
- packet.net_decode();
+ p_packet->net_decode();
// This code is literally a barebones AO server
// It is wise to do it this way, because I can
@@ -119,8 +118,8 @@ void DemoServer::handle_packet(AOPacket packet)
// Also, at some point, I will make akashit
// into a shared library.
- QString header = packet.get_header();
- QStringList contents = packet.get_contents();
+ QString header = p_packet->get_header();
+ QStringList contents = p_packet->get_contents();
if (header == "HI") {
client_sock->write("ID#0#DEMOINTERNAL#0#%");
@@ -273,6 +272,8 @@ void DemoServer::handle_packet(AOPacket packet)
client_sock->write(packet.toUtf8());
}
}
+
+ delete p_packet;
}
void DemoServer::load_demo(QString filename)
@@ -390,15 +391,28 @@ void DemoServer::playback()
current_packet = demo_data.dequeue();
}
if (!demo_data.isEmpty()) {
- AOPacket wait_packet = AOPacket(current_packet);
-
- int duration = wait_packet.get_contents().at(0).toInt();
+ QStringList f_contents;
+ // Packet should *always* end with #
+ if (current_packet.endsWith("#")) {
+ f_contents = current_packet.chopped(1).split("#");
+ }
+ // But, if it somehow doesn't, we should still be able to handle it
+ else {
+ f_contents = current_packet.split("#");
+ }
+ // Take the first arg as the command
+ QString command = f_contents.takeFirst();
+ int duration = 0;
+ if (!f_contents.isEmpty()) {
+ duration = f_contents.at(0).toInt();
+ }
// Max wait reached
if (max_wait != -1 && duration + elapsed_time > max_wait) {
+ int prev_duration = duration;
duration = qMax(0, max_wait - elapsed_time);
qDebug() << "Max_wait of " << max_wait << " reached. Forcing duration to " << duration << "ms";
// Skip the difference on the timers
- emit skip_timers(wait_packet.get_contents().at(0).toInt() - duration);
+ emit skip_timers(prev_duration - duration);
}
// Manual user skip, such as with >
else if (timer->remainingTime() > 0) {