2015-11-09 04:36:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MessageQueue.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2016-09-20 18:59:06 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2016-11-24 08:56:06 -06:00
|
|
|
#include <Protocol.hpp>
|
2016-09-01 15:45:04 -05:00
|
|
|
#include <Log.hpp>
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
#include <TileDesc.hpp>
|
2016-09-01 15:45:04 -05:00
|
|
|
|
2016-09-20 18:59:06 -05:00
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
void TileQueue::put_impl(const Payload& value)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-14 16:42:50 -05:00
|
|
|
const auto msg = std::string(value.data(), value.size());
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
const std::string firstToken = LOOLProtocol::getFirstToken(value);
|
2016-09-20 18:59:06 -05:00
|
|
|
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
if (firstToken == "canceltiles")
|
2016-09-20 18:59:06 -05:00
|
|
|
{
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("Processing [" << msg << "]. Before canceltiles have " << _queue.size() << " in queue.");
|
2016-09-20 18:59:06 -05:00
|
|
|
const auto seqs = msg.substr(12);
|
|
|
|
StringTokenizer tokens(seqs, ",", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
_queue.erase(std::remove_if(_queue.begin(), _queue.end(),
|
|
|
|
[&tokens](const Payload& v)
|
|
|
|
{
|
|
|
|
const std::string s(v.data(), v.size());
|
2016-11-01 17:40:11 -05:00
|
|
|
// Tile is for a thumbnail, don't cancel it
|
|
|
|
if (s.find("id=") != std::string::npos)
|
|
|
|
return false;
|
2016-09-20 18:59:06 -05:00
|
|
|
for (size_t i = 0; i < tokens.count(); ++i)
|
|
|
|
{
|
|
|
|
if (s.find("ver=" + tokens[i]) != std::string::npos)
|
|
|
|
{
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("Matched " << tokens[i] << ", Removing [" << s << "]");
|
2016-09-20 18:59:06 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}), _queue.end());
|
|
|
|
|
|
|
|
// Don't push canceltiles into the queue.
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("After canceltiles have " << _queue.size() << " in queue.");
|
2016-09-20 18:59:06 -05:00
|
|
|
return;
|
|
|
|
}
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
else if (firstToken == "tilecombine")
|
2016-09-25 15:04:27 -05:00
|
|
|
{
|
2016-09-26 03:35:20 -05:00
|
|
|
// Breakup tilecombine and deduplicate (we are re-combining the tiles
|
|
|
|
// in the get_impl() again)
|
2016-09-25 15:04:27 -05:00
|
|
|
const auto tileCombined = TileCombined::parse(msg);
|
|
|
|
for (auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
2016-09-26 03:35:20 -05:00
|
|
|
const std::string newMsg = tile.serialize("tile");
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
removeTileDuplicate(newMsg);
|
2016-09-26 03:35:20 -05:00
|
|
|
|
|
|
|
MessageQueue::put_impl(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
return;
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
else if (firstToken == "tile")
|
2016-09-26 03:35:20 -05:00
|
|
|
{
|
2017-01-20 08:56:47 -06:00
|
|
|
removeTileDuplicate(msg);
|
2016-09-23 03:18:54 -05:00
|
|
|
|
2016-09-26 03:35:20 -05:00
|
|
|
MessageQueue::put_impl(value);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-20 08:56:47 -06:00
|
|
|
else if (firstToken == "callback")
|
|
|
|
{
|
|
|
|
removeCallbackDuplicate(msg);
|
2016-09-26 03:35:20 -05:00
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
MessageQueue::put_impl(value);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
|
|
|
|
MessageQueue::put_impl(value);
|
|
|
|
}
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
void TileQueue::removeTileDuplicate(const std::string& tileMsg)
|
2016-09-26 03:35:20 -05:00
|
|
|
{
|
2016-11-26 21:24:35 -06:00
|
|
|
assert(LOOLProtocol::matchPrefix("tile", tileMsg, /*ignoreWhitespace*/ true));
|
2016-09-26 03:35:20 -05:00
|
|
|
|
2016-11-22 23:21:53 -06:00
|
|
|
// Ver is always provided at this point and it is necessary to
|
|
|
|
// return back to clients the last rendered version of a tile
|
|
|
|
// in case there are new invalidations and requests while rendering.
|
|
|
|
// Here we compare duplicates without 'ver' since that's irrelevant.
|
|
|
|
auto newMsgPos = tileMsg.find(" ver");
|
|
|
|
if (newMsgPos == std::string::npos)
|
|
|
|
{
|
|
|
|
newMsgPos = tileMsg.size() - 1;
|
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
|
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-26 03:35:20 -05:00
|
|
|
auto& it = _queue[i];
|
2016-11-22 23:21:53 -06:00
|
|
|
if (it.size() > newMsgPos &&
|
|
|
|
strncmp(tileMsg.data(), it.data(), newMsgPos) == 0)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2017-01-20 08:56:47 -06:00
|
|
|
LOG_TRC("Remove duplicate tile request: " << std::string(it.data(), it.size()) << " -> " << tileMsg);
|
2016-09-26 03:35:20 -05:00
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
}
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Read the viewId from the tokens.
|
|
|
|
std::string extractViewId(const std::string& origMsg, const std::vector<std::string> tokens)
|
|
|
|
{
|
|
|
|
size_t nonJson = tokens[0].size() + tokens[1].size() + tokens[2].size() + 3; // including spaces
|
|
|
|
std::string jsonString(origMsg.data() + nonJson, origMsg.size() - nonJson);
|
|
|
|
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
const auto result = parser.parse(jsonString);
|
|
|
|
const auto& json = result.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
return json->get("viewId").toString();
|
|
|
|
}
|
|
|
|
|
2017-01-25 05:21:38 -06:00
|
|
|
/// Extract the .uno: command ID from the potential command.
|
|
|
|
std::string extractUnoCommand(const std::string& command)
|
|
|
|
{
|
|
|
|
if (!LOOLProtocol::matchPrefix(".uno:", command))
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
size_t equalPos = command.find('=');
|
|
|
|
if (equalPos != std::string::npos)
|
|
|
|
return command.substr(0, equalPos);
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void TileQueue::removeCallbackDuplicate(const std::string& callbackMsg)
|
|
|
|
{
|
|
|
|
assert(LOOLProtocol::matchPrefix("callback", callbackMsg, /*ignoreWhitespace*/ true));
|
|
|
|
|
|
|
|
std::vector<std::string> tokens = LOOLProtocol::tokenize(callbackMsg);
|
|
|
|
|
|
|
|
if (tokens.size() < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// the message is "callback <view> <id> ..."
|
|
|
|
const std::string& callbackType = tokens[2];
|
2017-01-24 05:40:30 -06:00
|
|
|
|
|
|
|
if (callbackType == "0") // invalidation
|
|
|
|
{
|
2017-01-25 05:21:38 -06:00
|
|
|
// TODO later add a more advanced merging of invalidates (like two
|
|
|
|
// close ones merge into a bigger one); but for the moment remove just
|
|
|
|
// the plain duplicates
|
2017-01-24 05:40:30 -06:00
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = _queue[i];
|
|
|
|
|
|
|
|
if (callbackMsg.size() == it.size() && LOOLProtocol::matchPrefix(callbackMsg, it))
|
|
|
|
{
|
|
|
|
LOG_TRC("Remove duplicate invalidation callback: " << std::string(it.data(), it.size()) << " -> " << callbackMsg);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 05:21:38 -06:00
|
|
|
else if (callbackType == "8") // state changed
|
|
|
|
{
|
|
|
|
if (tokens.size() < 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string unoCommand = extractUnoCommand(tokens[3]);
|
|
|
|
if (unoCommand.length() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// remove obsolete states of the same .uno: command
|
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = _queue[i];
|
|
|
|
|
|
|
|
std::vector<std::string> queuedTokens = LOOLProtocol::tokenize(it.data(), it.size());
|
|
|
|
if (queuedTokens.size() < 4)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (queuedTokens[0] == tokens[0] && queuedTokens[1] == tokens[1] && queuedTokens[2] == tokens[2])
|
|
|
|
{
|
|
|
|
// callback, the same target, state changed; now check it's
|
|
|
|
// the same .uno: command
|
|
|
|
std::string queuedUnoCommand = extractUnoCommand(queuedTokens[3]);
|
|
|
|
if (queuedUnoCommand.length() == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (unoCommand == queuedUnoCommand)
|
|
|
|
{
|
|
|
|
LOG_TRC("Remove obsolete uno command: " << std::string(it.data(), it.size()) << " -> " << callbackMsg);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 05:40:30 -06:00
|
|
|
else if (callbackType == "1" || // the cursor has moved
|
2017-01-20 08:56:47 -06:00
|
|
|
callbackType == "5" || // the cursor visibility has changed
|
|
|
|
callbackType == "17" || // the cell cursor has moved
|
|
|
|
callbackType == "24" || // the view cursor has moved
|
|
|
|
callbackType == "26" || // the view cell cursor has moved
|
|
|
|
callbackType == "28") // the view cursor visibility has changed
|
|
|
|
{
|
|
|
|
bool isViewCallback = (callbackType == "24" || callbackType == "26" || callbackType == "28");
|
|
|
|
|
|
|
|
std::string viewId;
|
|
|
|
if (isViewCallback)
|
|
|
|
{
|
|
|
|
viewId = extractViewId(callbackMsg, tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = _queue[i];
|
|
|
|
|
|
|
|
// skip non-callbacks quickly
|
|
|
|
if (!LOOLProtocol::matchPrefix("callback", it))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<std::string> queuedTokens = LOOLProtocol::tokenize(it.data(), it.size());
|
|
|
|
if (queuedTokens.size() < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!isViewCallback && (queuedTokens[1] == tokens[1] && queuedTokens[2] == tokens[2]))
|
|
|
|
{
|
|
|
|
LOG_TRC("Remove obsolete callback: " << std::string(it.data(), it.size()) << " -> " << callbackMsg);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (isViewCallback && (queuedTokens[1] == tokens[1] && queuedTokens[2] == tokens[2]))
|
|
|
|
{
|
|
|
|
// we additionally need to ensure that the payload is about
|
|
|
|
// the same viewid (otherwise we'd merge them all views into
|
|
|
|
// one)
|
|
|
|
const std::string queuedViewId = extractViewId(std::string(it.data(), it.size()), queuedTokens);
|
|
|
|
|
|
|
|
if (viewId == queuedViewId)
|
|
|
|
{
|
|
|
|
LOG_TRC("Remove obsolete view callback: " << std::string(it.data(), it.size()) << " -> " << callbackMsg);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
int TileQueue::priority(const std::string& tileMsg)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
|
|
|
auto tile = TileDesc::parse(tileMsg); //FIXME: Expensive, avoid.
|
|
|
|
|
2016-09-30 15:38:20 -05:00
|
|
|
for (int i = static_cast<int>(_viewOrder.size()) - 1; i >= 0; --i)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
2016-09-30 15:00:20 -05:00
|
|
|
auto& cursor = _cursorPositions[_viewOrder[i]];
|
2016-09-23 03:37:33 -05:00
|
|
|
if (tile.intersectsWithRect(cursor.X, cursor.Y, cursor.Width, cursor.Height))
|
2016-09-30 15:00:20 -05:00
|
|
|
return i;
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
return -1;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
int TileQueue::findFirstNonPreview(bool preferTiles) const
|
2016-10-04 07:23:42 -05:00
|
|
|
{
|
2016-11-29 20:55:44 -06:00
|
|
|
int firstTile = -1;
|
|
|
|
int firstElse = -1;
|
2016-10-04 07:23:42 -05:00
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
2016-11-29 20:55:44 -06:00
|
|
|
const auto& front = _queue[i];
|
|
|
|
const bool isTile = LOOLProtocol::matchPrefix("tile", front);
|
|
|
|
//LOG_WRN("#" << i << " " << (isTile ? "isTile" : "non-tile"));
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
if (isTile && firstTile < 0)
|
|
|
|
{
|
|
|
|
const std::string msg(front.data(), front.size());
|
|
|
|
std::string id;
|
|
|
|
const bool isPreview = LOOLProtocol::getTokenStringFromMessage(msg, "id", id);
|
|
|
|
//LOG_WRN("#" << i << " " << (isPreview ? "isPreview" : "isTile") << ": " << msg);
|
|
|
|
if (!isPreview)
|
|
|
|
{
|
|
|
|
firstTile = i;
|
|
|
|
//LOG_WRN("firstTile: #" << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!isTile && firstElse < 0)
|
|
|
|
{
|
|
|
|
firstElse = i;
|
|
|
|
//LOG_WRN("firstElse: #" << i);
|
|
|
|
}
|
|
|
|
else if (firstTile >=0 && firstElse >= 0)
|
2016-11-16 07:47:11 -06:00
|
|
|
{
|
2016-10-04 07:23:42 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-11-29 20:55:44 -06:00
|
|
|
}
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
if (preferTiles && firstTile >= 0)
|
|
|
|
{
|
|
|
|
return firstTile;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstElse >= 0)
|
|
|
|
{
|
|
|
|
return firstElse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstTile >= 0)
|
|
|
|
{
|
|
|
|
return firstTile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TileQueue::bumpToTop(const size_t index)
|
|
|
|
{
|
|
|
|
if (index > 0)
|
|
|
|
{
|
|
|
|
Payload payload(_queue[index]);
|
|
|
|
//LOG_WRN("Bumping: " << std::string(payload.data(), payload.size()));
|
|
|
|
|
|
|
|
_queue.erase(_queue.begin() + index);
|
|
|
|
_queue.insert(_queue.begin(), payload);
|
2016-10-04 07:23:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
void TileQueue::updateTimestamps(const bool isTile)
|
|
|
|
{
|
|
|
|
if (isTile)
|
|
|
|
{
|
|
|
|
_lastGetTile = true;
|
|
|
|
_lastTileGetTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
else if (_lastGetTile)
|
|
|
|
{
|
|
|
|
// Update non-tile timestamp when switching from tiles.
|
|
|
|
_lastGetTile = false;
|
|
|
|
_lastGetTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TileQueue::shouldPreferTiles() const
|
|
|
|
{
|
|
|
|
if (_lastGetTile)
|
|
|
|
{
|
|
|
|
// If we had just done a tile, do something else.
|
|
|
|
LOG_TRC("Last was tile, doing non-tiles.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check how long it's been since we'd done tiles.
|
|
|
|
const auto tileDuration = (_lastGetTime - _lastTileGetTime);
|
|
|
|
const auto tileDurationMs = std::chrono::duration_cast<std::chrono::milliseconds>(tileDuration).count();
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastGetTime);
|
|
|
|
const auto durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
LOG_TRC("Tile duration: " << tileDurationMs << "ms, nonTile duration: " << durationMs << "ms.");
|
|
|
|
|
|
|
|
if (durationMs > MaxTileSkipDurationMs)
|
|
|
|
{
|
|
|
|
LOG_TRC("Capping non-tiles to 100ms. Prefer tiles now.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (durationMs > tileDurationMs)
|
|
|
|
{
|
|
|
|
LOG_TRC("Capping non-tiles to tileDurationMs (" << tileDurationMs << "). Prefer tiles now.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can still do some more non-tiles.
|
|
|
|
LOG_TRC("Have time for more non-tiles.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
TileQueue::Payload TileQueue::get_impl()
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-11-29 20:55:44 -06:00
|
|
|
LOG_TRC("MessageQueue depth: " << _queue.size());
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
auto front = _queue.front();
|
|
|
|
bool isTileFirst = LOOLProtocol::matchPrefix("tile", front);
|
|
|
|
//LOG_WRN("isTileFirst: " << isTileFirst);
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
if (_queue.size() == 1)
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-11-29 20:55:44 -06:00
|
|
|
updateTimestamps(isTileFirst);
|
|
|
|
|
|
|
|
//const auto msg = std::string(front.data(), front.size());
|
|
|
|
//LOG_TRC("MessageQueue res only: " << msg);
|
2016-11-26 21:47:17 -06:00
|
|
|
_queue.erase(_queue.begin());
|
2016-11-29 20:55:44 -06:00
|
|
|
return front;
|
|
|
|
}
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
// Drain callbacks as soon and as fast as possible.
|
|
|
|
if (!isTileFirst && LOOLProtocol::matchPrefix("callback", front))
|
|
|
|
{
|
|
|
|
updateTimestamps(false);
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
//const auto msg = std::string(front.data(), front.size());
|
|
|
|
//LOG_TRC("MessageQueue res call: " << msg);
|
|
|
|
_queue.erase(_queue.begin());
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Try draining all callbacks first.
|
|
|
|
|
|
|
|
const bool preferTiles = shouldPreferTiles();
|
|
|
|
const int nonPreviewIndex = findFirstNonPreview(preferTiles);
|
|
|
|
//LOG_WRN("First non-preview: " << nonPreviewIndex);
|
|
|
|
if (nonPreviewIndex < 0)
|
|
|
|
{
|
|
|
|
// We are left with previews only.
|
|
|
|
updateTimestamps(true); // We're doing a tile.
|
|
|
|
|
|
|
|
//const auto msg = std::string(front.data(), front.size());
|
|
|
|
//LOG_TRC("MessageQueue res prev: " << msg);
|
|
|
|
_queue.erase(_queue.begin());
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
|
|
|
bumpToTop(nonPreviewIndex);
|
|
|
|
front = _queue.front();
|
|
|
|
isTileFirst = LOOLProtocol::matchPrefix("tile", front);
|
|
|
|
//LOG_WRN("New front: " << std::string(front.data(), front.size()));
|
|
|
|
|
|
|
|
if (!isTileFirst)
|
|
|
|
{
|
|
|
|
updateTimestamps(false);
|
|
|
|
|
|
|
|
//const auto msg = std::string(front.data(), front.size());
|
|
|
|
//LOG_TRC("MessageQueue res call: " << msg);
|
|
|
|
_queue.erase(_queue.begin());
|
2016-09-15 07:40:26 -05:00
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
2016-09-23 03:18:54 -05:00
|
|
|
// We are handling a tile; first try to find one that is at the cursor's
|
|
|
|
// position, otherwise handle the one that is at the front
|
2016-09-30 15:00:20 -05:00
|
|
|
int prioritized = 0;
|
|
|
|
int prioritySoFar = -1;
|
2016-11-29 20:55:44 -06:00
|
|
|
std::string msg(front.data(), front.size());
|
2016-09-23 03:18:54 -05:00
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = _queue[i];
|
|
|
|
const std::string prio(it.data(), it.size());
|
2016-09-26 03:12:45 -05:00
|
|
|
|
|
|
|
// avoid starving - stop the search when we reach a non-tile,
|
|
|
|
// otherwise we may keep growing the queue of unhandled stuff (both
|
|
|
|
// tiles and non-tiles)
|
2016-11-29 20:55:44 -06:00
|
|
|
std::string id;
|
2016-11-26 21:24:35 -06:00
|
|
|
if (!LOOLProtocol::matchPrefix("tile", prio) ||
|
2016-11-16 07:47:11 -06:00
|
|
|
LOOLProtocol::getTokenStringFromMessage(prio, "id", id))
|
|
|
|
{
|
2016-09-26 03:12:45 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-09-26 03:12:45 -05:00
|
|
|
|
2016-11-16 07:47:11 -06:00
|
|
|
const int p = priority(prio);
|
2016-09-30 15:38:20 -05:00
|
|
|
if (p > prioritySoFar)
|
2016-09-23 03:18:54 -05:00
|
|
|
{
|
2016-09-30 15:00:20 -05:00
|
|
|
prioritySoFar = p;
|
|
|
|
prioritized = i;
|
2016-09-23 03:18:54 -05:00
|
|
|
msg = prio;
|
2016-09-30 16:06:26 -05:00
|
|
|
|
|
|
|
// found the highest priority already?
|
|
|
|
if (prioritySoFar == static_cast<int>(_viewOrder.size()) - 1)
|
2016-11-16 07:47:11 -06:00
|
|
|
{
|
2016-09-30 16:06:26 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-09-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
_queue.erase(_queue.begin() + prioritized);
|
2016-09-23 03:18:54 -05:00
|
|
|
|
2016-10-04 07:23:42 -05:00
|
|
|
std::vector<TileDesc> tiles;
|
2016-09-15 07:40:26 -05:00
|
|
|
tiles.emplace_back(TileDesc::parse(msg));
|
|
|
|
|
|
|
|
// Combine as many tiles as possible with the top one.
|
2016-09-25 20:30:56 -05:00
|
|
|
for (size_t i = 0; i < _queue.size(); )
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-09-25 20:30:56 -05:00
|
|
|
auto& it = _queue[i];
|
|
|
|
msg = std::string(it.data(), it.size());
|
2016-11-29 20:55:44 -06:00
|
|
|
std::string id;
|
2016-11-26 21:24:35 -06:00
|
|
|
if (!LOOLProtocol::matchPrefix("tile", msg) ||
|
2016-11-16 07:47:11 -06:00
|
|
|
LOOLProtocol::getTokenStringFromMessage(msg, "id", id))
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-09-25 20:30:56 -05:00
|
|
|
// Don't combine non-tiles or tiles with id.
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-09-25 20:30:56 -05:00
|
|
|
auto tile2 = TileDesc::parse(msg);
|
2016-11-29 20:55:44 -06:00
|
|
|
//LOG_TRC("Combining candidate: " << msg);
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-09-25 20:30:56 -05:00
|
|
|
// Check if it's on the same row.
|
|
|
|
if (tiles[0].onSameRow(tile2))
|
|
|
|
{
|
|
|
|
tiles.emplace_back(tile2);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
2016-09-20 18:20:23 -05:00
|
|
|
}
|
2016-09-15 07:40:26 -05:00
|
|
|
}
|
2016-09-20 18:20:23 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
updateTimestamps(true);
|
|
|
|
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("Combined " << tiles.size() << " tiles, leaving " << _queue.size() << " in queue.");
|
2016-09-15 07:40:26 -05:00
|
|
|
|
|
|
|
if (tiles.size() == 1)
|
|
|
|
{
|
|
|
|
msg = tiles[0].serialize("tile");
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("MessageQueue res: " << msg);
|
2016-09-15 07:40:26 -05:00
|
|
|
return Payload(msg.data(), msg.data() + msg.size());
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
const auto tileCombined = TileCombined::create(tiles).serialize("tilecombine");
|
2016-11-16 07:46:58 -06:00
|
|
|
LOG_TRC("MessageQueue res: " << tileCombined);
|
2016-09-15 07:40:26 -05:00
|
|
|
return Payload(tileCombined.data(), tileCombined.data() + tileCombined.size());
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|