lokinet/pybind/llarp/handlers/pyhandler.hpp

82 lines
2.2 KiB
C++
Raw Normal View History

2020-03-07 01:20:11 +00:00
#pragma once
2020-02-28 18:44:27 +00:00
#include "common.hpp"
#include "llarp.hpp"
#include "service/context.hpp"
#include "service/endpoint.hpp"
#include "router/abstractrouter.hpp"
2020-03-07 01:20:11 +00:00
namespace llarp
2020-02-28 18:44:27 +00:00
{
2020-03-07 01:20:11 +00:00
namespace handlers
2020-02-28 18:44:27 +00:00
{
using Context_ptr = std::shared_ptr<llarp::Context>;
2020-02-28 18:44:27 +00:00
struct PythonEndpoint final : public llarp::service::Endpoint,
public std::enable_shared_from_this<PythonEndpoint>
2020-02-28 18:44:27 +00:00
{
PythonEndpoint(std::string name, Context_ptr routerContext)
2020-03-07 01:20:11 +00:00
: llarp::service::Endpoint(
routerContext->router.get(), &routerContext->router->hiddenServiceContext())
, OurName(std::move(name))
2020-02-28 18:44:27 +00:00
{
}
const std::string OurName;
bool
HandleInboundPacket(
const service::ConvoTag tag,
const llarp_buffer_t& pktbuf,
service::ProtocolType proto) override
2020-02-28 18:44:27 +00:00
{
if (handlePacket)
2020-02-28 18:44:27 +00:00
{
AlignedBuffer<32> addr;
2020-02-28 18:44:27 +00:00
bool isSnode = false;
if (not GetEndpointWithConvoTag(tag, addr, isSnode))
2020-02-28 18:44:27 +00:00
return false;
if (isSnode)
2020-02-28 18:44:27 +00:00
return true;
std::vector<byte_t> pkt;
2020-02-28 18:44:27 +00:00
pkt.resize(pktbuf.sz);
std::copy_n(pktbuf.base, pktbuf.sz, pkt.data());
handlePacket(service::Address(addr), std::move(pkt), proto);
}
return true;
}
path::PathSet_ptr
GetSelf() override
{
return shared_from_this();
}
bool
SupportsV6() const override
{
return false;
}
using PacketHandler_t =
std::function<void(service::Address, std::vector<byte_t>, service::ProtocolType)>;
2020-02-28 18:44:27 +00:00
PacketHandler_t handlePacket;
2020-03-07 01:20:11 +00:00
void
SendPacket(service::Address remote, std::vector<byte_t> pkt, service::ProtocolType proto)
2020-02-28 18:44:27 +00:00
{
LogicCall(m_router->logic(), [remote, pkt, proto, self = shared_from_this()]() {
self->SendToServiceOrQueue(remote, llarp_buffer_t(pkt), proto);
});
2020-02-28 18:44:27 +00:00
}
2020-03-07 01:20:11 +00:00
std::string
GetOurAddress() const
2020-02-28 18:44:27 +00:00
{
return m_Identity.pub.Addr().ToString();
}
};
using PythonEndpoint_ptr = std::shared_ptr<PythonEndpoint>;
2020-03-07 01:20:11 +00:00
} // namespace handlers
2020-04-22 20:04:47 +00:00
} // namespace llarp