SocketNotifier.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // SocketNotifier.cpp
  3. //
  4. // Library: Net
  5. // Package: Reactor
  6. // Module: SocketNotifier
  7. //
  8. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Net/SocketNotifier.h"
  14. #include "Poco/Net/SocketReactor.h"
  15. #include "Poco/Net/SocketNotification.h"
  16. namespace Poco {
  17. namespace Net {
  18. SocketNotifier::SocketNotifier(const Socket& socket):
  19. _socket(socket)
  20. {
  21. }
  22. SocketNotifier::~SocketNotifier()
  23. {
  24. }
  25. void SocketNotifier::addObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
  26. {
  27. _nc.addObserver(observer);
  28. if (observer.accepts(pReactor->_pReadableNotification))
  29. _events.insert(pReactor->_pReadableNotification.get());
  30. else if (observer.accepts(pReactor->_pWritableNotification))
  31. _events.insert(pReactor->_pWritableNotification.get());
  32. else if (observer.accepts(pReactor->_pErrorNotification))
  33. _events.insert(pReactor->_pErrorNotification.get());
  34. else if (observer.accepts(pReactor->_pTimeoutNotification))
  35. _events.insert(pReactor->_pTimeoutNotification.get());
  36. }
  37. void SocketNotifier::removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
  38. {
  39. _nc.removeObserver(observer);
  40. EventSet::iterator it = _events.end();
  41. if (observer.accepts(pReactor->_pReadableNotification))
  42. it = _events.find(pReactor->_pReadableNotification.get());
  43. else if (observer.accepts(pReactor->_pWritableNotification))
  44. it = _events.find(pReactor->_pWritableNotification.get());
  45. else if (observer.accepts(pReactor->_pErrorNotification))
  46. it = _events.find(pReactor->_pErrorNotification.get());
  47. else if (observer.accepts(pReactor->_pTimeoutNotification))
  48. it = _events.find(pReactor->_pTimeoutNotification.get());
  49. if (it != _events.end())
  50. _events.erase(it);
  51. }
  52. namespace
  53. {
  54. static Socket nullSocket;
  55. }
  56. void SocketNotifier::dispatch(SocketNotification* pNotification)
  57. {
  58. pNotification->setSocket(_socket);
  59. pNotification->duplicate();
  60. try
  61. {
  62. _nc.postNotification(pNotification);
  63. }
  64. catch (...)
  65. {
  66. pNotification->setSocket(nullSocket);
  67. throw;
  68. }
  69. pNotification->setSocket(nullSocket);
  70. }
  71. } } // namespace Poco::Net