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 if (observer.accepts(pReactor->_pReadableNotification))
39 _events.insert(pReactor->_pReadableNotification.get());
40 else if (observer.accepts(pReactor->_pWritableNotification))
41 _events.insert(pReactor->_pWritableNotification.get());
42 else if (observer.accepts(pReactor->_pErrorNotification))
43 _events.insert(pReactor->_pErrorNotification.get());
44 else if (observer.accepts(pReactor->_pTimeoutNotification))
45 _events.insert(pReactor->_pTimeoutNotification.get());
46}
47
48
49void SocketNotifier::removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
50{
51 _nc.removeObserver(observer);
52 EventSet::iterator it = _events.end();
53 if (observer.accepts(pReactor->_pReadableNotification))
54 it = _events.find(pReactor->_pReadableNotification.get());
55 else if (observer.accepts(pReactor->_pWritableNotification))
56 it = _events.find(pReactor->_pWritableNotification.get());
57 else if (observer.accepts(pReactor->_pErrorNotification))
58 it = _events.find(pReactor->_pErrorNotification.get());
59 else if (observer.accepts(pReactor->_pTimeoutNotification))
60 it = _events.find(pReactor->_pTimeoutNotification.get());
61 if (it != _events.end())
62 _events.erase(it);
63}
64
65
66namespace
67{
68 static Socket nullSocket;
69}
70
71
72void SocketNotifier::dispatch(SocketNotification* pNotification)
73{
74 pNotification->setSocket(_socket);
75 pNotification->duplicate();
76 try
77 {
78 _nc.postNotification(pNotification);
79 }
80 catch (...)
81 {
82 pNotification->setSocket(nullSocket);
83 throw;
84 }
85 pNotification->setSocket(nullSocket);
86}
87
88
89} } // namespace Poco::Net
90