E-MailRelay
geventemitter.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file geventemitter.cpp
19///
20
21#include "gdef.h"
22#include "geventemitter.h"
23#include "gnetdone.h"
25#include "glog.h"
26#include "gassert.h"
27
29= default ;
30
32 m_handler(handler) ,
33 m_es(es)
34{
35}
36
38{
39 m_handler = handler ;
40 m_es = es ;
41}
42
44{
45 raiseEvent( &EventHandler::readEvent , fd ) ;
46}
47
49{
50 raiseEvent( &EventHandler::writeEvent , fd ) ;
51}
52
53void GNet::EventEmitter::raiseOtherEvent( Descriptor fd , EventHandler::Reason reason )
54{
55 raiseEvent( &EventHandler::otherEvent , fd , reason ) ;
56}
57
58void GNet::EventEmitter::raiseEvent( void (EventHandler::*method)() , Descriptor )
59{
60 // see also: std::make_exception_ptr, std::rethrow_exception
61
62 EventLoggingContext set_logging_context( m_es.esrc() ) ;
63 m_es_saved = m_es ; // in case the fd gets closed and re-opened when calling the event handler
64 try
65 {
66 if( m_handler != nullptr )
67 (m_handler->*method)() ; // EventHandler::readEvent()/writeEvent()
68 }
69 catch( GNet::Done & e )
70 {
71 if( m_es_saved.set() )
72 m_es_saved.call( e , true ) ; // ExceptionHandler::onException()
73 else
74 throw ;
75 }
76 catch( std::exception & e )
77 {
78 if( m_es_saved.set() )
79 m_es_saved.call( e , false ) ; // ExceptionHandler::onException()
80 else
81 throw ;
82 }
83}
84
85void GNet::EventEmitter::raiseEvent( void (EventHandler::*method)(EventHandler::Reason) ,
86 Descriptor , EventHandler::Reason reason )
87{
88 EventLoggingContext set_logging_context( m_handler && m_es.set() ? m_es.esrc() : nullptr ) ;
89 m_es_saved = m_es ; // (fd might get closed and reopened with a different exception sink)
90 try
91 {
92 if( m_handler != nullptr )
93 (m_handler->*method)( reason ) ; // EventHandler::otherEvent()
94 }
95 catch( GNet::Done & e )
96 {
97 if( m_es_saved.set() )
98 m_es_saved.call( e , true ) ; // ExceptionHandler::onException()
99 else
100 throw ;
101 }
102 catch( std::exception & e )
103 {
104 if( m_es_saved.set() )
105 m_es_saved.call( e , false ) ; // ExceptionHandler::onException()
106 else
107 throw ;
108 }
109}
110
112{
113 m_handler = nullptr ;
114}
115
117{
118 if( m_es.eh() == eh )
119 m_es.reset() ;
120 if( m_es_saved.eh() == eh )
121 m_es_saved.reset() ;
122}
123
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
An exception class that is detected by GNet::EventHandlerList and results in onException() being call...
Definition: gnetdone.h:40
An EventHandler and ExceptionSink tuple, with methods to raise an event and handle any exceptions.
Definition: geventemitter.h:46
void raiseWriteEvent(Descriptor)
Calls the EventHandler writeEvent() method.
void update(EventHandler *, ExceptionSink) noexcept
Sets the event handler and the exception sink.
void reset() noexcept
Resets the EventHandler so that the raise methods do nothing.
void raiseReadEvent(Descriptor)
Calls the EventHandler readEvent() method.
void disarm(ExceptionHandler *) noexcept
If the exception handler matches then reset it so that it is not called.
EventEmitter() noexcept
Default constructor.
void raiseOtherEvent(Descriptor, EventHandler::Reason)
Calls the EventHandler otherEvent() method.
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
Definition: geventhandler.h:48
virtual void readEvent()
Called for a read event.
virtual void writeEvent()
Called for a write event.
virtual void otherEvent(Reason)
Called for a socket-exception event, or a socket-close event on windows.
A class that sets the G::LogOuput::context() while in scope.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Network classes.
Definition: gdef.h:1144