1 | /* |
---|
2 | * Copyright (C) 2005 Mike Wray <mike.wray@hp.com>. |
---|
3 | * |
---|
4 | * This library is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU Lesser General Public License as |
---|
6 | * published by the Free Software Foundation; either version 2.1 of the |
---|
7 | * License, or (at your option) any later version. This library is |
---|
8 | * distributed in the hope that it will be useful, but WITHOUT ANY |
---|
9 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
10 | * FITNESS FOR A PARTICULAR PURPOSE. |
---|
11 | * See the GNU Lesser General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU Lesser General Public License |
---|
14 | * along with this library; if not, write to the Free Software Foundation, |
---|
15 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
16 | */ |
---|
17 | #include <stdlib.h> |
---|
18 | #include <stdbool.h> |
---|
19 | #include <stdio.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <errno.h> |
---|
22 | |
---|
23 | #include "connection.h" |
---|
24 | #include "selector.h" |
---|
25 | |
---|
26 | #define MODULE_NAME "select" |
---|
27 | #define DEBUG 1 |
---|
28 | #undef DEBUG |
---|
29 | #include "debug.h" |
---|
30 | |
---|
31 | void Selector_init(Selector *sel){ |
---|
32 | INIT_LIST_HEAD(&sel->list); |
---|
33 | } |
---|
34 | |
---|
35 | /** Close a selector and remove it from its list. |
---|
36 | * |
---|
37 | * @param sel selector (may be null) |
---|
38 | */ |
---|
39 | void Selector_close(Selector *sel){ |
---|
40 | if(!sel) return; |
---|
41 | dprintf(">\n"); |
---|
42 | if(sel->close){ |
---|
43 | sel->close(sel); |
---|
44 | } |
---|
45 | if(sel->list.next |
---|
46 | && sel->list.next != LIST_POISON1 |
---|
47 | && !list_empty(&sel->list)){ |
---|
48 | list_del_init(&sel->list); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** Add a selector to a select set. |
---|
53 | * The selector is closed if it has no 'select' function, |
---|
54 | * or it has one and it returns an error. |
---|
55 | * |
---|
56 | * @param sel selector |
---|
57 | * @param set select set |
---|
58 | */ |
---|
59 | int Selector_select(Selector *sel, SelectSet *set){ |
---|
60 | int err = -EINVAL; |
---|
61 | dprintf(">\n"); |
---|
62 | if(sel->select){ |
---|
63 | err = sel->select(sel, set); |
---|
64 | } |
---|
65 | if(err){ |
---|
66 | Selector_close(sel); |
---|
67 | } |
---|
68 | return err; |
---|
69 | } |
---|
70 | |
---|
71 | /** Call a selector with a select set. |
---|
72 | * The selector is closed if it has no 'selected' function, |
---|
73 | * or it has one and it returns an error. |
---|
74 | * |
---|
75 | * @param sel selector |
---|
76 | * @param set select set |
---|
77 | */ |
---|
78 | int Selector_selected(Selector *sel, SelectSet *set){ |
---|
79 | int err = -EINVAL; |
---|
80 | dprintf(">\n"); |
---|
81 | if(sel->selected){ |
---|
82 | err = sel->selected(sel, set); |
---|
83 | } |
---|
84 | if(err){ |
---|
85 | Selector_close(sel); |
---|
86 | } |
---|
87 | return err; |
---|
88 | } |
---|
89 | |
---|
90 | int conn_select_fn(Selector *sel, SelectSet *set){ |
---|
91 | int err = -EINVAL; |
---|
92 | Conn *conn = sel->data; |
---|
93 | |
---|
94 | dprintf(">\n"); |
---|
95 | if(conn){ |
---|
96 | err = 0; |
---|
97 | SelectSet_add(set, conn->sock, conn->mode); |
---|
98 | } |
---|
99 | return err; |
---|
100 | } |
---|
101 | |
---|
102 | int conn_selected_fn(Selector *sel, SelectSet *set){ |
---|
103 | int err = -EINVAL; |
---|
104 | Conn *conn = sel->data; |
---|
105 | |
---|
106 | dprintf(">\n"); |
---|
107 | if(conn){ |
---|
108 | err = Conn_handle(conn, set); |
---|
109 | } |
---|
110 | return err; |
---|
111 | } |
---|
112 | |
---|
113 | void conn_close_fn(Selector *sel){ |
---|
114 | Conn *conn = sel->data; |
---|
115 | |
---|
116 | wprintf("> sel=%p\n", sel); |
---|
117 | if(conn){ |
---|
118 | Conn_close(conn); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void Selector_conn_init(Selector *sel, Conn *conn, |
---|
123 | int mode, void *data, |
---|
124 | int (*fn)(struct Conn *conn, int mode)){ |
---|
125 | conn->mode = SELECT_READ; |
---|
126 | conn->data = data; |
---|
127 | conn->fn = fn; |
---|
128 | sel->data = conn; |
---|
129 | sel->select = conn_select_fn; |
---|
130 | sel->close = conn_close_fn; |
---|
131 | sel->selected = conn_selected_fn; |
---|
132 | } |
---|
133 | |
---|