1 #ifndef ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_ 2 #define ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_ 3 4 #include <list> 5 #include <memory> 6 #include <mutex> 7 #include <unordered_map> 8 9 #include <pdx/file_handle.h> 10 #include <pdx/service_dispatcher.h> 11 12 namespace android { 13 namespace pdx { 14 namespace uds { 15 16 class ServiceDispatcher : public pdx::ServiceDispatcher { 17 public: 18 // Get a new instance of ServiceDispatcher, or return nullptr if init failed. 19 static std::unique_ptr<pdx::ServiceDispatcher> Create(); 20 21 ~ServiceDispatcher() override; 22 int AddService(const std::shared_ptr<Service>& service) override; 23 int RemoveService(const std::shared_ptr<Service>& service) override; 24 int ReceiveAndDispatch() override; 25 int ReceiveAndDispatch(int timeout) override; 26 int EnterDispatchLoop() override; 27 void SetCanceled(bool cancel) override; 28 bool IsCanceled() const override; 29 30 private: 31 ServiceDispatcher(); 32 33 // Internal thread accounting. 34 int ThreadEnter(); 35 void ThreadExit(); 36 37 std::mutex mutex_; 38 std::condition_variable condition_; 39 std::atomic<bool> canceled_{false}; 40 41 std::list<std::shared_ptr<Service>> services_; 42 43 int thread_count_ = 0; 44 LocalHandle event_fd_; 45 LocalHandle epoll_fd_; 46 47 ServiceDispatcher(const ServiceDispatcher&) = delete; 48 void operator=(const ServiceDispatcher&) = delete; 49 }; 50 51 } // namespace uds 52 } // namespace pdx 53 } // namespace android 54 55 #endif // ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_ 56