[DEV] simplify protocol and add multiple packect base interface
This commit is contained in:
parent
c362381107
commit
7ce31359c9
@ -86,62 +86,14 @@ zeus::AbstractFunction::AbstractFunction(const std::string& _name,
|
||||
}
|
||||
|
||||
|
||||
bool zeus::AbstractFunction::checkCompatibility(const ParamType& _type, const std::string& _params) {
|
||||
if (createType<bool>() == _type) {
|
||||
return _params == "bool";
|
||||
bool zeus::AbstractFunction::checkCompatibility(const ParamType& _type, const ParamType& _params) {
|
||||
if (_params == _type) {
|
||||
return true;
|
||||
}
|
||||
if ( createType<int64_t>() == _type
|
||||
|| createType<int32_t>() == _type
|
||||
|| createType<int16_t>() == _type
|
||||
|| createType<int8_t>() == _type
|
||||
|| createType<uint64_t>() == _type
|
||||
|| createType<uint32_t>() == _type
|
||||
|| createType<uint16_t>() == _type
|
||||
|| createType<uint8_t>() == _type
|
||||
|| createType<float>() == _type
|
||||
|| createType<double>() == _type) {
|
||||
return _params == "int8"
|
||||
|| _params == "int16"
|
||||
|| _params == "int32"
|
||||
|| _params == "int64"
|
||||
|| _params == "uint8"
|
||||
|| _params == "uint16"
|
||||
|| _params == "uint32"
|
||||
|| _params == "uint64"
|
||||
|| _params == "float"
|
||||
|| _params == "double";
|
||||
}
|
||||
if (createType<std::vector<std::string>>() == _type) {
|
||||
return _params == "vector:string";
|
||||
}
|
||||
if ( createType<std::vector<bool>>() == _type
|
||||
|| createType<std::vector<int64_t>>() == _type
|
||||
|| createType<std::vector<int32_t>>() == _type
|
||||
|| createType<std::vector<int16_t>>() == _type
|
||||
|| createType<std::vector<int8_t>>() == _type
|
||||
|| createType<std::vector<uint64_t>>() == _type
|
||||
|| createType<std::vector<uint32_t>>() == _type
|
||||
|| createType<std::vector<uint16_t>>() == _type
|
||||
|| createType<std::vector<uint8_t>>() == _type
|
||||
|| createType<std::vector<float>>() == _type
|
||||
|| createType<std::vector<double>>() == _type) {
|
||||
return _params == "vector:int8"
|
||||
|| _params == "vector:int16"
|
||||
|| _params == "vector:int32"
|
||||
|| _params == "vector:int64"
|
||||
|| _params == "vector:uint8"
|
||||
|| _params == "vector:uint16"
|
||||
|| _params == "vector:uint32"
|
||||
|| _params == "vector:uint64"
|
||||
|| _params == "vector:float"
|
||||
|| _params == "vector:double"
|
||||
|| _params == "vector:empty";
|
||||
}
|
||||
if (createType<zeus::File>() == _type) {
|
||||
return _params == "file";
|
||||
}
|
||||
if (createType<std::string>() == _type) {
|
||||
return _params == "string";
|
||||
// check cross compatibilité (All number are convertible (with min/max) and all number vector are convertible (min/max too)
|
||||
if ( _type.isNumber() == true
|
||||
&& _params.isNumber() == true) {
|
||||
return _type.isVector() == _params.isVector();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ namespace zeus {
|
||||
public:
|
||||
virtual ~AbstractFunction() {};
|
||||
public:
|
||||
bool checkCompatibility(const ParamType& _type, const std::string& _params);
|
||||
bool checkCompatibility(const ParamType& _type, const ParamType& _params);
|
||||
public:
|
||||
std::string getPrototypeFull() const;
|
||||
virtual std::string getPrototype() const = 0;
|
||||
|
@ -73,6 +73,7 @@ namespace zeus {
|
||||
answer.setPartId(m_partId);
|
||||
answer.setPartFinish(false);
|
||||
if (m_partId == 0) {
|
||||
answer.setType(zeus::Buffer::typeMessage::answer);
|
||||
m_node.fileOpenRead();
|
||||
std::string extention = std::string(m_data.getFileName().begin()+m_data.getFileName().size() -3, m_data.getFileName().end());
|
||||
ZEUS_WARNING("send file: '" << m_data.getFileName() << "' with extention: '" << extention << "'");
|
||||
@ -83,6 +84,7 @@ namespace zeus {
|
||||
m_partId++;
|
||||
return false;
|
||||
}
|
||||
answer.setType(zeus::Buffer::typeMessage::data);
|
||||
int32_t tmpSize = 1024;
|
||||
if (m_size < 1024) {
|
||||
tmpSize = m_size;
|
||||
|
886
zeus/Buffer.cpp
886
zeus/Buffer.cpp
File diff suppressed because it is too large
Load Diff
@ -5,17 +5,19 @@
|
||||
*/
|
||||
#pragma once
|
||||
#include <etk/types.h>
|
||||
#include <enet/WebSocket.h>
|
||||
#include <zeus/ParamType.h>
|
||||
|
||||
namespace zeus {
|
||||
//U32 message lenght
|
||||
#pragma pack(push,1)
|
||||
struct headerBin {
|
||||
uint16_t versionProtocol; // protocol Version (might be 1)
|
||||
//uint16_t versionProtocol; // protocol Version (might be 1)
|
||||
uint32_t transactionID;
|
||||
uint32_t clientID; // same as sevice ID
|
||||
int16_t partID; // if < 0 the partId ifs the last (start at 0 if multiple or 0x8000 if single message)
|
||||
uint16_t typeMessage; //TypeMessgae (1:call, 2:Answer, 4:event)
|
||||
uint16_t numberOfParameter; //TypeMessgae (1:call, 2:Answer, 4:event)
|
||||
uint16_t numberOfParameter;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
/*
|
||||
@ -82,27 +84,7 @@ namespace zeus {
|
||||
class Buffer {
|
||||
private:
|
||||
headerBin m_header;
|
||||
std::vector<uint16_t> m_paramOffset;
|
||||
std::vector<uint8_t> m_data;
|
||||
public:
|
||||
const uint8_t* getHeader() const {
|
||||
return reinterpret_cast<const uint8_t*>(&m_header);
|
||||
}
|
||||
uint32_t getHeaderSize() const {
|
||||
return sizeof(headerBin);
|
||||
}
|
||||
const uint8_t* getParam() const {
|
||||
return reinterpret_cast<const uint8_t*>(&m_paramOffset[0]);
|
||||
}
|
||||
uint32_t getParamSize() const {
|
||||
return m_paramOffset.size() * 2;
|
||||
}
|
||||
const uint8_t* getData() const {
|
||||
return &m_data[0];
|
||||
}
|
||||
uint32_t getDataSize() const {
|
||||
return m_data.size();
|
||||
}
|
||||
mutable std::vector<std::pair<int32_t,std::vector<uint8_t>>> m_parameter;
|
||||
private:
|
||||
void internalComposeWith(const uint8_t* _buffer, uint32_t _lenght);
|
||||
public:
|
||||
@ -111,8 +93,6 @@ namespace zeus {
|
||||
void composeWith(const std::string& _buffer);
|
||||
std::string generateHumanString();
|
||||
void clear();
|
||||
uint16_t getProtocalVersion() const;
|
||||
void setProtocolVersion(uint16_t _value);
|
||||
uint32_t getTransactionId() const;
|
||||
void setTransactionId(uint32_t _value);
|
||||
uint32_t getClientId() const;// this is the same as serviceId
|
||||
@ -129,10 +109,10 @@ namespace zeus {
|
||||
bool getPartFinish() const;
|
||||
void setPartFinish(bool _value);
|
||||
enum class typeMessage {
|
||||
call = 0x0001,
|
||||
answer = 0x0002,
|
||||
event = 0x0004,
|
||||
data = 0x0008,
|
||||
call = 0x0001, // Remote call on a service ID
|
||||
answer = 0x0002, // Answer from a previous call
|
||||
data = 0x0003, // data message happend when partId > 0 it compleate the data of a parameter or an answer or an event
|
||||
event = 0x0004, // event message
|
||||
};
|
||||
enum typeMessage getType() const;
|
||||
void setType(enum typeMessage _value);
|
||||
@ -143,14 +123,15 @@ namespace zeus {
|
||||
private:
|
||||
template<class ZEUS_TYPE_DATA>
|
||||
ZEUS_TYPE_DATA internalGetParameter(int32_t _id) const;
|
||||
std::string internalGetParameterType(int32_t _id) const;
|
||||
zeus::ParamType internalGetParameterType(int32_t _id) const;
|
||||
const uint8_t* internalGetParameterPointer(int32_t _id) const;
|
||||
uint32_t internalGetParameterSize(int32_t _id) const;
|
||||
std::string simpleStringParam(uint32_t _id) const;
|
||||
public:
|
||||
std::string getCall() const;
|
||||
void setCall(std::string _value);
|
||||
uint16_t getNumberParameter() const;
|
||||
std::string getParameterType(int32_t _id) const;
|
||||
zeus::ParamType getParameterType(int32_t _id) const;
|
||||
const uint8_t* getParameterPointer(int32_t _id) const;
|
||||
uint32_t getParameterSize(int32_t _id) const;
|
||||
|
||||
@ -188,7 +169,7 @@ namespace zeus {
|
||||
//multiple section of data (part ...)
|
||||
void addData(void* _data, uint32_t _size);
|
||||
|
||||
uint64_t prepare();
|
||||
bool writeOn(enet::WebSocket& _interface);
|
||||
};
|
||||
std::ostream& operator <<(std::ostream& _os, enum zeus::Buffer::typeMessage _value);
|
||||
}
|
||||
|
@ -45,12 +45,10 @@ void zeus::GateWayService::stop() {
|
||||
|
||||
void zeus::GateWayService::SendData(uint64_t _userSessionId, zeus::Buffer& _data) {
|
||||
_data.setClientId(_userSessionId);
|
||||
_data.prepare();
|
||||
m_interfaceClient.writeBinary(_data);
|
||||
}
|
||||
|
||||
void zeus::GateWayService::onServiceData(zeus::Buffer& _value) {
|
||||
ZEUS_DEBUG("On service data: " << _value.generateHumanString());
|
||||
uint32_t transactionId = _value.getTransactionId();
|
||||
//data.add("from-service", ejson::String(m_name));
|
||||
if (_value.getType() == zeus::Buffer::typeMessage::event) {
|
||||
|
@ -8,19 +8,59 @@
|
||||
#include <zeus/debug.h>
|
||||
#include <zeus/File.h>
|
||||
|
||||
std::ostream& zeus::operator <<(std::ostream& _os, const zeus::ParamType& _obj) {
|
||||
_os << "{" << _obj.getId() << ":" << _obj.getName() << "}";
|
||||
return _os;
|
||||
}
|
||||
|
||||
zeus::ParamType::ParamType(const char* _name):
|
||||
m_typeName(_name) {
|
||||
zeus::ParamType::ParamType(const char* _name, uint16_t _id, bool _isNumber, bool _isVector):
|
||||
m_typeName(_name),
|
||||
m_id(_id),
|
||||
m_isNumber(_isNumber),
|
||||
m_isVector(_isVector) {
|
||||
|
||||
}
|
||||
zeus::ParamType::ParamType(const std::string& _name, uint16_t _id, bool _isNumber, bool _isVector):
|
||||
m_typeName(_name),
|
||||
m_id(_id),
|
||||
m_isNumber(_isNumber),
|
||||
m_isVector(_isVector) {
|
||||
|
||||
}
|
||||
|
||||
const char* zeus::ParamType::getName() const {
|
||||
bool zeus::ParamType::isNumber() const {
|
||||
return m_isNumber;
|
||||
}
|
||||
|
||||
bool zeus::ParamType::isVector() const {
|
||||
return m_isVector;
|
||||
}
|
||||
const std::string& zeus::ParamType::getName() const {
|
||||
return m_typeName;
|
||||
}
|
||||
|
||||
uint16_t zeus::ParamType::getId() const {
|
||||
return m_id;
|
||||
}
|
||||
|
||||
bool zeus::ParamType::operator == (const ParamType& _obj) const {
|
||||
if (m_id != _obj.m_id) {
|
||||
return false;
|
||||
}
|
||||
if (m_id == paramTypeObject) {
|
||||
return m_typeName == _obj.m_typeName;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool zeus::ParamType::operator != (const ParamType& _obj) const {
|
||||
if (m_id != _obj.m_id) {
|
||||
return true;
|
||||
}
|
||||
if (m_id == paramTypeObject) {
|
||||
return m_typeName != _obj.m_typeName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool zeus::ParamType::operator == (const std::string& _value) const {
|
||||
return _value == m_typeName;
|
||||
@ -29,43 +69,52 @@ bool zeus::ParamType::operator != (const std::string& _value) const {
|
||||
return _value != m_typeName;
|
||||
}
|
||||
|
||||
#define generate_basic_type(_type, _name) \
|
||||
bool zeus::ParamType::operator == (const uint16_t& _value) const {
|
||||
return _value == m_id;
|
||||
}
|
||||
|
||||
bool zeus::ParamType::operator != (const uint16_t& _value) const {
|
||||
return _value != m_id;
|
||||
}
|
||||
|
||||
#define generate_basic_type(_type, _name, _id, _num, _vect) \
|
||||
namespace zeus { \
|
||||
template<> zeus::ParamType createType<_type>() {\
|
||||
return zeus::ParamType(_name); \
|
||||
return zeus::ParamType(_name, _id, _num, _vect); \
|
||||
} \
|
||||
}
|
||||
|
||||
generate_basic_type(void, "void");
|
||||
generate_basic_type(bool, "bool");
|
||||
generate_basic_type(float, "float");
|
||||
generate_basic_type(double, "double");
|
||||
generate_basic_type(int64_t, "int64");
|
||||
generate_basic_type(int32_t, "int32");
|
||||
generate_basic_type(int16_t, "int16");
|
||||
generate_basic_type(int8_t, "int8");
|
||||
generate_basic_type(uint64_t, "uint64");
|
||||
generate_basic_type(uint32_t, "uint32");
|
||||
generate_basic_type(uint16_t, "uint16");
|
||||
generate_basic_type(uint8_t, "uint8");
|
||||
generate_basic_type(std::string, "string");
|
||||
generate_basic_type(void, "void", 0x1, false, false);
|
||||
generate_basic_type(bool, "bool", 0x2, false, false);
|
||||
generate_basic_type(float, "float", 0x3, true, false);
|
||||
generate_basic_type(double, "double", 0x4, true, false);
|
||||
generate_basic_type(int64_t, "int64", 0x5, true, false);
|
||||
generate_basic_type(int32_t, "int32", 0x6, true, false);
|
||||
generate_basic_type(int16_t, "int16", 0x7, true, false);
|
||||
generate_basic_type(int8_t, "int8", 0x8, true, false);
|
||||
generate_basic_type(uint64_t, "uint64", 0x9, true, false);
|
||||
generate_basic_type(uint32_t, "uint32", 0xA, true, false);
|
||||
generate_basic_type(uint16_t, "uint16", 0xB, true, false);
|
||||
generate_basic_type(uint8_t, "uint8", 0xC, true, false);
|
||||
generate_basic_type(std::string, "string", 0xD, false, false);
|
||||
|
||||
|
||||
generate_basic_type(std::vector<bool>, "vector:bool");
|
||||
generate_basic_type(std::vector<float>, "vector:float");
|
||||
generate_basic_type(std::vector<double>, "vector:double");
|
||||
generate_basic_type(std::vector<int64_t>, "vector:int64");
|
||||
generate_basic_type(std::vector<int32_t>, "vector:int32");
|
||||
generate_basic_type(std::vector<int16_t>, "vector:int16");
|
||||
generate_basic_type(std::vector<int8_t>, "vector:int8");
|
||||
generate_basic_type(std::vector<uint64_t>, "vector:uint64");
|
||||
generate_basic_type(std::vector<uint32_t>, "vector:uint32");
|
||||
generate_basic_type(std::vector<uint16_t>, "vector:uint16");
|
||||
generate_basic_type(std::vector<uint8_t>, "vector:uint8");
|
||||
generate_basic_type(std::vector<std::string>, "vector:string");
|
||||
generate_basic_type(std::vector<void>, "vector:empty", 0x0102, true, true);
|
||||
generate_basic_type(std::vector<bool>, "vector:bool", 0x0102, false, true);
|
||||
generate_basic_type(std::vector<float>, "vector:float", 0x0103, true, true);
|
||||
generate_basic_type(std::vector<double>, "vector:double", 0x0104, true, true);
|
||||
generate_basic_type(std::vector<int64_t>, "vector:int64", 0x0105, true, true);
|
||||
generate_basic_type(std::vector<int32_t>, "vector:int32", 0x0106, true, true);
|
||||
generate_basic_type(std::vector<int16_t>, "vector:int16", 0x0107, true, true);
|
||||
generate_basic_type(std::vector<int8_t>, "vector:int8", 0x0108, true, true);
|
||||
generate_basic_type(std::vector<uint64_t>, "vector:uint64", 0x0109, true, true);
|
||||
generate_basic_type(std::vector<uint32_t>, "vector:uint32", 0x010A, true, true);
|
||||
generate_basic_type(std::vector<uint16_t>, "vector:uint16", 0x010B, true, true);
|
||||
generate_basic_type(std::vector<uint8_t>, "vector:uint8", 0x010C, true, true);
|
||||
generate_basic_type(std::vector<std::string>, "vector:string", 0x010D, false, true);
|
||||
|
||||
generate_basic_type(zeus::File, "file", 0x000E, false, false);
|
||||
generate_basic_type(zeus::FileServer, "file", 0x000E, false, false);
|
||||
|
||||
generate_basic_type(zeus::File, "file");
|
||||
generate_basic_type(zeus::FileServer, "file");
|
||||
|
||||
const uint16_t zeus::paramTypeObject = 0xFFFF;
|
||||
|
||||
|
@ -10,15 +10,26 @@
|
||||
namespace zeus {
|
||||
class ParamType {
|
||||
protected:
|
||||
const char* m_typeName;
|
||||
const std::string m_typeName;
|
||||
const uint16_t m_id;
|
||||
const bool m_isNumber;
|
||||
const bool m_isVector;
|
||||
public:
|
||||
ParamType(const char* _name = "");
|
||||
const char* getName() const;
|
||||
ParamType(const char* _name = "", uint16_t _id=0, bool _isNumber=false, bool _isVector=false);
|
||||
ParamType(const std::string& _name, uint16_t _id, bool _isNumber=false, bool _isVector=false);
|
||||
const std::string& getName() const;
|
||||
uint16_t getId() const;
|
||||
bool operator == (const ParamType& _obj) const;
|
||||
bool operator != (const ParamType& _obj) const;
|
||||
bool operator == (const std::string& _value) const;
|
||||
bool operator != (const std::string& _value) const;
|
||||
bool operator == (const uint16_t& _value) const;
|
||||
bool operator != (const uint16_t& _value) const;
|
||||
bool isNumber() const;
|
||||
bool isVector() const;
|
||||
};
|
||||
|
||||
extern const uint16_t paramTypeObject;
|
||||
std::ostream& operator <<(std::ostream& _os, const zeus::ParamType& _obj);
|
||||
|
||||
template<class ZEUS_TYPE>
|
||||
ParamType createType();
|
||||
|
@ -32,7 +32,10 @@ void zeus::TcpString::setInterface(enet::Tcp _connection, bool _isServer) {
|
||||
m_connection.connectUri(this, &zeus::TcpString::onReceiveUri);
|
||||
m_connection.start();
|
||||
} else {
|
||||
m_connection.start("/stupidName");
|
||||
std::vector<std::string> protocols;
|
||||
protocols.push_back("zeus/0.8");
|
||||
protocols.push_back("zeus/1.0");
|
||||
m_connection.start("/stupidName", protocols);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,30 +91,23 @@ void zeus::TcpString::disconnect(bool _inThreadStop){
|
||||
}
|
||||
|
||||
int32_t zeus::TcpString::writeBinary(zeus::Buffer& _data) {
|
||||
uint64_t size = _data.prepare();
|
||||
ZEUS_DEBUG("Send BINARY " << size << " bytes '" << _data.generateHumanString() << "'");
|
||||
if (m_connection.isAlive() == false) {
|
||||
return -2;
|
||||
}
|
||||
if (m_connection.writeHeader(size, false) == false) {
|
||||
if (_data.writeOn(m_connection) == true) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
uint8_t* data = nullptr;
|
||||
uint32_t dataSize = 0;
|
||||
data = (uint8_t*)_data.getHeader();
|
||||
dataSize = _data.getHeaderSize();
|
||||
size = m_connection.writeData(data, dataSize);
|
||||
data = (uint8_t*)_data.getParam();
|
||||
dataSize = _data.getParamSize();
|
||||
size += m_connection.writeData(data, dataSize);
|
||||
data = (uint8_t*)_data.getData();
|
||||
dataSize = _data.getDataSize();
|
||||
size += m_connection.writeData(data, dataSize);
|
||||
return size;
|
||||
}
|
||||
|
||||
bool zeus::TcpString::onReceiveUri(const std::string& _uri) {
|
||||
bool zeus::TcpString::onReceiveUri(const std::string& _uri, const std::vector<std::string>& _protocols) {
|
||||
ZEUS_INFO("Receive Header uri: " << _uri);
|
||||
for (auto &it : _protocols) {
|
||||
if (it == "zeus/1.0") {
|
||||
m_connection.setProtocol(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (_uri == "/stupidName") {
|
||||
return true;
|
||||
}
|
||||
|
@ -49,8 +49,7 @@ namespace zeus {
|
||||
void setInterfaceName(const std::string& _name);
|
||||
int32_t writeBinary(zeus::Buffer& _data);
|
||||
void ping();
|
||||
private:
|
||||
bool onReceiveUri(const std::string& _uri);
|
||||
bool onReceiveUri(const std::string& _uri, const std::vector<std::string>& _protocols);
|
||||
void onReceiveData(std::vector<uint8_t>& _frame, bool _isBinary);
|
||||
void newBuffer(zeus::Buffer& _buffer);
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user