- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

geventhandler.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // geventhandler.cpp
19 //
20
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "geventhandler.h"
24 #include "geventloop.h"
25 #include "gexception.h"
26 #include "gdebug.h"
27 #include "gassert.h"
28 #include "gdescriptor.h"
29 #include "glog.h"
30
31 GNet::EventHandler::~EventHandler()
32 {
33 }
34
35 void GNet::EventHandler::readEvent()
36 {
37 G_DEBUG( "GNet::EventHandler::readEvent: no override" ) ;
38 }
39
40 void GNet::EventHandler::writeEvent()
41 {
42 G_DEBUG( "GNet::EventHandler::writeEvent: no override" ) ;
43 }
44
45 void GNet::EventHandler::exceptionEvent()
46 {
47 throw G::Exception( "exception event" ) ;
48 }
49
50 // ===
51
52 GNet::EventHandlerList::EventHandlerList( const std::string & type ) :
53 m_type(type) ,
54 m_lock(0U) ,
55 m_has_garbage(false)
56 {
57 }
58
59 GNet::EventHandlerList::Iterator GNet::EventHandlerList::begin() const
60 {
61 return m_map.begin() ;
62 }
63
64 GNet::EventHandlerList::Iterator GNet::EventHandlerList::end() const
65 {
66 return m_map.end() ;
67 }
68
69 bool GNet::EventHandlerList::contains( Descriptor fd ) const
70 {
71 return m_map.find(fd) != m_map.end() ;
72 }
73
74 GNet::EventHandler * GNet::EventHandlerList::find( Descriptor fd )
75 {
76 Map::iterator p = m_map.find( fd ) ;
77 return p != m_map.end() ? (*p).second : NULL ;
78 }
79
80 void GNet::EventHandlerList::add( Descriptor fd , EventHandler * handler )
81 {
82 G_ASSERT( handler != NULL ) ;
83 G_DEBUG( "GNet::EventHandlerList::add: " << m_type << "-list: " << "adding " << fd ) ;
84
85 m_map[fd] = handler ;
86 }
87
88 void GNet::EventHandlerList::remove( Descriptor fd )
89 {
90 Map::iterator p = m_map.find( fd ) ;
91 if( p != m_map.end() )
92 {
93 G_DEBUG( "GNet::EventHandlerList::remove: " << m_type << "-list: " << "removing " << fd ) ;
94 if( m_lock )
95 {
96 (*p).second = NULL ;
97 m_has_garbage = true ;
98 }
99 else
100 {
101 m_map.erase( p ) ;
102 }
103 }
104 }
105
106 void GNet::EventHandlerList::lock()
107 {
108 m_lock++ ;
109 }
110
111 void GNet::EventHandlerList::unlock()
112 {
113 G_ASSERT( m_lock != 0U ) ;
114 m_lock-- ;
115 if( m_lock == 0U && m_has_garbage )
116 collectGarbage() ;
117 }
118
119 void GNet::EventHandlerList::collectGarbage()
120 {
121 const Map::iterator end = m_map.end() ;
122 for( Map::iterator p = m_map.begin() ; p != end ; )
123 {
124 Map::iterator test = p++ ;
125 if( (*test).second == NULL )
126 m_map.erase( test ) ;
127 }
128 m_has_garbage = false ;
129 }
130
glog.h
GNet::EventHandlerList::begin
Iterator begin() const
Returns a forward iterator.
Definition: geventhandler.cpp:59
GNet::EventHandler::readEvent
virtual void readEvent()
Called for a read event.
Definition: geventhandler.cpp:35
GNet::Descriptor
A network file descriptor.
Definition: gdescriptor.h:37
geventloop.h
GNet::EventHandlerList::lock
void lock()
Called at the start of an iteration which might change the list.
Definition: geventhandler.cpp:106
G_ASSERT
#define G_ASSERT(test)
Definition: gassert.h:30
gassert.h
GNet::EventHandler::writeEvent
virtual void writeEvent()
Called for a write event.
Definition: geventhandler.cpp:40
GNet::EventHandlerList::Iterator
Map::const_iterator Iterator
Definition: geventhandler.h:100
GNet::EventHandlerList::contains
bool contains(Descriptor fd) const
Returns true if the list contains the given file-descriptor.
Definition: geventhandler.cpp:69
geventhandler.h
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
GNet::EventHandlerList::add
void add(Descriptor fd, EventHandler *handler)
Adds a file-descriptor/handler pair to the list.
Definition: geventhandler.cpp:80
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
GNet::EventHandlerList::unlock
void unlock()
Called at the end of an iteration.
Definition: geventhandler.cpp:111
GNet::EventHandlerList::remove
void remove(Descriptor fd)
Removes a file-descriptor from the list.
Definition: geventhandler.cpp:88
gdescriptor.h
gexception.h
GNet::EventHandlerList::end
Iterator end() const
Returns an end iterator.
Definition: geventhandler.cpp:64
G::Exception
A general-purpose exception class derived from std::exception and containing a std::string.
Definition: gexception.h:44
GNet::EventHandlerList::EventHandlerList
EventHandlerList(const std::string &type)
Constructor.
Definition: geventhandler.cpp:52
GNet::EventHandlerList::find
EventHandler * find(Descriptor fd)
Finds the handler associated with the given file descriptor.
Definition: geventhandler.cpp:74
GNet::EventHandler::~EventHandler
virtual ~EventHandler()
Destructor.
Definition: geventhandler.cpp:31
gdebug.h
GNet::EventHandler::exceptionEvent
virtual void exceptionEvent()
Called for an exception event.
Definition: geventhandler.cpp:45