extern_interface/include/exint/protocol.hpp

44 lines
1.3 KiB
C++
Raw Permalink Normal View History

#ifndef _INCLUDE_EXINT_PROTOCOL_
#define _INCLUDE_EXINT_PROTOCOL_
#include <memory>
#include "transport/base.hpp"
#include "comframe.h"
class ComframeProtocol {
public:
typedef std::shared_ptr<ComFrame> FrameType;
static inline ssize_t pred_size(void* buf, size_t size) {
if (buf == nullptr) return sizeof(ComFrameHeader);
if (size < sizeof(ComFrameHeader)) return -1;
if (ComFrameHeader_Check(ComFrame_HEADER(buf), true)) {
return -1;
}
return ComFrame_Length((ComFrame*)buf, true);
}
static inline FrameType make_frame(void* buf, size_t size) {
if (buf == nullptr || size < sizeof(ComFrameHeader))
return FrameType();
ComFrameHeader_SwapEndian(ComFrame_HEADER(buf));
auto frame_len = ComFrame_LENGTH(buf);
if (size < frame_len)
return FrameType();
auto frame = ComFrame_NewUninited(frame_len);
memcpy(frame, buf, frame_len);
return std::shared_ptr<ComFrame>(frame, ComFrame_Del);
}
static inline size_t frame_size(FrameType frame) {
return ComFrame_Length(frame.get(), true);
}
static inline void* frame_data(FrameType frame) {
return frame.get();
}
};
using Transport = transport::BaseTransport<ComframeProtocol>;
#endif