NAP
udpthread.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External includes
8 #include <nap/resourceptr.h>
9 #include <nap/resource.h>
10 #include <nap/device.h>
11 #include <thread>
12 
13 // NAP includes
14 #include <nap/numeric.h>
15 #include <concurrentqueue.h>
16 #include <rtti/factory.h>
17 
18 // ASIO forward declaration
19 namespace asio
20 {
21  class io_context;
22 }
23 
24 namespace nap
25 {
27 
32  {
36  };
37 
38  // forward declares
39  class UDPAdapter;
40  class UDPService;
41 
51  class NAPAPI UDPThread : public Device
52  {
53  friend class UDPService;
54  friend class UDPAdapter;
55  RTTI_ENABLE(Device)
56  public:
57  UDPThread();
58 
63  UDPThread(UDPService& service);
64 
68  virtual ~UDPThread();
69 
75  virtual bool start(utility::ErrorState& errorState) override final;
76 
80  virtual void stop() override final;
81 
85  asio::io_context& getIOContext();
86 
91  void manualProcess();
92 
93  // properties
95  private:
99  void thread();
100 
104  void process();
105 
110  void registerAdapter(UDPAdapter* adapter);
111 
116  void removeAdapter(UDPAdapter* adapter);
117 
118  // threading
119  std::thread mThread;
120  std::mutex mMutex;
121  std::atomic_bool mRun = { false };
122  std::function<void()> mManualProcessFunc;
123 
124  // service
125  UDPService& mService;
126 
127  // adapters
128  std::vector<UDPAdapter*> mAdapters;
129 
130  struct Impl;
131  std::unique_ptr<Impl> mImpl;
132  };
133 
134  // Object creator used for constructing the UDP thread
136 }
nap::UDPThread
Definition: udpthread.h:51
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::EUDPThreadUpdateMethod
EUDPThreadUpdateMethod
Definition: udpthread.h:31
nap::UDP_MAIN_THREAD
@ UDP_MAIN_THREAD
process UDPAdapters on main thread
Definition: udpthread.h:33
nap::UDP_MANUAL
@ UDP_MANUAL
only process UDPAdapters when the user explicitly calls manualProcess on the UDPThread
Definition: udpthread.h:35
nap::UDPAdapter
Definition: udpadapter.h:25
nap::UDP_SPAWN_OWN_THREAD
@ UDP_SPAWN_OWN_THREAD
process UDPAdapters in newly spawned thread
Definition: udpthread.h:34
nap
Definition: templateapp.h:17
asio
Definition: udpthread.h:19
nap::UDPService
Definition: udpservice.h:20
nap::Device
Definition: device.h:20