libassa  3.5.1
FdSet.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // FdSet.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 2006 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 
13 #include "FdSet.h"
14 #include "Logger.h"
15 
16 using namespace ASSA;
17 
18 bool
20 setFd (handler_t fd_)
21 {
22  FD_SET (fd_, this);
23 
24 #if !defined (WIN32)
25  ActiveFDs_Iter iter;
26  iter = std::find (m_actfds.begin (),
27  m_actfds.end (),
28  fd_);
29  if (iter == m_actfds.end ()) { // not found
30  m_actfds.push_back (fd_);
31  }
32 #endif
33 
34  return true;
35 }
36 
37 bool
39 clear (handler_t fd_)
40 {
41  DL ((REACT,"Clearing fd=%d\n", fd_));
42 
43  if (!isSet (fd_)) {
44  DL ((REACT,"Not set! - ignoring.\n"));
45  return false;
46  }
47 
48  FD_CLR (fd_, this);
49  if (FD_ISSET (fd_, this)) {
50  DL ((REACT,"Woop - an error! FD_CLR failed!\n"));
51  }
52 
53 #if !defined (WIN32)
54  ActiveFDs_Iter iter;
55  iter = std::find (m_actfds.begin (),
56  m_actfds.end (),
57  fd_);
58  if (iter != m_actfds.end ()) {
59  DL ((REACT,"fd=%d found and erased\n", fd_));
60  m_actfds.erase (iter);
61  }
62  else {
63  DL ((REACT,"fd=%d not found in m_actfds list!\n", fd_));
64  }
65 #endif
66 
67  return true;
68 }
69 
70 void
72 sync ()
73 {
74 #if !defined (WIN32)
75  ActiveFDs_Iter iter;
76  restart:
77  iter = m_actfds.begin ();
78  while (iter != m_actfds.end ()) {
79  if (!isSet (*iter)) {
80  m_actfds.erase (iter);
81  goto restart;
82  }
83  iter++;
84  }
85 #endif
86 }
87 
88 void
90 reset ()
91 {
92  ::memset(this, 0, sizeof (fd_set));
93 
94 #if !defined (WIN32)
95  m_actfds.clear ();
96 #endif
97 }
98 
99 int
101 maxInSet ()
102 {
103 #if defined (WIN32)
104  return 0; // win32 select doesn't need this value
105 #else
106  if (m_actfds.size () == 0) {
107  return 0;
108  }
109  ActiveFDs_Iter iter = std::max_element (m_actfds.begin (), m_actfds.end ());
110  return (*iter);
111 #endif
112 }
113 
114 std::string
116 dump_c_str ()
117 {
118  std::ostringstream report;
119 
120  report << " enabled=" << numSet ();
121 
122 #if defined (WIN32)
123  if (this->fd_count) {
124  report << " : ";
125  }
126  for (int i=0; i < this->fd_count; i++) {
127  report << " " << this->fd_array[i];
128  }
129 #else /* UNIX */
130  ActiveFDs_Iter iter = m_actfds.begin ();
131  if (m_actfds.size ()) {
132  report << " : ";
133  }
134  while (iter != m_actfds.end ()) {
135  report << " " << (u_int)*iter;
136  iter++;
137  }
138 #endif
139 
140  report << std::ends;
141  return (report.str ());
142 }
143 
An abstraction to message logging facility.
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
int handler_t
Definition: Logger_Impl.h:82
unsigned int u_int
Definition: Logger_Impl.h:40
std::list< u_int >::iterator ActiveFDs_Iter
Definition: FdSet.h:110
std::list< u_int > m_actfds
Definition: FdSet.h:112
void reset()
Reset every bit in the set (OFF).
Definition: FdSet.cpp:90
bool setFd(handler_t fd_)
Set flag (ON) for the argument fd.
Definition: FdSet.cpp:20
void sync()
Sync internals after used by select(3C)
Definition: FdSet.cpp:72
std::string dump_c_str()
Return object state dump as an ASCII string.
Definition: FdSet.cpp:116
int maxInSet()
Find out the highest file descriptor in the set.
Definition: FdSet.cpp:101
bool isSet(handler_t fd_)
Test whether fd's flag is on.
Definition: FdSet.h:122
bool clear(handler_t fd_)
Clear flag (OFF) for the argument fd.
Definition: FdSet.cpp:39
int numSet()
Determine how many bits are set (ON) in the set.
Definition: FdSet.h:126
Definition: Acceptor.h:40
@ REACT
Class Reactor/PrioriyQueue messages
Definition: LogMask.h:39
Socket & ends(Socket &os_)
ends manipulator.
Definition: Socket.h:622