SocketNotifier.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // SocketNotifier.h
  3. //
  4. // Library: Net
  5. // Package: Reactor
  6. // Module: SocketNotifier
  7. //
  8. // Definition of the SocketNotifier class.
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Net_SocketNotifier_INCLUDED
  16. #define Net_SocketNotifier_INCLUDED
  17. #include "Poco/Net/Net.h"
  18. #include "Poco/Net/Socket.h"
  19. #include "Poco/RefCountedObject.h"
  20. #include "Poco/NotificationCenter.h"
  21. #include "Poco/Observer.h"
  22. #include <set>
  23. namespace Poco {
  24. namespace Net {
  25. class Socket;
  26. class SocketReactor;
  27. class SocketNotification;
  28. class Net_API SocketNotifier: public Poco::RefCountedObject
  29. /// This class is used internally by SocketReactor
  30. /// to notify registered event handlers of socket events.
  31. {
  32. public:
  33. explicit SocketNotifier(const Socket& socket);
  34. /// Creates the SocketNotifier for the given socket.
  35. void addObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer);
  36. /// Adds the given observer.
  37. void removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer);
  38. /// Removes the given observer.
  39. bool hasObserver(const Poco::AbstractObserver& observer) const;
  40. /// Returns true if the given observer is registered.
  41. bool accepts(SocketNotification* pNotification);
  42. /// Returns true if there is at least one observer for the given notification.
  43. void dispatch(SocketNotification* pNotification);
  44. /// Dispatches the notification to all observers.
  45. bool hasObservers() const;
  46. /// Returns true if there are subscribers.
  47. std::size_t countObservers() const;
  48. /// Returns the number of subscribers;
  49. protected:
  50. ~SocketNotifier();
  51. /// Destroys the SocketNotifier.
  52. private:
  53. typedef std::multiset<SocketNotification*> EventSet;
  54. EventSet _events;
  55. Poco::NotificationCenter _nc;
  56. Socket _socket;
  57. };
  58. //
  59. // inlines
  60. //
  61. inline bool SocketNotifier::accepts(SocketNotification* pNotification)
  62. {
  63. return _events.find(pNotification) != _events.end();
  64. }
  65. inline bool SocketNotifier::hasObserver(const Poco::AbstractObserver& observer) const
  66. {
  67. return _nc.hasObserver(observer);
  68. }
  69. inline bool SocketNotifier::hasObservers() const
  70. {
  71. return _nc.hasObservers();
  72. }
  73. inline std::size_t SocketNotifier::countObservers() const
  74. {
  75. return _nc.countObservers();
  76. }
  77. } } // namespace Poco::Net
  78. #endif // Net_SocketNotifier_INCLUDED