DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
internal.h
Go to the documentation of this file.
1 #pragma once
2 #include "DCS_ModuleUtils.h"
3 #include "../../DCS_Network/include/internal.h"
4 
5 #include <queue>
6 #include <mutex>
7 #include <condition_variable>
8 
27 DCS::GenericHandle AllocateGenericHandle(DCS::u16 size, DCS::GenericHandle obj = nullptr);
28 
33 void FreeGenericHandle(DCS::GenericHandle hnd);
34 
35 namespace DCS
36 {
37  namespace Utils
38  {
39 #pragma warning( push )
40 #pragma warning( disable : 4251 )
41 
47  {
48  public:
50  SMessageQueue() {};
51  ~SMessageQueue() {};
52 
53  Type pop()
54  {
55  std::unique_lock<std::mutex> lock(m);
56  if (q.empty())
57  {
58  c.wait(lock);
59  }
60  if (!q.empty())
61  {
62  Type copy = q.front();
63  q.pop();
64  return copy;
65  }
66  return Type();
67  }
68 
69  void push(const Type& value)
70  {
71  std::unique_lock<std::mutex> lock(m);
72  q.push(value);
73  lock.unlock();
74  c.notify_one();
75  }
76 
77  void notify_unblock()
78  {
79  c.notify_all();
80  }
81 
82  int size()
83  {
84  std::unique_lock<std::mutex> lock(m);
85  int s = (int)q.size();
86  lock.unlock();
87  return s;
88  }
89 
90  private:
91  std::queue<Type> q;
92  std::mutex m;
93  std::condition_variable c;
94  };
95 
101  {
102  public:
103  ByteQueue(u64 buffer_size)
104  {
105  buffer = new u8[buffer_size];
106  internal_buff_size = 0;
107  internal_buff_max_size = buffer_size;
108  ntf_unblock.store(false);
109  };
110  ~ByteQueue()
111  {
112  delete[] buffer;
113  internal_buff_size = 0;
114  internal_buff_max_size = 0;
115  };
116 
117  void addBytes(u8* data, u64 size)
118  {
119  std::unique_lock<std::mutex> lock(m);
120 
121  c.wait(lock, [&] {return internal_buff_size + size <= internal_buff_max_size; });
122 
123  memcpy(buffer + internal_buff_size, data, size);
124  internal_buff_size += size;
125 
126  lock.unlock();
127  c.notify_one();
128  }
129 
130  u64 fetchNextMsg(u8* buffer_dst)
131  {
132  std::unique_lock<std::mutex> lock(m);
133 
134  c.wait(lock, [&] {return (internal_buff_size >= sizeof(i32) || ntf_unblock.load()); });
135 
136  if (ntf_unblock.load())
137  {
138  return 0;
139  }
140 
141  i32 to_read;
142  memcpy(&to_read, buffer, sizeof(i32));
143  const i32 packet_size = to_read + sizeof(i32);
144 
145  c.wait(lock, [&] { return packet_size <= internal_buff_size; });
146 
147  memcpy(buffer_dst, buffer + sizeof(i32), to_read);
148  internal_buff_size -= packet_size;
149 
150  if (internal_buff_size > 0)
151  memmove(buffer, buffer + packet_size, internal_buff_size);
152 
153  lock.unlock();
154  c.notify_one();
155 
156  return to_read;
157  }
158 
159  u64 count()
160  {
161  std::unique_lock<std::mutex> lock(m);
162  u64 ibs = internal_buff_size;
163  lock.unlock();
164  return ibs;
165  }
166 
167  void notify_unblock()
168  {
169  ntf_unblock.store(true);
170  c.notify_all();
171  }
172 
173  void notify_restart()
174  {
175  delete[] buffer;
176  internal_buff_size = 0;
177  buffer = new u8[internal_buff_max_size];
178  ntf_unblock.store(false);
179  }
180 
181  private:
182  u8* buffer;
183  u64 internal_buff_size;
184  u64 internal_buff_max_size;
185 
186  std::atomic<bool> ntf_unblock;
187  std::mutex m;
188  std::condition_variable c;
189  };
190 #pragma warning( pop )
191 
192  template<typename E>
193  constexpr auto toUnderlyingType(E e)
194  {
195  return static_cast<typename std::underlying_type<E>::type>(e);
196  }
197 
198  DCS_INTERNAL_TEST void GetConsoleSize(int* x, int* y);
199 
200  DCS_INTERNAL_TEST void SetConsoleMargins(int t, int b);
201 
202  DCS_INTERNAL_TEST void WriteConsoleLine(int b, const char* str);
203 
204  DCS_INTERNAL_TEST void SetStdinEcho(bool enable);
205  }
206 }
Container for all the exported symbols that fall in the Utils category.
Definition: internal.h:100
Definition: internal.h:152
unsigned short u16
Equivalent to uint_16t.
Definition: DCS_ModuleUtils.h:56
void * GenericHandle
A generic opaque handle that is only meaningful for the API.
Definition: DCS_ModuleUtils.h:66
signed long i32
Equivalent to int_32t.
Definition: DCS_ModuleUtils.h:53
Definition: registry.h:70
Definition: internal.h:46
#define DCS_INTERNAL_TEST
Defines the export interface acessible via the dll-interface applicable for internal functions only...
Definition: exports.h:137
unsigned long long u64
Equivalent to uint_64t.
Definition: DCS_ModuleUtils.h:52
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58