xbmc
ActorProtocol.h
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 #include <utility>
18 
19 class CEvent;
20 
21 namespace Actor
22 {
23 
25 {
26 public:
27  virtual ~CPayloadWrapBase() = default;
28 };
29 
30 template<typename Payload>
32 {
33 public:
34  ~CPayloadWrap() override = default;
35  CPayloadWrap(Payload* data) { m_pPayload.reset(data); }
36  CPayloadWrap(Payload& data) { m_pPayload.reset(new Payload(data)); }
37  Payload* GetPlayload() { return m_pPayload.get(); }
38 
39 protected:
40  std::unique_ptr<Payload> m_pPayload;
41 };
42 
43 class Protocol;
44 
45 class Message
46 {
47  friend class Protocol;
48 
49  static constexpr size_t MSG_INTERNAL_BUFFER_SIZE = 32;
50 
51 public:
52  int signal;
53  bool isSync = false;
54  bool isSyncFini;
55  bool isOut;
56  bool isSyncTimeout;
57  size_t payloadSize;
58  uint8_t buffer[MSG_INTERNAL_BUFFER_SIZE];
59  uint8_t *data = nullptr;
60  std::unique_ptr<CPayloadWrapBase> payloadObj;
61  Message *replyMessage = nullptr;
62  Protocol &origin;
63  CEvent *event = nullptr;
64 
65  void Release();
66  bool Reply(int sig, void *data = nullptr, size_t size = 0);
67 
68 private:
69  explicit Message(Protocol &_origin) noexcept
70  :origin(_origin) {}
71 };
72 
73 class Protocol
74 {
75 public:
76  Protocol(std::string name, CEvent* inEvent, CEvent* outEvent)
77  : portName(std::move(name)), containerInEvent(inEvent), containerOutEvent(outEvent)
78  {
79  }
80  Protocol(std::string name) : Protocol(std::move(name), nullptr, nullptr) {}
81  ~Protocol();
82  Message *GetMessage();
83  void ReturnMessage(Message *msg);
84  bool SendOutMessage(int signal,
85  const void* data = nullptr,
86  size_t size = 0,
87  Message* outMsg = nullptr);
88  bool SendOutMessage(int signal, CPayloadWrapBase *payload, Message *outMsg = nullptr);
89  bool SendInMessage(int signal,
90  const void* data = nullptr,
91  size_t size = 0,
92  Message* outMsg = nullptr);
93  bool SendInMessage(int signal, CPayloadWrapBase *payload, Message *outMsg = nullptr);
94  bool SendOutMessageSync(int signal,
95  Message** retMsg,
96  std::chrono::milliseconds timeout,
97  const void* data = nullptr,
98  size_t size = 0);
99  bool SendOutMessageSync(int signal,
100  Message** retMsg,
101  std::chrono::milliseconds timeout,
102  CPayloadWrapBase* payload);
103  bool ReceiveOutMessage(Message **msg);
104  bool ReceiveInMessage(Message **msg);
105  void Purge();
106  void PurgeIn(int signal);
107  void PurgeOut(int signal);
108  void DeferIn(bool value) { inDefered = value; }
109  void DeferOut(bool value) { outDefered = value; }
110  void Lock() { criticalSection.lock(); }
111  void Unlock() { criticalSection.unlock(); }
112  std::string portName;
113 
114 protected:
115  CEvent *containerInEvent, *containerOutEvent;
116  CCriticalSection criticalSection;
117  std::queue<Message*> outMessages;
118  std::queue<Message*> inMessages;
119  std::queue<Message*> freeMessageQueue;
120  bool inDefered = false, outDefered = false;
121 };
122 
123 }
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: ActorProtocol.h:73
Definition: ActorProtocol.h:31
Definition: ActorProtocol.h:21
Definition: ActorProtocol.h:45
Definition: ActorProtocol.h:24