From a69b1d0c580bc779740ef79a7d16b69229896785 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Jan 2023 20:31:24 +0100 Subject: Client to Server: send diffs instead of whole file --- whiteboard.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'whiteboard.cpp') diff --git a/whiteboard.cpp b/whiteboard.cpp index b84f7cc..059571d 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -39,6 +39,7 @@ #include "libreichwein/xml.h" #include "config.h" +#include "diff.h" #include "qrcode.h" #include "storage.h" @@ -151,20 +152,27 @@ std::string Whiteboard::handle_request(Whiteboard::connection& c, const std::str if (command == "modify") { std::string id {xml.get("request.id")}; - std::string data {xml.get("request.data")}; - if (m_storage->getDocument(id) != data) { - m_storage->setDocument(id, data); - m_registry.setId(c, id); - notify_other_connections_file(c, id); - - int pos {xml.get("request.pos")}; - if (m_storage->getCursorPos(id) != pos) { - m_storage->setCursorPos(id, pos); - notify_other_connections_pos(c, id); - } - return make_xml({{"type", "modify"}, {"revision", std::to_string(m_storage->getRevision(id)) }}); + + int baserev {xml.get("request.baserev")}; + if (baserev != m_storage->getRevision(id)) + return make_xml({{"type", "error"}, {"message", "Bad base revision ("s + std::to_string(baserev) + "). Current: "s + std::to_string(m_storage->getRevision(id)) }}); + + pt::ptree ptree; + ptree.put_child("diff", xml.get_child("request.diff")); + Diff d{ptree}; + std::string data {m_storage->getDocument(id)}; + data = d.apply(data); + + m_storage->setDocument(id, data); + m_registry.setId(c, id); + notify_other_connections_file(c, id); + + int pos {xml.get("request.pos")}; + if (m_storage->getCursorPos(id) != pos) { + m_storage->setCursorPos(id, pos); + notify_other_connections_pos(c, id); } - return {}; + return make_xml({{"type", "modify"}, {"revision", std::to_string(m_storage->getRevision(id)) }}); } else if (command == "cursorpos") { std::string id {xml.get("request.id")}; int pos {xml.get("request.pos")}; -- cgit v1.2.3