File: FdSet.cpp

package info (click to toggle)
libassa 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,268 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (143 lines) | stat: -rw-r--r-- 2,659 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// -*- c++ -*-
//------------------------------------------------------------------------------
//                               FdSet.cpp
//------------------------------------------------------------------------------
//  Copyright (C) 2006  Vladislav Grinchenko 
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------

#include "FdSet.h"
#include "Logger.h"

using namespace ASSA;

bool
FdSet::
setFd (handler_t fd_) 
{ 
	FD_SET (fd_, this); 

#if !defined (WIN32)
	ActiveFDs_Iter iter;
	iter = std::find (m_actfds.begin (), 
					  m_actfds.end (), 
					  fd_);
	if (iter == m_actfds.end ()) { // not found
		m_actfds.push_back (fd_);
	}
#endif

	return true;
}

bool 
FdSet::
clear (handler_t fd_) 
{
	DL ((REACT,"Clearing fd=%d\n", fd_));

	if (!isSet (fd_)) {
		DL ((REACT,"Not set! - ignoring.\n"));
		return false;
	}

	FD_CLR (fd_, this); 
	if (FD_ISSET (fd_, this)) {
		DL ((REACT,"Woop - an error! FD_CLR failed!\n"));
	}

#if !defined (WIN32)
	ActiveFDs_Iter iter;
	iter = std::find (m_actfds.begin (), 
					  m_actfds.end (), 
					  fd_);
	if (iter != m_actfds.end ()) { 
		DL ((REACT,"fd=%d found and erased\n", fd_));
		m_actfds.erase (iter);
	}
	else {
		DL ((REACT,"fd=%d not found in m_actfds list!\n", fd_));
	}
#endif

	return true;
}

void 
FdSet::
sync () 
{ 
#if !defined (WIN32)
	ActiveFDs_Iter iter;
  restart:
	iter = m_actfds.begin ();
	while (iter != m_actfds.end ()) {
		if (!isSet (*iter)) {
			m_actfds.erase (iter);
			goto restart;
		}
		iter++;
	}
#endif
}

void 
FdSet::
reset () 
{ 
	::memset(this, 0, sizeof (fd_set)); 

#if !defined (WIN32)
	m_actfds.clear ();
#endif
}

int
FdSet::
maxInSet ()
{
#if defined (WIN32)
	return 0;					// win32 select doesn't need this value
#else
	if (m_actfds.size () == 0) {
		return 0;
	}
	ActiveFDs_Iter iter = std::max_element (m_actfds.begin (), m_actfds.end ());
	return (*iter);
#endif
}

std::string
FdSet::
dump_c_str ()
{
	std::ostringstream report;

	report << " enabled=" << numSet ();

#if defined (WIN32)
	if (this->fd_count) {
		report << " : ";
	}
	for (int i=0; i < this->fd_count; i++) {
		report << " " << this->fd_array[i];
	}
#else /* UNIX */
	ActiveFDs_Iter iter = m_actfds.begin ();
	if (m_actfds.size ()) {
		report << " : ";
	}
	while (iter != m_actfds.end ()) {
		report << " " << (u_int)*iter;
		iter++;
	}
#endif

	report << std::ends;
	return (report.str ());
}