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"
|
2016-05-20 16:42:14 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2015-03-12 18:34:42 -05:00
|
|
|
#include "TileCache.hpp"
|
2016-05-20 16:42:14 -05:00
|
|
|
#include "Log.hpp"
|
2015-03-12 18:34:42 -05:00
|
|
|
|
|
|
|
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);
|
2016-05-01 19:32:59 -05:00
|
|
|
void sendBinaryFrame(const char *buffer, int length);
|
2015-04-20 09:43:31 -05:00
|
|
|
|
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-05-16 21:48:33 -05:00
|
|
|
void shutdown(Poco::UInt16 statusCode, const std::string& message)
|
|
|
|
{
|
2016-05-19 19:29:18 -05:00
|
|
|
if (_ws)
|
|
|
|
{
|
|
|
|
_ws->shutdown(statusCode, message);
|
|
|
|
}
|
2016-05-16 21:48:33 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 00:14:27 -05:00
|
|
|
bool isActive() const { return _isActive; }
|
2016-04-21 04:36:47 -05:00
|
|
|
void setIsActive(bool active) { _isActive = active; }
|
2016-04-09 16:47:23 -05:00
|
|
|
|
|
|
|
/// 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; }
|
|
|
|
|
2016-04-23 11:11:11 -05:00
|
|
|
Kind getKind() const { return _kind; }
|
|
|
|
|
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-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);
|
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
void updateLastActivityTime()
|
|
|
|
{
|
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:42:14 -05:00
|
|
|
template <typename T>
|
|
|
|
void forwardToPeer(T& p, const char *buffer, int length)
|
|
|
|
{
|
|
|
|
const auto message = LOOLProtocol::getAbbreviatedMessage(buffer, length);
|
|
|
|
|
|
|
|
auto peer = p.lock();
|
|
|
|
if (!peer)
|
|
|
|
{
|
|
|
|
throw Poco::ProtocolException(getName() + ": no peer to forward to: [" + message + "].");
|
|
|
|
}
|
|
|
|
else if (peer->isCloseFrame())
|
|
|
|
{
|
|
|
|
Log::trace(getName() + ": peer began the closing handshake. Dropping forward message [" + message + "].");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::trace(getName() + " -> " + peer->getName() + ": " + message);
|
|
|
|
peer->sendBinaryFrame(buffer, length);
|
|
|
|
}
|
|
|
|
|
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
|
2016-04-27 19:51:54 -05:00
|
|
|
bool _haveDocPassword;
|
2016-02-04 11:35:26 -06:00
|
|
|
|
|
|
|
// 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;
|
2016-04-21 00:14:27 -05:00
|
|
|
/// True if the user is active, otherwise false (switched tabs).
|
|
|
|
bool _isActive;
|
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: */
|