1
0

SSDPAPI.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include "nx/nxstring.h"
  3. #include "nx/nxuri.h"
  4. #include <vector>
  5. #include "nu/AutoLock.h"
  6. #include "nx/nxthread.h"
  7. #include "foundation/error.h"
  8. #include "jnetlib/jnetlib.h"
  9. #include "cb_ssdp.h"
  10. #include "nu/ThreadLoop.h"
  11. #include "api_ssdp.h"
  12. #include "nswasabi/ServiceName.h"
  13. class SSDPAPI : public api_ssdp
  14. {
  15. public:
  16. SSDPAPI();
  17. ~SSDPAPI();
  18. WASABI_SERVICE_NAME("SSDP API");
  19. int Initialize();
  20. int WASABICALL SSDP_RegisterService(nx_uri_t location, nx_string_t st, nx_string_t usn);
  21. int WASABICALL SSDP_RegisterCallback(cb_ssdp *callback);
  22. int WASABICALL SSDP_UnregisterCallback(cb_ssdp *callback);
  23. int WASABICALL SSDP_Search(nx_string_t st);
  24. private:
  25. struct Service
  26. {
  27. nx_uri_t location;
  28. nx_string_t type;
  29. nx_string_t usn;
  30. time_t last_seen;
  31. time_t expiry;
  32. };
  33. // TODO: map it off of USN
  34. typedef std::vector<Service> ServiceList;
  35. typedef std::vector<cb_ssdp*> CallbackList;
  36. nx_thread_return_t Internal_Run();
  37. ns_error_t Internal_Notify(const char *st, sockaddr *addr, socklen_t length);
  38. void Internal_RegisterCallback(cb_ssdp *callback);
  39. void Internal_UnregisterCallback(cb_ssdp *callback);
  40. void Internal_Search(nx_string_t st);
  41. static nx_thread_return_t NXTHREADCALL SSDPThread(nx_thread_parameter_t parameter);
  42. int FindUSN(ServiceList &service_list, const char *usn, Service *&service);
  43. ThreadLoop thread_loop;
  44. CallbackList callbacks;
  45. ServiceList services;
  46. ServiceList found_services;
  47. nu::LockGuard services_lock;
  48. nx_thread_t ssdp_thread;
  49. jnl_udp_t listener;
  50. const char *user_agent;
  51. static void APC_RegisterCallback(void *_this, void *_callback, double) { ((SSDPAPI *)_this)->Internal_RegisterCallback((cb_ssdp *)_callback); }
  52. static void APC_UnregisterCallback(void *_this, void *_callback, double) { ((SSDPAPI *)_this)->Internal_UnregisterCallback((cb_ssdp *)_callback); }
  53. static void APC_Search(void *_this, void *st, double) { ((SSDPAPI *)_this)->Internal_Search((nx_string_t)st); }
  54. };