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
19 FdSet::
setFd(handler_t fd_)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
38 FdSet::
clear(handler_t fd_)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
71 FdSet::
sync()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
89 FdSet::
reset()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
100 FdSet::
maxInSet()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
115 FdSet::
dump_c_str()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 
144