libassa
3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // FdSet.cpp 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 2006 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 00013 #include "FdSet.h" 00014 #include "Logger.h" 00015 00016 using namespace ASSA; 00017 00018 bool 00019 FdSet:: 00020 setFd (handler_t fd_) 00021 { 00022 FD_SET (fd_, this); 00023 00024 #if !defined (WIN32) 00025 ActiveFDs_Iter iter; 00026 iter = std::find (m_actfds.begin (), 00027 m_actfds.end (), 00028 fd_); 00029 if (iter == m_actfds.end ()) { // not found 00030 m_actfds.push_back (fd_); 00031 } 00032 #endif 00033 00034 return true; 00035 } 00036 00037 bool 00038 FdSet:: 00039 clear (handler_t fd_) 00040 { 00041 DL ((REACT,"Clearing fd=%d\n", fd_)); 00042 00043 if (!isSet (fd_)) { 00044 DL ((REACT,"Not set! - ignoring.\n")); 00045 return false; 00046 } 00047 00048 FD_CLR (fd_, this); 00049 if (FD_ISSET (fd_, this)) { 00050 DL ((REACT,"Woop - an error! FD_CLR failed!\n")); 00051 } 00052 00053 #if !defined (WIN32) 00054 ActiveFDs_Iter iter; 00055 iter = std::find (m_actfds.begin (), 00056 m_actfds.end (), 00057 fd_); 00058 if (iter != m_actfds.end ()) { 00059 DL ((REACT,"fd=%d found and erased\n", fd_)); 00060 m_actfds.erase (iter); 00061 } 00062 else { 00063 DL ((REACT,"fd=%d not found in m_actfds list!\n", fd_)); 00064 } 00065 #endif 00066 00067 return true; 00068 } 00069 00070 void 00071 FdSet:: 00072 sync () 00073 { 00074 #if !defined (WIN32) 00075 ActiveFDs_Iter iter; 00076 restart: 00077 iter = m_actfds.begin (); 00078 while (iter != m_actfds.end ()) { 00079 if (!isSet (*iter)) { 00080 m_actfds.erase (iter); 00081 goto restart; 00082 } 00083 iter++; 00084 } 00085 #endif 00086 } 00087 00088 void 00089 FdSet:: 00090 reset () 00091 { 00092 ::memset(this, 0, sizeof (fd_set)); 00093 00094 #if !defined (WIN32) 00095 m_actfds.clear (); 00096 #endif 00097 } 00098 00099 int 00100 FdSet:: 00101 maxInSet () 00102 { 00103 #if defined (WIN32) 00104 return 0; // win32 select doesn't need this value 00105 #else 00106 if (m_actfds.size () == 0) { 00107 return 0; 00108 } 00109 ActiveFDs_Iter iter = std::max_element (m_actfds.begin (), m_actfds.end ()); 00110 return (*iter); 00111 #endif 00112 } 00113 00114 std::string 00115 FdSet:: 00116 dump_c_str () 00117 { 00118 std::ostringstream report; 00119 00120 report << " enabled=" << numSet (); 00121 00122 #if defined (WIN32) 00123 if (this->fd_count) { 00124 report << " : "; 00125 } 00126 for (int i=0; i < this->fd_count; i++) { 00127 report << " " << this->fd_array[i]; 00128 } 00129 #else /* UNIX */ 00130 ActiveFDs_Iter iter = m_actfds.begin (); 00131 if (m_actfds.size ()) { 00132 report << " : "; 00133 } 00134 while (iter != m_actfds.end ()) { 00135 report << " " << (u_int)*iter; 00136 iter++; 00137 } 00138 #endif 00139 00140 report << std::ends; 00141 return (report.str ()); 00142 } 00143