diff options
Diffstat (limited to 'xbmc/utils/ActorProtocol.h')
| -rw-r--r-- | xbmc/utils/ActorProtocol.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/xbmc/utils/ActorProtocol.h b/xbmc/utils/ActorProtocol.h new file mode 100644 index 0000000..97297a6 --- /dev/null +++ b/xbmc/utils/ActorProtocol.h | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include "threads/CriticalSection.h" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <memory> | ||
| 15 | #include <queue> | ||
| 16 | #include <string> | ||
| 17 | |||
| 18 | class CEvent; | ||
| 19 | |||
| 20 | namespace Actor | ||
| 21 | { | ||
| 22 | |||
| 23 | class CPayloadWrapBase | ||
| 24 | { | ||
| 25 | public: | ||
| 26 | virtual ~CPayloadWrapBase() = default; | ||
| 27 | }; | ||
| 28 | |||
| 29 | template<typename Payload> | ||
| 30 | class CPayloadWrap : public CPayloadWrapBase | ||
| 31 | { | ||
| 32 | public: | ||
| 33 | ~CPayloadWrap() override = default; | ||
| 34 | CPayloadWrap(Payload *data) {m_pPayload.reset(data);}; | ||
| 35 | CPayloadWrap(Payload &data) {m_pPayload.reset(new Payload(data));}; | ||
| 36 | Payload *GetPlayload() {return m_pPayload.get();}; | ||
| 37 | protected: | ||
| 38 | std::unique_ptr<Payload> m_pPayload; | ||
| 39 | }; | ||
| 40 | |||
| 41 | class Protocol; | ||
| 42 | |||
| 43 | class Message | ||
| 44 | { | ||
| 45 | friend class Protocol; | ||
| 46 | |||
| 47 | static constexpr size_t MSG_INTERNAL_BUFFER_SIZE = 32; | ||
| 48 | |||
| 49 | public: | ||
| 50 | int signal; | ||
| 51 | bool isSync = false; | ||
| 52 | bool isSyncFini; | ||
| 53 | bool isOut; | ||
| 54 | bool isSyncTimeout; | ||
| 55 | size_t payloadSize; | ||
| 56 | uint8_t buffer[MSG_INTERNAL_BUFFER_SIZE]; | ||
| 57 | uint8_t *data = nullptr; | ||
| 58 | std::unique_ptr<CPayloadWrapBase> payloadObj; | ||
| 59 | Message *replyMessage = nullptr; | ||
| 60 | Protocol &origin; | ||
| 61 | CEvent *event = nullptr; | ||
| 62 | |||
| 63 | void Release(); | ||
| 64 | bool Reply(int sig, void *data = nullptr, size_t size = 0); | ||
| 65 | |||
| 66 | private: | ||
| 67 | explicit Message(Protocol &_origin) noexcept | ||
| 68 | :origin(_origin) {} | ||
| 69 | }; | ||
| 70 | |||
| 71 | class Protocol | ||
| 72 | { | ||
| 73 | public: | ||
| 74 | Protocol(std::string name, CEvent* inEvent, CEvent *outEvent) | ||
| 75 | :portName(name), containerInEvent(inEvent), containerOutEvent(outEvent) {} | ||
| 76 | Protocol(std::string name) | ||
| 77 | : Protocol(name, nullptr, nullptr) {} | ||
| 78 | ~Protocol(); | ||
| 79 | Message *GetMessage(); | ||
| 80 | void ReturnMessage(Message *msg); | ||
| 81 | bool SendOutMessage(int signal, | ||
| 82 | const void* data = nullptr, | ||
| 83 | size_t size = 0, | ||
| 84 | Message* outMsg = nullptr); | ||
| 85 | bool SendOutMessage(int signal, CPayloadWrapBase *payload, Message *outMsg = nullptr); | ||
| 86 | bool SendInMessage(int signal, | ||
| 87 | const void* data = nullptr, | ||
| 88 | size_t size = 0, | ||
| 89 | Message* outMsg = nullptr); | ||
| 90 | bool SendInMessage(int signal, CPayloadWrapBase *payload, Message *outMsg = nullptr); | ||
| 91 | bool SendOutMessageSync( | ||
| 92 | int signal, Message** retMsg, int timeout, const void* data = nullptr, size_t size = 0); | ||
| 93 | bool SendOutMessageSync(int signal, Message **retMsg, int timeout, CPayloadWrapBase *payload); | ||
| 94 | bool ReceiveOutMessage(Message **msg); | ||
| 95 | bool ReceiveInMessage(Message **msg); | ||
| 96 | void Purge(); | ||
| 97 | void PurgeIn(int signal); | ||
| 98 | void PurgeOut(int signal); | ||
| 99 | void DeferIn(bool value) {inDefered = value;}; | ||
| 100 | void DeferOut(bool value) {outDefered = value;}; | ||
| 101 | void Lock() {criticalSection.lock();}; | ||
| 102 | void Unlock() {criticalSection.unlock();}; | ||
| 103 | std::string portName; | ||
| 104 | |||
| 105 | protected: | ||
| 106 | CEvent *containerInEvent, *containerOutEvent; | ||
| 107 | CCriticalSection criticalSection; | ||
| 108 | std::queue<Message*> outMessages; | ||
| 109 | std::queue<Message*> inMessages; | ||
| 110 | std::queue<Message*> freeMessageQueue; | ||
| 111 | bool inDefered = false, outDefered = false; | ||
| 112 | }; | ||
| 113 | |||
| 114 | } | ||
