E-MailRelay
geventemitter.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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#include <functional>
28
29namespace GNet
30{
31 namespace EventEmitterImp
32 {
33 template <typename T> void raiseEvent( T handler , EventState & es ) ;
34
35 // for cleaner stack traces compared to std::bind ...
36 struct Binder1
37 {
38 using Method = void (GNet::EventHandler::*)() ;
39 Method m_method ;
40 EventHandler * m_handler ;
41 void operator()() { (m_handler->*m_method)() ; }
42 } ;
43 struct Binder2
44 {
45 using Method = void (GNet::EventHandler::*)( EventHandler::Reason ) ;
46 Method m_method ;
47 EventHandler * m_handler ;
48 EventHandler::Reason m_reason ;
49 void operator()() { (m_handler->*m_method)( m_reason ) ; }
50 } ;
51 Binder1 bind( Binder1::Method method , EventHandler * handler ) { return {method,handler} ; }
52 Binder2 bind( Binder2::Method method , EventHandler * handler , EventHandler::Reason reason ) { return {method,handler,reason} ; }
53 }
54}
55
56template <typename T>
57void GNet::EventEmitterImp::raiseEvent( T handler , EventState & es )
58{
59 // see also: std::make_exception_ptr, std::rethrow_exception
60
61 EventLoggingContext set_logging_context( es ) ;
62 try
63 {
64 handler() ; // eg. EventHandler::readEvent()
65 }
66 catch( GNet::Done & e )
67 {
68 if( es.hasExceptionHandler() )
69 es.doOnException( e , true ) ;
70 else
71 throw ;
72 }
73 catch( std::exception & e )
74 {
75 if( es.hasExceptionHandler() )
76 es.doOnException( e , false ) ;
77 else
78 throw ;
79 }
80}
81
82// --
83
85{
86 namespace imp = EventEmitterImp ;
87 if( handler )
88 imp::raiseEvent( imp::bind(&EventHandler::readEvent,handler) , es ) ;
89}
90
92{
93 namespace imp = EventEmitterImp ;
94 if( handler )
95 imp::raiseEvent( imp::bind(&EventHandler::writeEvent,handler) , es ) ;
96}
97
98void GNet::EventEmitter::raiseOtherEvent( EventHandler * handler , EventState & es , EventHandler::Reason reason )
99{
100 namespace imp = EventEmitterImp ;
101 if( handler )
102 imp::raiseEvent( imp::bind(&EventHandler::otherEvent,handler,reason) , es ) ;
103}
104
An exception class that is caught separately by GNet::EventEmitter and GNet::TimerList so that onExce...
Definition: gnetdone.h:40
static void raiseReadEvent(EventHandler *, EventState &)
Calls readEvent() on the event handler and catches any exceptions and delivers them to the EventState...
static void raiseOtherEvent(EventHandler *, EventState &, EventHandler::Reason)
Calls otherEvent() on the event handler and catches any exceptions and delivers them to the EventStat...
static void raiseWriteEvent(EventHandler *, EventState &)
Calls writeEvent() on the event handler and catches any exceptions and delivers them to the EventStat...
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 lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
Network classes.
Definition: gdef.h:1243