2019-04-24 23:27:31 +00:00
|
|
|
#ifndef ABYSS_SERVER_HPP
|
|
|
|
#define ABYSS_SERVER_HPP
|
2018-10-24 18:02:42 +00:00
|
|
|
|
2019-01-11 01:59:44 +00:00
|
|
|
#include <ev/ev.h>
|
2019-02-15 23:04:04 +00:00
|
|
|
#include <util/json.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/string_view.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2020-02-19 21:58:01 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <nonstd/optional.hpp>
|
|
|
|
|
2018-10-24 18:02:42 +00:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-10-25 17:03:25 +00:00
|
|
|
#include <unordered_map>
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
namespace abyss
|
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
namespace httpd
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
|
|
|
struct ConnImpl;
|
|
|
|
|
|
|
|
struct IRPCHandler
|
|
|
|
{
|
2018-11-22 23:59:03 +00:00
|
|
|
using Method_t = std::string;
|
2019-03-02 02:27:38 +00:00
|
|
|
using Params = nlohmann::json;
|
|
|
|
using Response = nlohmann::json;
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
IRPCHandler(ConnImpl* impl);
|
|
|
|
|
2020-02-19 21:58:01 +00:00
|
|
|
virtual Response
|
2019-03-02 02:27:38 +00:00
|
|
|
HandleJSONRPC(Method_t method, const Params& params) = 0;
|
2018-10-24 18:02:42 +00:00
|
|
|
|
2018-11-06 22:48:17 +00:00
|
|
|
virtual ~IRPCHandler();
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldClose(llarp_time_t now) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConnImpl* m_Impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BaseReqHandler
|
|
|
|
{
|
|
|
|
BaseReqHandler(llarp_time_t req_timeout);
|
2019-05-19 22:11:07 +00:00
|
|
|
virtual ~BaseReqHandler();
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-22 16:20:50 +00:00
|
|
|
ServeAsync(llarp_ev_loop_ptr loop, std::shared_ptr< llarp::Logic > logic,
|
2018-10-24 18:02:42 +00:00
|
|
|
const sockaddr* bindaddr);
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveConn(IRPCHandler* handler);
|
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
/// close the handler and acceptor
|
|
|
|
void
|
|
|
|
Close();
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
|
|
|
now() const
|
|
|
|
{
|
|
|
|
return llarp_ev_loop_time_now_ms(m_loop);
|
|
|
|
}
|
|
|
|
|
2018-10-24 18:02:42 +00:00
|
|
|
protected:
|
|
|
|
virtual IRPCHandler*
|
2018-11-02 18:02:45 +00:00
|
|
|
CreateHandler(ConnImpl* connimpl) = 0;
|
2018-10-24 18:02:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void
|
|
|
|
OnTick(llarp_tcp_acceptor*);
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick();
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnAccept(struct llarp_tcp_acceptor*, struct llarp_tcp_conn*);
|
|
|
|
|
2019-04-08 12:01:52 +00:00
|
|
|
llarp_ev_loop_ptr m_loop;
|
2019-05-22 16:20:50 +00:00
|
|
|
std::shared_ptr< llarp::Logic > m_Logic;
|
2018-10-24 18:02:42 +00:00
|
|
|
llarp_tcp_acceptor m_acceptor;
|
|
|
|
std::list< std::unique_ptr< IRPCHandler > > m_Conns;
|
|
|
|
llarp_time_t m_ReqTimeout;
|
|
|
|
};
|
2018-11-01 12:47:14 +00:00
|
|
|
} // namespace httpd
|
2018-10-24 18:02:42 +00:00
|
|
|
} // namespace abyss
|
|
|
|
|
|
|
|
#endif
|