lokinet/libabyss/include/abyss/server.hpp

88 lines
1.7 KiB
C++
Raw Normal View History

#ifndef ABYSS_SERVER_HPP
#define ABYSS_SERVER_HPP
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-11 01:59:44 +00:00
#include <util/logic.hpp>
#include <util/string_view.hpp>
#include <util/time.hpp>
#include <absl/types/optional.h>
#include <list>
#include <memory>
#include <string>
2018-10-25 17:03:25 +00:00
#include <unordered_map>
namespace abyss
{
2018-11-01 12:47:14 +00:00
namespace httpd
{
struct ConnImpl;
struct IRPCHandler
{
using Method_t = std::string;
using Params = nlohmann::json;
using Response = nlohmann::json;
IRPCHandler(ConnImpl* impl);
virtual absl::optional< Response >
HandleJSONRPC(Method_t method, const Params& params) = 0;
virtual ~IRPCHandler();
bool
ShouldClose(llarp_time_t now) const;
private:
ConnImpl* m_Impl;
};
struct BaseReqHandler
{
BaseReqHandler(llarp_time_t req_timeout);
~BaseReqHandler();
bool
2019-04-08 12:01:52 +00:00
ServeAsync(llarp_ev_loop_ptr loop, llarp::Logic* logic,
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);
}
protected:
virtual IRPCHandler*
CreateHandler(ConnImpl* connimpl) = 0;
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;
2018-12-10 14:14:55 +00:00
llarp::Logic* m_Logic;
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
} // namespace abyss
#endif