read/write

This commit is contained in:
2025-02-06 14:07:52 +01:00
parent 6bbc849076
commit f3e98543d7
5 changed files with 29 additions and 106 deletions

View File

@@ -1,61 +0,0 @@
#pragma once
/**
* \file PacketDispatcher.h
* \brief File containing the td::protocol::PacketDispatcher class
*/
#include <sp/common/NonCopyable.h>
#include <vector>
#include <map>
namespace sp {
namespace protocol {
/**
* \class Dispatcher
* \brief Class used to dispatch things
*/
template <typename T_Enum, typename T_Handler, typename T>
class Dispatcher : private NonCopyable {
private:
std::map<T_Enum, std::vector<T_Handler*>> m_Handlers;
public:
/**
* \brief Constructor
*/
Dispatcher() {}
/**
* \brief Dispatch a packet
* \param packet The packet to dispatch
*/
void Dispatch(const T& packet);
/**
* \brief Register a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void RegisterHandler(T_Enum type, T_Handler& handler);
/**
* \brief Unregister a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void UnregisterHandler(T_Enum type, T_Handler& handler);
/**
* \brief Unregister a packet handler
* \param handler The packet handler
*/
void UnregisterHandler(T_Handler& handler);
};
} // namespace protocol
} // namespace td
#include "Dispatcher.inl"

View File

@@ -1,38 +0,0 @@
#pragma once
namespace sp {
namespace protocol {
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::Dispatch(const T& packet) {
T_Enum type = packet.GetType();
for (auto* handler : m_Handlers[type])
handler->Check(packet);
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::RegisterHandler(T_Enum type, T_Handler& handler) {
auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler);
if (found == m_Handlers[type].end())
m_Handlers[type].push_back(&handler);
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Enum type, T_Handler& handler) {
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Handler& handler) {
for (auto& pair : m_Handlers) {
if (pair.second.empty())
continue;
auto type = pair.first;
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
}
}
} // namespace protocol
} // namespace td

View File

@@ -8,8 +8,12 @@ template <typename ValueType>
class Field {
public:
// Provide an access to the stored value
ValueType& GetValue();
const ValueType& GetValue() const;
ValueType& GetValue() {
return m_Value;
}
const ValueType& GetValue() const {
return m_Value;
}
// Read (deserialise) and update internal value
void Read(DataBuffer& buffer) {

View File

@@ -1,6 +1,7 @@
#pragma once
#include <sp/Message.h>
#include <sp/Templates.h>
namespace sp {
namespace option {
@@ -100,6 +101,11 @@ class MessageImplFieldsBase : public TBase {
return m_Fields;
}
template<std::size_t FIndex>
auto& GetField() {
return std::get<FIndex>(GetFields()).GetValue();
}
private:
TFields m_Fields;
};
@@ -108,9 +114,9 @@ template <typename TBase>
class MessageImplFieldsReadBase : public TBase {
protected:
void ReadImpl(DataBuffer& buffer) override {
// Access fields via interface provided in previous chunk
//TODO: add endianess
auto& allFields = TBase::GetFields();
//... // read all the fields
tupleForEach(allFields, FieldReader{buffer});
}
};
@@ -118,9 +124,9 @@ template <typename TBase>
class MessageImplFieldsWriteBase : public TBase {
protected:
void WriteImpl(DataBuffer& buffer) override {
// Access fields via interface provided in previous chunk
//TODO: add endianess
auto& allFields = TBase::GetFields();
//... // write all the fields
tupleForEach(allFields, FieldWriter{buffer});
}
};

View File

@@ -52,7 +52,19 @@ class MyHandler : public sp::GenericHandler<MyMessage, AllMessages> {
int main() {
MyMessage::MsgIdType test;
auto yes = std::make_unique<ActualMessage1<MyMessage>>();
auto& fields = yes->GetFields();
std::get<0>(fields) = 69;
std::get<1>(fields) = 42;
MyHandler handlerTest;
yes->Dispatch(handlerTest);
sp::DataBuffer buffer;
yes->Write(buffer);
auto yesyes = std::make_unique<ActualMessage1<MyMessage>>();
yesyes->Read(buffer);
std::cout << "Field 1 : " << yesyes->GetField<0>() << std::endl;
std::cout << "Field 2 : " << (unsigned) yesyes->GetField<1>() << std::endl;
return 0;
}