current version
[opsoft_test.git] / gclib2 / modules / Net / INET / Poll.cxx
blob1052998da5f9835fe8c984f44482a643ab45659a
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include <gclib2.h>
8 #include <Poll.h>
9 #include <sys/poll.h>
11 Poll::Poll ()
13 m_connections = new List;
14 pull = NULL;
17 Poll::~Poll ()
19 delete m_connections;
22 Connection * Poll::__findConnection (Connection * c)
24 m_connections->first ();
25 while (true) {
26 if (m_connections->get () == NULL)
27 return NULL;
28 if (((Connection *) m_connections->get ()) == c)
29 return c;
30 m_connections->next ();
32 return NULL;
35 Connection * Poll::__findName (char * str)
37 Connection * c;
38 m_connections->first ();
39 while (true) {
40 c = (Connection *) m_connections->get ();
41 if (! c)
42 return NULL;
43 if (EQ (c->name (), str))
44 return c;
45 m_connections->next ();
47 return NULL;
50 Connection * Poll::add (Connection * c)
52 m_connections->add (LPCHAR (c));
53 return c;
56 Connection * Poll::unlink (Connection * c)
58 if (__findConnection (c))
59 m_connections->rm ();
60 return c;
63 pollfd * Poll::poll_build (int * nfds)
65 int count;
66 int i = 0;
67 Connection * c;
69 if (pull)
70 DROP (pull);
72 count = m_connections->count ();
73 pull = CNEW (pollfd, count);
74 m_connections->first ();
75 while (true) {
76 c = (Connection *) m_connections->get ();
77 if (! c)
78 break;
80 pull[i].fd = c->socket ();
81 pull[i].events = c->pollFlags();
82 pull[i].revents = 0;
84 m_connections->next ();
85 ++i;
88 if (nfds)
89 *nfds = count;
91 return pull;
94 Connection * Poll::scan ()
96 Connection * c;
97 int p_pos = 0;
99 if (! pull)
100 return NULL;
102 m_connections->first ();
103 while (true) {
104 c = (Connection *) m_connections->get ();
105 if (! c)
106 break;
107 if (pull[p_pos].revents) {
108 c->setRevents (pull[p_pos].revents);
109 pull[p_pos].revents = 0;
110 return c;
112 ++p_pos;
113 m_connections->next ();
116 return NULL;
119 int Poll::poll (int timeout)
121 int nfds;
122 poll_build (&nfds);
123 return ::poll (pull, nfds, timeout);
126 int Poll::count ()
128 return m_connections->count ();
131 List * Poll::connections ()
133 List * l;
134 l = new List;
135 *l = *m_connections;
136 return l;