Skip to content

Commit

Permalink
[feature] Update Protocol/Handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
Neels99 committed Feb 13, 2024
1 parent 7d32298 commit 8b3ef46
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 84 deletions.
2 changes: 1 addition & 1 deletion libp2p/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set(libp2p_source
socket.h
socket_data.h
net_supervisor.h
message.h protocol.h handler.h handshake.h protocol.cpp protocol_events.h node.h)
message.h protocol.h handler.h handshake.h protocol_events.h node.h)

set(libp2ppreset_source
preset/p2p_socket_data.h
Expand Down
18 changes: 11 additions & 7 deletions libp2p/handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#include "socket.h"
#include "message.h"

template <typename SocketType>
class Handshake : public virtual ProtocolEvents
template <typename SocketType, typename... COMPONENTS>
class BaseHandshake : public ProtocolEvents, public COMPONENTS...
{
protected:
typedef SocketType socket_type;
SocketType* socket;
public:
Event<> event_handle_message; // Вызывается, когда мы получаем любое сообщение.

Handshake(auto socket_) : socket(socket_)
virtual void handle_raw(std::shared_ptr<RawMessage> raw_msg) = 0;
public:
Handshake(socket_type* socket_) : socket(socket_), COMPONENTS(this, std::forward<Args>(args))...
{
socket->set_message_handler
(
Expand All @@ -33,9 +33,13 @@ class Handshake : public virtual ProtocolEvents
auto get_socket() const { return socket; }
auto get_addr() const { return socket->get_addr(); }

virtual void handle_message(std::shared_ptr<RawMessage> raw_msg) = 0;
void handle_message(std::shared_ptr<RawMessage> raw_msg)
{
handle_raw(raw_msg);
event_handle_message->happened();
}

virtual void close()
void close()
{
socket->close();
delete socket;
Expand Down
20 changes: 0 additions & 20 deletions libp2p/protocol.cpp

This file was deleted.

97 changes: 41 additions & 56 deletions libp2p/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,73 @@
#include <functional>
#include <utility>

#include <libdevcore/logger.h>
#include "socket.h"
#include "message.h"
#include "protocol_events.h"
#include "handler.h"
#include <libdevcore/logger.h>

class BaseProtocol : public virtual ProtocolEvents
template <typename SocketType, typename ProtocolType, typename... COMPONENTS>
class BaseProtocol : public ProtocolEvents, public COMPONENTS...
{
protected:
Socket* socket;
typedef SocketType socket_type;
HandlerManagerPtr<ProtocolType> handler_manager;
SocketType* socket;

public:
virtual void write(std::shared_ptr<Message> msg);
virtual void handle(std::shared_ptr<RawMessage> raw_msg) = 0;
public:
// Not safe, socket->message_handler = nullptr; wanna for manual setup
BaseProtocol() = default;

explicit BaseProtocol (Socket* _socket) : socket(_socket)
{
}

virtual void write(std::shared_ptr<Message> msg) = 0;

public:
void set_socket(Socket* _socket);
Socket* get_socket() { return socket; }
auto get_addr() { return socket->get_addr(); }

bool operator<(BaseProtocol* rhs)
explicit BaseProtocol (socket_type* socket_, HandlerManagerPtr<ProtocolType> handler_manager_)
: socket(socket_), handler_manager(handler_manager_), COMPONENTS(this, std::forward<Args>(args))...
{
if (!socket)
{
// TODO: throw
}
if (!rhs->socket)
{
// TODO: throw
}

return socket->get_addr() < rhs->socket->get_addr();
socket->set_message_handler
(
[this](const std::shared_ptr<RawMessage>& raw_msg)
{
handle_message(raw_msg);
}
);
}

virtual void disconnect();
};

template <typename T>
class Protocol : public BaseProtocol
{
protected:
std::string name;
HandlerManagerPtr<T> handler_manager;
~BaseProtocol() = default;

public:
// Not safe, socket->message_handler = nullptr; handler_manager = nullptr; wanna for manual setup
Protocol() : BaseProtocol() {}
explicit Protocol(std::string _name) : BaseProtocol(), name(_name) {}

Protocol (std::string _name, Socket* _socket) : BaseProtocol(_socket), name(std::move(_name)) {}

Protocol (std::string _name, Socket* _socket, HandlerManagerPtr<T> _handler_manager): BaseProtocol(_socket), name(std::move(_name)),
handler_manager(_handler_manager)
{
_socket->set_message_handler(std::bind(&Protocol::handle, this, std::placeholders::_1));
}

virtual void handle(std::shared_ptr<RawMessage> raw_msg) override
{
event_handle_message->happened(); // ProtocolEvents::event_handle_message
auto get_socket() const { return socket; }
auto get_addr() const { return socket->get_addr(); }

void handle_message(std::shared_ptr<RawMessage> raw_msg)
{
auto handler = handler_manager->get_handler(raw_msg->command);
if (handler)
{
LOG_DEBUG_P2P << name << " protocol call handler for " << raw_msg->command;
handler->invoke(raw_msg->value, (T*)this);
handler->invoke(raw_msg->value, (ProtocolType*)this);
} else
{
LOG_WARNING << "Handler " << raw_msg->command << " not found!";
}
event_handle_message->happened();
}

void close()
{
socket->close();
delete socket;
}

public:
void set_handler_manager(HandlerManagerPtr<T> _mngr)
bool operator<(BaseProtocol* rhs)
{
handler_manager = _mngr;
if (!socket)
{
// TODO: throw
}
if (!rhs->socket)
{
// TODO: throw
}

return get_addr() < rhs->get_addr();
}
};

0 comments on commit 8b3ef46

Please sign in to comment.