2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-04 17:14:04 -06:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
#ifndef INCLUDED_LOOLSESSION_HPP
|
|
|
|
#define INCLUDED_LOOLSESSION_HPP
|
|
|
|
|
2016-04-18 18:12:26 -05:00
|
|
|
#include <atomic>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <cassert>
|
2015-04-22 13:35:52 -05:00
|
|
|
#include <memory>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <mutex>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <ostream>
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Buffer.h>
|
2015-04-16 11:15:40 -05:00
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/Process.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Types.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
#include "MessageQueue.hpp"
|
2015-03-12 18:34:42 -05:00
|
|
|
#include "TileCache.hpp"
|
|
|
|
|
|
|
|
class LOOLSession
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-12 18:34:42 -05:00
|
|
|
public:
|
2016-01-28 12:16:32 -06:00
|
|
|
/// We have three kinds of Websocket sessions
|
|
|
|
/// 1) Between the master loolwsd server to the end-user LOOL client
|
2016-03-18 08:25:23 -05:00
|
|
|
/// 2) Between the master loolwsd server and a jailed child process, in the master process
|
|
|
|
/// 3) Ditto, in the jailed process
|
2015-04-20 09:43:31 -05:00
|
|
|
enum class Kind { ToClient, ToPrisoner, ToMaster };
|
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
const std::string& getId() const { return _id; }
|
|
|
|
const std::string& getName() const { return _name; }
|
2016-01-21 08:26:34 -06:00
|
|
|
bool isDisconnected() const { return _disconnected; }
|
2015-12-27 21:47:39 -06:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
void sendTextFrame(const std::string& text);
|
|
|
|
|
2015-05-28 06:51:08 -05:00
|
|
|
virtual bool getStatus(const char *buffer, int length) = 0;
|
|
|
|
|
2015-09-09 12:23:49 -05:00
|
|
|
virtual bool getCommandValues(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
2015-08-18 13:01:05 -05:00
|
|
|
|
2015-09-29 05:27:45 -05:00
|
|
|
virtual bool getPartPageRectangles(const char *buffer, int length) = 0;
|
|
|
|
|
2015-12-25 12:51:32 -06:00
|
|
|
bool handleInput(const char *buffer, int length);
|
2015-06-09 10:04:46 -05:00
|
|
|
|
2016-01-21 08:26:34 -06:00
|
|
|
/// Invoked when we want to disconnect a session.
|
2016-04-13 07:56:23 -05:00
|
|
|
virtual void disconnect();
|
2016-01-21 08:26:34 -06:00
|
|
|
|
|
|
|
/// Called to handle disconnection command from socket.
|
2016-04-13 07:56:23 -05:00
|
|
|
virtual bool handleDisconnect();
|
2016-01-21 08:26:34 -06:00
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
bool isInactive() const { return getInactivityMS() >= InactivityThresholdMS; }
|
|
|
|
|
|
|
|
/// Returns the inactivity time of the client in milliseconds.
|
|
|
|
double getInactivityMS() const
|
|
|
|
{
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastActivityTime);
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:12:26 -05:00
|
|
|
void closeFrame() { _isCloseFrame = true; };
|
|
|
|
bool isCloseFrame() const { return _isCloseFrame; }
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
protected:
|
2015-12-27 21:47:39 -06:00
|
|
|
LOOLSession(const std::string& id, const Kind kind,
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> ws);
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual ~LOOLSession();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
void setId(const std::string& id)
|
|
|
|
{
|
|
|
|
_id = id;
|
|
|
|
_name = _kindString + '-' + id;
|
|
|
|
}
|
2015-07-20 04:54:08 -05:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
void sendBinaryFrame(const char *buffer, int length);
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-11-19 03:30:00 -06:00
|
|
|
/// Parses the options of the "load" command, shared between MasterProcessSession::loadDocument() and ChildProcessSession::loadDocument().
|
|
|
|
void parseDocOptions(const Poco::StringTokenizer& tokens, int& part, std::string& timestamp);
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
2015-04-16 11:15:40 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2016-01-14 06:00:04 -06:00
|
|
|
virtual void sendCombinedTiles(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
|
|
|
|
2015-11-27 08:13:25 -06:00
|
|
|
virtual void sendFontRendering(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
void updateLastActivityTime()
|
|
|
|
{
|
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// Fields common to sessions in master and jailed processes:
|
2015-04-16 11:15:40 -05:00
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
// Our kind signifies to what we are connected to.
|
|
|
|
const Kind _kind;
|
|
|
|
|
|
|
|
// The kind cached as a string.
|
|
|
|
const std::string _kindString;
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// In the master process, the websocket to the LOOL client or the jailed child process. In a
|
|
|
|
// jailed process, the websocket to the parent.
|
2015-05-28 08:42:38 -05:00
|
|
|
std::shared_ptr<Poco::Net::WebSocket> _ws;
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-06-05 05:05:51 -05:00
|
|
|
// The actual URL, also in the child, even if the child never accesses that.
|
2015-04-20 09:43:31 -05:00
|
|
|
std::string _docURL;
|
2015-06-05 08:12:06 -05:00
|
|
|
|
2016-01-06 23:33:54 -06:00
|
|
|
// The Jailed document path.
|
|
|
|
std::string _jailedFilePath;
|
|
|
|
|
2016-02-04 11:35:26 -06:00
|
|
|
// Password provided, if any, to open the document
|
|
|
|
std::string _docPassword;
|
|
|
|
|
|
|
|
// If password is provided or not
|
|
|
|
bool _isDocPasswordProvided;
|
|
|
|
|
|
|
|
// Whether document has been opened succesfuly
|
|
|
|
bool _isDocLoaded;
|
|
|
|
|
|
|
|
// Whether document is password protected
|
|
|
|
bool _isDocPasswordProtected;
|
|
|
|
|
2015-11-18 11:09:13 -06:00
|
|
|
/// Document options: a JSON string, containing options (rendering, also possibly load in the future).
|
|
|
|
std::string _docOptions;
|
|
|
|
|
2016-04-18 18:12:26 -05:00
|
|
|
// Whether websocket received close frame. Closing Handshake
|
|
|
|
std::atomic<bool> _isCloseFrame;
|
|
|
|
|
2015-12-25 12:51:32 -06:00
|
|
|
private:
|
|
|
|
|
|
|
|
virtual bool _handleInput(const char *buffer, int length) = 0;
|
|
|
|
|
2015-06-05 08:12:06 -05:00
|
|
|
private:
|
2016-01-21 08:26:34 -06:00
|
|
|
/// A session ID specific to an end-to-end connection (from user to lokit).
|
2015-12-27 21:47:39 -06:00
|
|
|
std::string _id;
|
2016-01-21 08:26:34 -06:00
|
|
|
/// A readable name that identifies our peer and ID.
|
2015-12-27 21:47:39 -06:00
|
|
|
std::string _name;
|
2016-01-21 08:26:34 -06:00
|
|
|
/// True if we have been disconnected.
|
|
|
|
bool _disconnected;
|
2015-12-27 21:47:39 -06:00
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
std::chrono::steady_clock::time_point _lastActivityTime;
|
|
|
|
|
2015-06-05 08:12:06 -05:00
|
|
|
std::mutex _mutex;
|
2016-04-09 16:47:23 -05:00
|
|
|
|
|
|
|
static constexpr auto InactivityThresholdMS = 120 * 1000;
|
2015-04-20 09:43:31 -05:00
|
|
|
};
|
2015-03-09 10:33:53 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
template<typename charT, typename traits>
|
|
|
|
inline std::basic_ostream<charT, traits> & operator <<(std::basic_ostream<charT, traits> & stream, LOOLSession::Kind kind)
|
|
|
|
{
|
|
|
|
switch (kind)
|
|
|
|
{
|
|
|
|
case LOOLSession::Kind::ToClient:
|
|
|
|
return stream << "TO_CLIENT";
|
|
|
|
case LOOLSession::Kind::ToPrisoner:
|
|
|
|
return stream << "TO_PRISONER";
|
|
|
|
case LOOLSession::Kind::ToMaster:
|
|
|
|
return stream << "TO_MASTER";
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return stream << "UNK_" + std::to_string(static_cast<int>(kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
#endif
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|