mirror of https://github.com/oxen-io/lokinet
initial epoll event loop implementation for tcp
parent
2e511dc414
commit
957a5ed833
@ -0,0 +1,14 @@
|
||||
#ifndef __ABYSS_JSON_JSON_HPP
|
||||
#define __ABYSS_JSON_JSON_HPP
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
namespace abyss
|
||||
{
|
||||
namespace json
|
||||
{
|
||||
typedef rapidjson::Document Object;
|
||||
}
|
||||
} // namespace abyss
|
||||
|
||||
#endif
|
@ -0,0 +1,74 @@
|
||||
#ifndef __ABYSS_SERVER_HPP__
|
||||
#define __ABYSS_SERVER_HPP__
|
||||
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/logic.h>
|
||||
#include <llarp/time.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <abyss/json.hpp>
|
||||
|
||||
namespace abyss
|
||||
{
|
||||
namespace http
|
||||
{
|
||||
struct ConnImpl;
|
||||
|
||||
struct IRPCHandler
|
||||
{
|
||||
typedef std::string Method_t;
|
||||
typedef abyss::json::Object Params;
|
||||
typedef abyss::json::Object Response;
|
||||
|
||||
IRPCHandler(ConnImpl* impl);
|
||||
|
||||
virtual bool
|
||||
HandleJSONRPC(const Method_t& method, const Params& params,
|
||||
Response& response) = 0;
|
||||
|
||||
~IRPCHandler();
|
||||
|
||||
bool
|
||||
ShouldClose(llarp_time_t now) const;
|
||||
|
||||
private:
|
||||
ConnImpl* m_Impl;
|
||||
};
|
||||
|
||||
struct BaseReqHandler
|
||||
{
|
||||
BaseReqHandler(llarp_time_t req_timeout);
|
||||
~BaseReqHandler();
|
||||
|
||||
bool
|
||||
ServeAsync(llarp_ev_loop* loop, llarp_logic* logic,
|
||||
const sockaddr* bindaddr);
|
||||
|
||||
void
|
||||
RemoveConn(IRPCHandler* handler);
|
||||
|
||||
protected:
|
||||
virtual IRPCHandler*
|
||||
CreateHandler(ConnImpl* connimpl) const = 0;
|
||||
|
||||
private:
|
||||
static void
|
||||
OnTick(llarp_tcp_acceptor*);
|
||||
|
||||
void
|
||||
Tick();
|
||||
|
||||
static void
|
||||
OnAccept(struct llarp_tcp_acceptor*, struct llarp_tcp_conn*);
|
||||
|
||||
llarp_ev_loop* m_loop;
|
||||
llarp_logic* m_Logic;
|
||||
llarp_tcp_acceptor m_acceptor;
|
||||
std::list< std::unique_ptr< IRPCHandler > > m_Conns;
|
||||
llarp_time_t m_ReqTimeout;
|
||||
};
|
||||
} // namespace http
|
||||
} // namespace abyss
|
||||
|
||||
#endif
|
@ -1,86 +1,5 @@
|
||||
#ifndef __LIB_ABYSS_HPP__
|
||||
#define __LIB_ABYSS_HPP__
|
||||
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/logic.h>
|
||||
#include <llarp/time.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#ifdef USE_RAPIDJSON
|
||||
#include <rapidjson/document.h>
|
||||
namespace json = rapidjson;
|
||||
#else
|
||||
namespace json
|
||||
{
|
||||
struct Document;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace abyss
|
||||
{
|
||||
namespace http
|
||||
{
|
||||
struct ConnImpl;
|
||||
|
||||
struct IRPCHandler
|
||||
{
|
||||
typedef std::string Method_t;
|
||||
typedef ::json::Document Params;
|
||||
typedef ::json::Document Response;
|
||||
|
||||
IRPCHandler(ConnImpl* impl);
|
||||
|
||||
virtual bool
|
||||
HandleJSONRPC(const Method_t& method, const Params& params,
|
||||
Response& response) = 0;
|
||||
|
||||
~IRPCHandler();
|
||||
|
||||
bool
|
||||
ShouldClose(llarp_time_t now) const;
|
||||
|
||||
private:
|
||||
ConnImpl* m_Impl;
|
||||
};
|
||||
|
||||
struct BaseReqHandler
|
||||
{
|
||||
BaseReqHandler(llarp_time_t req_timeout);
|
||||
~BaseReqHandler();
|
||||
|
||||
bool
|
||||
ServeAsync(llarp_ev_loop* loop, llarp_logic* logic,
|
||||
const sockaddr* bindaddr);
|
||||
|
||||
void
|
||||
RemoveConn(IRPCHandler* handler);
|
||||
|
||||
protected:
|
||||
virtual IRPCHandler*
|
||||
CreateHandler(ConnImpl* connimpl) const = 0;
|
||||
|
||||
private:
|
||||
void
|
||||
ScheduleTick(llarp_time_t ms);
|
||||
|
||||
static void
|
||||
OnTick(void* user, llarp_time_t orig, llarp_time_t left);
|
||||
|
||||
void
|
||||
Tick();
|
||||
|
||||
static void
|
||||
OnAccept(struct llarp_tcp_acceptor*, struct llarp_tcp_conn*);
|
||||
|
||||
llarp_ev_loop* m_loop;
|
||||
llarp_logic* m_Logic;
|
||||
llarp_tcp_acceptor m_acceptor;
|
||||
std::list< std::unique_ptr< IRPCHandler > > m_Conns;
|
||||
llarp_time_t m_ReqTimeout;
|
||||
};
|
||||
} // namespace http
|
||||
} // namespace abyss
|
||||
|
||||
#include <abyss/server.hpp>
|
||||
#include <abyss/json.hpp>
|
||||
#endif
|
||||
|
@ -0,0 +1,52 @@
|
||||
#include <libabyss.hpp>
|
||||
#include <llarp/net.hpp>
|
||||
#include <sys/signal.h>
|
||||
|
||||
struct DemoHandler : public abyss::http::IRPCHandler
|
||||
{
|
||||
DemoHandler(abyss::http::ConnImpl* impl) : abyss::http::IRPCHandler(impl)
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
HandleJSONRPC(const Method_t& method, const Params& params, Response& resp)
|
||||
{
|
||||
resp.SetObject().AddMember("test", "value", resp.GetAllocator());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
struct DemoServer : public abyss::http::BaseReqHandler
|
||||
{
|
||||
DemoServer() : abyss::http::BaseReqHandler(1000)
|
||||
{
|
||||
}
|
||||
|
||||
abyss::http::IRPCHandler*
|
||||
CreateHandler(abyss::http::ConnImpl* impl) const
|
||||
{
|
||||
return new DemoHandler(impl);
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
llarp_threadpool* threadpool = llarp_init_same_process_threadpool();
|
||||
llarp_ev_loop* loop = nullptr;
|
||||
llarp_ev_loop_alloc(&loop);
|
||||
llarp_logic* logic = llarp_init_single_process_logic(threadpool);
|
||||
sockaddr_in addr;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = htons(1222);
|
||||
addr.sin_family = AF_INET;
|
||||
DemoServer serv;
|
||||
llarp::Addr a(addr);
|
||||
llarp::LogInfo("bind to ", a);
|
||||
if(serv.ServeAsync(loop, logic, a))
|
||||
llarp_ev_loop_run_single_process(loop, threadpool, logic);
|
||||
else
|
||||
llarp::LogError("Failed to serve: ", strerror(errno));
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue