E-MailRelay

geventhandler.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 geventhandler.cpp
19///
20
21#include "gdef.h"
22#include "geventhandler.h"
23#include "gexception.h"
24#include "geventloop.h"
25#include "glog.h"
26
27 GNet::EventHandler::EventHandler()
28= default ;
29
30 GNet::EventHandler::~EventHandler()
31{
32 static_assert( noexcept(EventLoop::ptr()) , "" ) ;
33 static_assert( noexcept(EventLoop::ptr()->drop(GNet::Descriptor())) , "" ) ;
34 EventLoop * event_loop = EventLoop::ptr() ;
35 if( event_loop != nullptr )
36 event_loop->drop( m_fd ) ;
37}
38
39 void GNet::EventHandler::readEvent()
40{
41 G_DEBUG( "GNet::EventHandler::readEvent: no override" ) ;
42}
43
44 void GNet::EventHandler::writeEvent()
45{
46 G_DEBUG( "GNet::EventHandler::writeEvent: no override" ) ;
47}
48
49 void GNet::EventHandler::otherEvent( EventHandler::Reason reason )
50{
51 throw G::Exception( "socket disconnect event" , str(reason) ) ;
52}
53
54std::string GNet::EventHandler::str( EventHandler::Reason reason )
55{
56 if( reason == EventHandler::Reason::failed ) return "connection failed" ;
57 if( reason == EventHandler::Reason::closed ) return "closed" ;
58 if( reason == EventHandler::Reason::down ) return "network down" ;
59 if( reason == EventHandler::Reason::reset ) return "connection reset by peer" ;
60 if( reason == EventHandler::Reason::abort ) return "connection aborted" ;
61 return {} ;
62}
63
GNet::Descriptor
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
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::EventLoop
An abstract base class for a singleton that keeps track of open sockets and their associated handlers...
Definition: geventloop.h:63
GNet::EventLoop::drop
virtual void drop(Descriptor fd) noexcept=0
Removes the given event descriptor from the event loop as the EventHandler is being destructed.
GNet::EventLoop::ptr
static EventLoop * ptr() noexcept
Returns a pointer to an instance of the class, if any.
Definition: geventloop.cpp:40
G::Exception
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:64
geventhandler.h
geventloop.h
gexception.h
glog.h