E-MailRelay

geventhandler.h
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 geventhandler.h
19///
20
21#ifndef G_NET_EVENT_HANDLER_H
22#define G_NET_EVENT_HANDLER_H
23
24#include "gdef.h"
25#include "gdatetime.h"
26#include "gexceptionhandler.h"
27#include "gdescriptor.h"
28#include <vector>
29#include <string>
30
31 namespace GNet
32{
33 class EventHandler ;
34}
35
36//| \class GNet::EventHandler
37/// A base class for classes that have a file descriptor and handle
38/// asynchronous events from the event loop.
39///
40/// The event handler virtual methods are called when an event is
41/// detected on the associated file descriptor.
42///
43/// The EventEmitter class ensures that if an exception is thrown out
44/// of an event handler it is caught and delivered to an associated
45/// ExceptionHandler interface (if any).
46///
47 class GNet::EventHandler
48{
49 public:
50 enum class Reason
51 {
52 failed , // connection failed
53 closed , // fin packet, clean shutdown
54 down , // network down
55 reset , // rst packet
56 abort , // socket failed
57 other // eg. oob data
58 } ;
59
60 EventHandler() ;
61 ///< Constructor.
62
63 virtual ~EventHandler() ;
64 ///< Destructor.
65
66 virtual void readEvent() ;
67 ///< Called for a read event. Overridable. The default
68 ///< implementation does nothing. The descriptor might
69 ///< not be valid() if a non-socket event on windows.
70
71 virtual void writeEvent() ;
72 ///< Called for a write event. Overrideable. The default
73 ///< implementation does nothing.
74
75 virtual void otherEvent( Reason ) ;
76 ///< Called for a socket-exception event, or a socket-close
77 ///< event on windows. Overridable. The default
78 ///< implementation throws an exception.
79
80 static std::string str( Reason ) ;
81 ///< Returns a printable description of the other-event
82 ///< reason.
83
84 void setDescriptor( Descriptor ) noexcept ;
85 ///< File descriptor setter. Used by the event loop.
86
87 Descriptor descriptor() const noexcept ;
88 ///< File descriptor getter. Used by the event loop.
89
90public:
91 EventHandler( const EventHandler & ) = delete ;
92 EventHandler( EventHandler && ) = delete ;
93 EventHandler & operator=( const EventHandler & ) = delete ;
94 EventHandler & operator=( EventHandler && ) = delete ;
95
96private:
97 Descriptor m_fd ;
98} ;
99
100inline
101 void GNet::EventHandler::setDescriptor( Descriptor fd ) noexcept
102{
103 m_fd = fd ;
104}
105
106 inline
107 GNet::Descriptor GNet::EventHandler::descriptor() const noexcept
108{
109 return m_fd ;
110}
111
112#endif
GNet::Descriptor
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
GNet::EventHandler
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
Definition: geventhandler.h:48
GNet::EventHandler::readEvent
virtual void readEvent()
Called for a read event.
Definition: geventhandler.cpp:39
GNet::EventHandler::writeEvent
virtual void writeEvent()
Called for a write event.
Definition: geventhandler.cpp:44
GNet::EventHandler::~EventHandler
virtual ~EventHandler()
Destructor.
Definition: geventhandler.cpp:30
GNet::EventHandler::EventHandler
EventHandler()
Constructor.
GNet::EventHandler::otherEvent
virtual void otherEvent(Reason)
Called for a socket-exception event, or a socket-close event on windows.
Definition: geventhandler.cpp:49
GNet::EventHandler::str
static std::string str(Reason)
Returns a printable description of the other-event reason.
Definition: geventhandler.cpp:54
GNet::EventHandler::setDescriptor
void setDescriptor(Descriptor) noexcept
File descriptor setter. Used by the event loop.
Definition: geventhandler.h:101
GNet::EventHandler::descriptor
Descriptor descriptor() const noexcept
File descriptor getter. Used by the event loop.
Definition: geventhandler.h:107
gdatetime.h
gdescriptor.h
gexceptionhandler.h
GNet
Network classes.
Definition: gdef.h:1243