NAP
oscreceiver.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/device.h>
9 #include <rtti/factory.h>
10 #include <utility/dllexport.h>
11 #include <thread>
12 #include <queue>
13 
14 // Local Includes
15 #include "oscpacketlistener.h"
16 #include "oscreceivingsocket.h"
17 #include "oscevent.h"
18 
19 // Forward Declares
20 namespace nap
21 {
22  class OSCService;
23 }
24 
25 namespace nap
26 {
34  class NAPAPI OSCReceiver : public Device
35  {
36  friend class OSCService;
37  RTTI_ENABLE(Device)
38  public:
39  // Constructor used by factory
40  OSCReceiver(OSCService& service);
41 
46  virtual bool start(utility::ErrorState& errorState) override;
47 
51  virtual void stop() override;
52 
53  public:
54  int mPort = 7000;
55  bool mDebugOutput = false;
56  bool mAllowPortReuse = false;
57 
62  void addEvent(OSCEventPtr event);
63 
64  private:
65  // Runs in the background
66  void eventThread(int port);
67 
73  void consumeEvents(std::queue<OSCEventPtr>& outEvents);
74 
75  // The socket used for receiving messages
76  std::unique_ptr<OSCReceivingSocket> mSocket = nullptr;
77 
78  // Queue that holds all the consumed events
79  std::queue<OSCEventPtr> mEvents;
80 
81  // Mutex associated with setting / getting events
82  std::mutex mEventMutex;
83 
84  // The listener used for handling messages
85  std::unique_ptr<OSCPacketListener> mListener = nullptr;
86 
87  // OSC service that manages all the osc receivers / senders
88  OSCService* mService = nullptr;
89 
90  // The thread that receives and converts the messages
91  std::thread mEventThread;
92  };
93 
94  // Object creator used for constructing the the OSC receiver
96 }
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::OSCReceiver
Definition: oscreceiver.h:34
nap
Definition: templateapp.h:17
nap::OSCService
Definition: oscservice.h:28
nap::Device
Definition: device.h:20
nap::OSCEventPtr
std::unique_ptr< nap::OSCEvent > OSCEventPtr
Definition: oscevent.h:156