Skip to content

Commit

Permalink
Add protocol to RpcFrame constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Nov 4, 2024
1 parent 9995bcf commit 83d3ba0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libshvchainpack/include/shv/chainpack/rpcmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SHVCHAINPACK_DECL_EXPORT RpcFrame
std::string data;

RpcFrame() = default;
RpcFrame(RpcValue::MetaData &&meta, std::string &&data) : meta(std::move(meta)), data(std::move(data)) {}
RpcFrame(Rpc::ProtocolType protocol, RpcValue::MetaData &&meta, std::string &&data) : protocol(protocol), meta(std::move(meta)), data(std::move(data)) {}
bool isValid() const { return !meta.isEmpty() && !data.empty(); }
RpcMessage toRpcMessage(std::string *errmsg = nullptr) const;
std::string toFrameData() const;
Expand Down
9 changes: 3 additions & 6 deletions libshvchainpack/src/rpcmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ RpcFrame RpcFrame::fromFrameData(const std::string &frame_data)
if(pos < 0)
throw ParseException(CCPCP_RC_MALFORMED_INPUT, "Metadata missing", -1, {});
auto data = std::string(frame_data, static_cast<size_t>(pos));
RpcFrame frame(std::move(meta), std::move(data));
frame.protocol = protocol;
RpcFrame frame(protocol, std::move(meta), std::move(data));
return frame;
}
case Rpc::ProtocolType::Cpon: {
Expand All @@ -156,8 +155,7 @@ RpcFrame RpcFrame::fromFrameData(const std::string &frame_data)
if(pos < 0)
throw ParseException(CCPCP_RC_MALFORMED_INPUT, "Metadata missing", -1, {});
auto data = std::string(frame_data, static_cast<size_t>(pos));
RpcFrame frame(std::move(meta), std::move(data));
frame.protocol = protocol;
RpcFrame frame(protocol, std::move(meta), std::move(data));
return frame;
}
default:
Expand Down Expand Up @@ -654,8 +652,7 @@ RpcFrame RpcMessage::toToRpcFrame(Rpc::ProtocolType protocol) const
default:
throw std::runtime_error("Invalid protocol type");
}
RpcFrame frame{std::move(meta), std::move(data)};
frame.protocol = protocol;
RpcFrame frame{protocol, std::move(meta), std::move(data)};
return frame;
}

Expand Down
1 change: 1 addition & 0 deletions libshviotqt/include/shv/iotqt/rpc/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SHVIOTQT_DECL_EXPORT FrameReader {
int tryToReadMeta(std::istringstream &in);
protected:
std::vector<chainpack::RpcFrame> m_frames;
chainpack::Rpc::ProtocolType m_protocol;
chainpack::RpcValue::MetaData m_meta;
std::optional<size_t> m_dataStart;

Expand Down
2 changes: 1 addition & 1 deletion libshviotqt/src/rpc/serialportsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void SerialFrameReader::finishFrame()
}
shvDebug() << "ADD FRAME:" << chainpack::utils::hexArray(m_readBuffer.data(), m_readBuffer.size());
auto frame_data = std::string(m_readBuffer, m_dataStart.value());
m_frames.emplace_back(std::move(m_meta), std::move(frame_data));
m_frames.emplace_back(m_protocol, std::move(m_meta), std::move(frame_data));
setState(ReadState::WaitingForStx);
}

Expand Down
4 changes: 3 additions & 1 deletion libshviotqt/src/rpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ int FrameReader::tryToReadMeta(std::istringstream &in)
auto protocol = in.get();
AbstractStreamReader *rd = nullptr;
if (protocol == protocol_cpon) {
m_protocol = shv::chainpack::Rpc::ProtocolType::Cpon;
rd = new chainpack::CponReader(in);
}
else if (protocol == protocol_chainpack) {
m_protocol = shv::chainpack::Rpc::ProtocolType::ChainPack;
rd = new chainpack::ChainPackReader(in);
}
if (rd) {
Expand Down Expand Up @@ -142,7 +144,7 @@ QList<int> StreamFrameReader::addData(std::string_view data)
}
auto frame_data = std::string(m_readBuffer, m_dataStart.value(), frame_size);
m_readBuffer = std::string(m_readBuffer, consumed_len + frame_size);
m_frames.emplace_back(std::move(m_meta), std::move(frame_data));
m_frames.emplace_back(m_protocol, std::move(m_meta), std::move(frame_data));
m_meta = {};
m_dataStart = {};
}
Expand Down

0 comments on commit 83d3ba0

Please sign in to comment.