Files
MuditaOS/module-services/service-desktop/parser/ParserFSM.hpp
Marek Niepieklo 08cc7e0663 [EGD-7400] Device won't reconnect to MC when unplugged
In case of disconnection from MC during reception of large messages
(e.g. to FS endpoint), the parser may be left in a state expecting
further parts of the message. This causes incorrect handling of new
messages arriving after reconnection to MC.
Added a timer to reset a Parser when transfer times out.
2021-11-16 09:55:06 +01:00

61 lines
1.4 KiB
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once
#include "MessageHandler.hpp"
#include <Timers/TimerHandle.hpp>
#include <json11.hpp>
#include <magic_enum.hpp>
#include <string>
namespace sdesktop::endpoints
{
enum class State
{
NoMsg,
ReceivedPartialHeader,
ReceivedPartialPayload,
};
class StateMachine
{
public:
explicit StateMachine(sys::Service *OwnerService);
void processMessage(std::string &&msg);
[[nodiscard]] auto getCurrentState() const noexcept -> State
{
return state;
};
void setState(const State newState) noexcept
{
state = newState;
}
void setMessageHandler(std::unique_ptr<MessageHandler> handler);
private:
std::string receivedMsg;
State state = State::NoMsg;
std::string payload;
std::string header;
unsigned long payloadLength = 0;
sys::Service *OwnerServicePtr = nullptr;
std::unique_ptr<MessageHandler> messageHandler;
sys::TimerHandle parserTimer;
void parseHeader();
void resetParser();
void restartTimer();
void parsePartialHeader();
void parseNewMessage();
void parsePartialMessage();
void parsePayload();
};
} // namespace sdesktop::endpoints