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
14
15#include "Poco/Net/SocketNotifier.h"
16#include "Poco/Net/SocketReactor.h"
17#include "Poco/Net/SocketNotification.h"
18
19
20namespace Poco {
21namespace Net {
22
23
24SocketNotifier::SocketNotifier(const Socket& socket):
25 _socket(socket)
26{
27}
28
29
30SocketNotifier::~SocketNotifier()
31{
32}
33
34
35void SocketNotifier::addObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
36{
37 _nc.addObserver(observer);
38 ScopedLock l(_mutex);
39 if (observer.accepts(pReactor->_pReadableNotification))
40 _events.insert(pReactor->_pReadableNotification.get());
41 else if (observer.accepts(pReactor->_pWritableNotification))
42 _events.insert(pReactor->_pWritableNotification.get());
43 else if (observer.accepts(pReactor->_pErrorNotification))
44 _events.insert(pReactor->_pErrorNotification.get());
45 else if (observer.accepts(pReactor->_pTimeoutNotification))
46 _events.insert(pReactor->_pTimeoutNotification.get());
47}
48
49
50void SocketNotifier::removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
51{
52 _nc.removeObserver(observer);
53 ScopedLock l(_mutex);
54 EventSet::iterator it = _events.end();
55 if (observer.accepts(pReactor->_pReadableNotification))
56 it = _events.find(pReactor->_pReadableNotification.get());
57 else if (observer.accepts(pReactor->_pWritableNotification))
58 it = _events.find(pReactor->_pWritableNotification.get());
59 else if (observer.accepts(pReactor->_pErrorNotification))
60 it = _events.find(pReactor->_pErrorNotification.get());
61 else if (observer.accepts(pReactor->_pTimeoutNotification))
62 it = _events.find(pReactor->_pTimeoutNotification.get());
63 if (it != _events.end())
64 _events.erase(it);
65}
66
67
68namespace
69{
70 static Socket nullSocket;
71}
72
73
74void SocketNotifier::dispatch(SocketNotification* pNotification)
75{
76 pNotification->setSocket(_socket);
77 pNotification->duplicate();
78 try
79 {
80 _nc.postNotification(pNotification);
81 }
82 catch (...)
83 {
84 pNotification->setSocket(nullSocket);
85 throw;
86 }
87 pNotification->setSocket(nullSocket);
88}
89
90
91} } // namespace Poco::Net
92