2016-08-16 16:51:22 +02:00
|
|
|
/*
|
2017-11-13 23:09:33 +01:00
|
|
|
* This file is part of the Flowee project
|
2026-04-09 18:56:13 +02:00
|
|
|
* Copyright (C) 2016-2026 Tom Zander <tom@flowee.org>
|
2016-08-16 16:51:22 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2018-02-15 19:50:12 +01:00
|
|
|
#ifndef APISERVER_H
|
|
|
|
|
#define APISERVER_H
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2021-02-17 22:49:19 +01:00
|
|
|
#include "NetProtect.h"
|
|
|
|
|
|
2019-04-09 17:24:38 +02:00
|
|
|
#include <networkmanager/NetworkManager.h>
|
|
|
|
|
#include <networkmanager/NetworkService.h>
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2018-02-10 22:48:07 +01:00
|
|
|
#include <univalue.h>
|
2016-08-16 16:51:22 +02:00
|
|
|
#include <vector>
|
2020-09-29 17:06:57 +02:00
|
|
|
#include <memory>
|
2021-02-16 22:08:16 +01:00
|
|
|
#include <mutex>
|
2016-08-16 16:51:22 +02:00
|
|
|
#include <string>
|
|
|
|
|
#include <list>
|
2026-04-09 18:56:13 +02:00
|
|
|
#include <boost/asio/steady_timer.hpp>
|
2020-09-29 17:06:57 +02:00
|
|
|
#include <boost/thread.hpp>
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2018-02-15 19:50:12 +01:00
|
|
|
namespace Api {
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2020-09-29 17:06:57 +02:00
|
|
|
class Parser;
|
|
|
|
|
class ASyncParser;
|
|
|
|
|
class SessionData;
|
2019-03-09 16:36:13 +01:00
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
class Server {
|
|
|
|
|
public:
|
2025-02-08 19:05:26 +01:00
|
|
|
Server(boost::asio::io_context &context);
|
2020-09-29 17:06:57 +02:00
|
|
|
~Server();
|
2018-02-17 14:27:49 +01:00
|
|
|
|
|
|
|
|
void addService(NetworkService *service);
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2020-09-29 17:06:57 +02:00
|
|
|
NetworkConnection copyConnection(const NetworkConnection &orig);
|
|
|
|
|
|
|
|
|
|
/// create a message stating that we had a failure.
|
|
|
|
|
Message createFailedMessage(const Message &origin, const std::string &failReason) const;
|
|
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
private:
|
|
|
|
|
void newConnection(NetworkConnection &connection);
|
|
|
|
|
void connectionRemoved(const EndPoint &endPoint);
|
2019-03-26 19:37:33 +01:00
|
|
|
void incomingMessage(const Message &message);
|
2016-08-16 16:51:22 +02:00
|
|
|
|
|
|
|
|
void checkConnections(boost::system::error_code error);
|
|
|
|
|
|
|
|
|
|
class Connection {
|
|
|
|
|
public:
|
2020-09-29 17:06:57 +02:00
|
|
|
Connection(Server *parent, NetworkConnection && connection);
|
2019-03-09 16:36:13 +01:00
|
|
|
~Connection();
|
2016-08-16 16:51:22 +02:00
|
|
|
void incomingMessage(const Message &message);
|
|
|
|
|
|
|
|
|
|
NetworkConnection m_connection;
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-29 17:06:57 +02:00
|
|
|
/// Handle a parser that just calls the old RPC code
|
2020-10-26 18:30:39 +01:00
|
|
|
void handleRpcParser(const std::unique_ptr<Parser> &parser, const Message &message);
|
2020-09-29 17:06:57 +02:00
|
|
|
/// Handle a parser that does the heavy lifting itself.
|
2020-10-26 18:30:39 +01:00
|
|
|
void handleMainParser(const std::unique_ptr<Parser> &parser, const Message &message);
|
2020-09-29 17:06:57 +02:00
|
|
|
/// Handles an async parser, asynchroniously.
|
2020-10-26 18:30:39 +01:00
|
|
|
void startASyncParser(std::unique_ptr<Parser> && parser);
|
2020-09-29 17:06:57 +02:00
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
void sendFailedMessage(const Message &origin, const std::string &failReason);
|
|
|
|
|
|
2020-09-29 17:06:57 +02:00
|
|
|
Server * const m_parent;
|
2019-03-09 16:36:13 +01:00
|
|
|
std::map<uint32_t, SessionData*> m_properties;
|
2020-10-26 15:11:28 +01:00
|
|
|
std::array<std::atomic_bool, 10> m_runningParsers;
|
2016-08-16 16:51:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct NewConnection {
|
|
|
|
|
NetworkConnection connection;
|
2021-02-18 10:06:25 +01:00
|
|
|
uint32_t initialConnectionTime;
|
2016-08-16 16:51:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NetworkManager m_networkManager;
|
2021-02-17 22:49:19 +01:00
|
|
|
NetProtect m_netProtect;
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2021-02-16 22:08:16 +01:00
|
|
|
mutable std::mutex m_mutex; // protects the next 4 vars.
|
2020-09-29 17:06:57 +02:00
|
|
|
std::list<Connection*> m_connections; // make this a list of shared_ptrs to Connection
|
2016-08-16 16:51:22 +02:00
|
|
|
std::list<NewConnection> m_newConnections;
|
|
|
|
|
bool m_timerRunning;
|
2026-04-09 18:56:13 +02:00
|
|
|
boost::asio::steady_timer m_newConnectionTimeout;
|
2016-08-16 16:51:22 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|