Revert "loolwsd: kill receiveFrame with char* and cleanup usage cases"

This reverts commit 45c1856c6a.

This patch requires a very bleeding edge Poco, reverting for now.
libreoffice-5-3
Michael Meeks 2016-11-25 09:46:01 +00:00
parent 47699cd908
commit a2058341a3
7 changed files with 72 additions and 49 deletions

View File

@ -89,24 +89,23 @@ public:
{
do
{
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
buffer.resize(0);
n = _ws.receiveFrame(buffer, flags);
char buffer[100000];
n = _ws.receiveFrame(buffer, sizeof(buffer), flags);
if (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE)
{
{
std::unique_lock<std::mutex> lock(coutMutex);
std::cout << "Got " << getAbbreviatedFrameDump(buffer.begin(), n, flags) << std::endl;
std::cout << "Got " << getAbbreviatedFrameDump(buffer, n, flags) << std::endl;
}
const std::string firstLine = getFirstLine(buffer.begin(), n);
std::string firstLine = getFirstLine(buffer, n);
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
if (std::getenv("DISPLAY") != nullptr && tokens[0] == "tile:")
{
TemporaryFile pngFile;
std::ofstream pngStream(pngFile.path(), std::ios::binary);
pngStream.write(buffer.begin() + firstLine.size() + 1, n - firstLine.size() - 1);
pngStream.write(buffer + firstLine.size() + 1, n - firstLine.size() - 1);
pngStream.close();
if (std::system((std::string("display ") + pngFile.path()).c_str()) == -1)
{

View File

@ -196,7 +196,7 @@ void shutdownLimitReached(LOOLWebSocket& ws)
{
int flags = 0;
int retries = 7;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
std::vector<char> buffer(READ_BUFFER_SIZE * 100);
const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000);
do
@ -214,8 +214,7 @@ void shutdownLimitReached(LOOLWebSocket& ws)
// Ignore incoming messages.
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
{
buffer.resize(0);
ws.receiveFrame(buffer, flags);
ws.receiveFrame(buffer.data(), buffer.capacity(), flags);
}
// Shutdown.

View File

@ -76,6 +76,39 @@ public:
{
}
/// Wrapper for Poco::Net::WebSocket::receiveFrame() that handles PING frames
/// (by replying with a PONG frame) and PONG frames. PONG frames are ignored.
/// Should we also factor out the handling of non-final and continuation frames into this?
int receiveFrame(char* buffer, const int length, int& flags)
{
#ifdef ENABLE_DEBUG
// Delay receiving the frame
std::this_thread::sleep_for(getWebSocketDelay());
#endif
// Timeout given is in microseconds.
static const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000);
while (poll(waitTime, Poco::Net::Socket::SELECT_READ))
{
const int n = Poco::Net::WebSocket::receiveFrame(buffer, length, flags);
if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PING)
{
sendFrame(buffer, n, WebSocket::FRAME_FLAG_FIN | WebSocket::FRAME_OP_PONG);
}
else if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PONG)
{
// In case we do send pongs in the future.
}
else
{
return n;
}
}
return -1;
}
/// Wrapper for Poco::Net::WebSocket::receiveFrame() that handles PING frames
/// (by replying with a PONG frame) and PONG frames. PONG frames are ignored.
/// Should we also factor out the handling of non-final and continuation frames into this?

View File

@ -44,16 +44,17 @@ namespace {
std::string readFontList(const std::shared_ptr<LOOLWebSocket> &socket)
{
int flags;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
char buffer[100 * 1000];
buffer.resize(0);
const int length = socket->receiveFrame(buffer, flags);
int length = socket->receiveFrame(buffer, sizeof (buffer), flags);
if (length > 0)
{
return std::string(buffer.begin(), length);
assert(length<(int)sizeof(buffer));
buffer[length] = '\0';
return std::string(buffer);
}
return std::string("read failure");
else
return std::string("read failure");
}
}

View File

@ -173,8 +173,7 @@ int getErrorCode(LOOLWebSocket& ws, std::string& message)
ws.setReceiveTimeout(timeout);
do
{
buffer.resize(0);
bytes = ws.receiveFrame(buffer, flags);
bytes = ws.receiveFrame(buffer.begin(), READ_BUFFER_SIZE, flags);
}
while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
@ -410,7 +409,7 @@ void SocketProcessor(const std::string& name,
const Poco::Timespan waitTime(timeoutMs * 1000);
int flags = 0;
int n = 0;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
char buffer[READ_BUFFER_SIZE];
do
{
if (!socket->poll(waitTime, Poco::Net::Socket::SELECT_READ))
@ -419,12 +418,11 @@ void SocketProcessor(const std::string& name,
break;
}
buffer.resize(0);
n = socket->receiveFrame(buffer, flags);
std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer.begin(), n, flags) << std::endl;
n = socket->receiveFrame(buffer, sizeof(buffer), flags);
std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, n, flags) << std::endl;
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
{
if (!handler(std::string(buffer.begin(), n)))
if (!handler(std::string(buffer, n)))
{
break;
}

View File

@ -164,12 +164,11 @@ void HTTPCrashTest::testCrashKit()
// receive close frame handshake
int bytes;
int flags;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
char buffer[READ_BUFFER_SIZE];
do
{
buffer.resize(0);
bytes = socket->receiveFrame(buffer, flags);
std::cerr << testname << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer.begin(), bytes, flags) << std::endl;
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
std::cerr << testname << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, bytes, flags) << std::endl;
}
while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
@ -177,7 +176,7 @@ void HTTPCrashTest::testCrashKit()
socket->shutdown();
// no more messages is received.
bytes = socket->receiveFrame(buffer, flags);
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
CPPUNIT_ASSERT_MESSAGE("Expected no more data", bytes <= 2); // The 2-byte marker is ok.
CPPUNIT_ASSERT_EQUAL(0x88, flags);
}

View File

@ -253,42 +253,37 @@ void HTTPWSTest::testHandShake()
socket.setReceiveTimeout(0);
int flags = 0;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
buffer.resize(0);
int bytes = socket.receiveFrame(buffer, flags);
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: find"), std::string(buffer.begin(), bytes));
char buffer[1024] = {0};
int bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: find"), std::string(buffer, bytes));
buffer.resize(0);
bytes = socket.receiveFrame(buffer, flags);
if (bytes > 0 && !std::strstr(buffer.begin(), "error:"))
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
if (bytes > 0 && !std::strstr(buffer, "error:"))
{
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: connect"), std::string(buffer.begin(), bytes));
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: connect"), std::string(buffer, bytes));
buffer.resize(0);
bytes = socket.receiveFrame(buffer, flags);
if (!std::strstr(buffer.begin(), "error:"))
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
if (!std::strstr(buffer, "error:"))
{
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: ready"), std::string(buffer.begin(), bytes));
CPPUNIT_ASSERT_EQUAL(std::string("statusindicator: ready"), std::string(buffer, bytes));
}
else
{
// check error message
CPPUNIT_ASSERT(std::strstr(SERVICE_UNAVAILABLE_INTERNAL_ERROR, buffer.begin()) != nullptr);
CPPUNIT_ASSERT(std::strstr(SERVICE_UNAVAILABLE_INTERNAL_ERROR, buffer) != nullptr);
// close frame message
buffer.resize(0);
bytes = socket.receiveFrame(buffer, flags);
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
CPPUNIT_ASSERT((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_CLOSE);
}
}
else
{
// check error message
CPPUNIT_ASSERT(std::strstr(SERVICE_UNAVAILABLE_INTERNAL_ERROR, buffer.begin()) != nullptr);
CPPUNIT_ASSERT(std::strstr(SERVICE_UNAVAILABLE_INTERNAL_ERROR, buffer) != nullptr);
// close frame message
buffer.resize(0);
bytes = socket.receiveFrame(buffer, flags);
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
CPPUNIT_ASSERT((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_CLOSE);
}
}
@ -314,16 +309,15 @@ void HTTPWSTest::testCloseAfterClose()
// receive close frame handshake
int bytes;
int flags;
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
char buffer[READ_BUFFER_SIZE];
do
{
buffer.resize(0);
bytes = socket->receiveFrame(buffer, flags);
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
}
while (bytes && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
// no more messages is received.
bytes = socket->receiveFrame(buffer, flags);
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
std::cerr << "Received " << bytes << " bytes, flags: "<< std::hex << flags << std::dec << std::endl;
CPPUNIT_ASSERT_EQUAL(0, bytes);
CPPUNIT_ASSERT_EQUAL(0, flags);