|
1432
|
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
2
|
|
|
3 /*
|
|
|
4 $Id: socketmanager.c 1442 2001-01-28 01:52:27Z warmenhoven $
|
|
|
5 */
|
|
|
6
|
|
|
7 #include "socketmanager.h"
|
|
|
8
|
|
|
9 /**
|
|
|
10 * The icqlib socket manager is a simple socket abstraction layer, which
|
|
|
11 * supports opening and closing sockets as well as installing handler
|
|
|
12 * functions for read ready and write ready events. Its purpose is to
|
|
|
13 * both unify socket handling in icqlib and expose icqlib's socket
|
|
|
14 * requirements so the library client can assist with socket housekeeping.
|
|
|
15 *
|
|
|
16 * Library clients have two options to support icqlib:
|
|
|
17 *
|
|
|
18 * 1. Periodically call icq_Main. This will handle all select logic
|
|
|
19 * internally. Advantage is implementation ease, disadvantage is wasted
|
|
|
20 * CPU cycles because of polling and poor TCP file transfer performance.
|
|
|
21 *
|
|
|
22 * 2. Install a icq_SocketNotify callback, perform your own socket
|
|
|
23 * management, and notify icqlib using the icq_SocketReady method when
|
|
|
24 * a socket is ready for reading or writing. Advantage is efficiency,
|
|
|
25 * disadvantage is extra code.
|
|
|
26 *
|
|
|
27 */
|
|
|
28
|
|
|
29 /* need to track:
|
|
|
30 * socket wants read notification
|
|
|
31 * socket no longer wants read notification
|
|
|
32 * socket wants write notification
|
|
|
33 * socket no longer wants write notification
|
|
|
34 */
|
|
|
35
|
|
|
36 #include <sys/types.h>
|
|
|
37
|
|
|
38 #ifndef _WIN32
|
|
|
39 #include <unistd.h>
|
|
|
40 #include <sys/time.h>
|
|
|
41 #include <sys/socket.h>
|
|
|
42 #else
|
|
|
43 #include <winsock.h>
|
|
|
44 #endif
|
|
|
45
|
|
|
46 list *icq_SocketList = NULL;
|
|
|
47 fd_set icq_FdSets[ICQ_SOCKET_MAX];
|
|
|
48 int icq_MaxSocket;
|
|
|
49
|
|
|
50 /**
|
|
|
51 * Creates a new socket using the operating system's socket creation
|
|
|
52 * facitily.
|
|
|
53 */
|
|
|
54 int icq_SocketNew(int domain, int type, int protocol)
|
|
|
55 {
|
|
|
56 int s = socket(domain, type, protocol);
|
|
|
57
|
|
|
58 icq_SocketAlloc(s);
|
|
|
59
|
|
|
60 return s;
|
|
|
61 }
|
|
|
62
|
|
|
63
|
|
|
64 /**
|
|
|
65 * Creates a new socket by accepting a connection from a listening
|
|
|
66 * socket.
|
|
|
67 */
|
|
|
68 int icq_SocketAccept(int listens, struct sockaddr *addr, socklen_t *addrlen)
|
|
|
69 {
|
|
|
70 int s = accept(listens, addr, addrlen);
|
|
|
71
|
|
|
72 icq_SocketAlloc(s);
|
|
|
73
|
|
|
74 return s;
|
|
|
75 }
|
|
|
76
|
|
|
77 /**
|
|
|
78 * Creates a new icq_Socket structure, and appends it to the
|
|
|
79 * socketmanager's global socket list.
|
|
|
80 */
|
|
|
81 void icq_SocketAlloc(int s)
|
|
|
82 {
|
|
|
83 if (s != -1)
|
|
|
84 {
|
|
|
85 icq_Socket *psocket = (icq_Socket *)malloc(sizeof(icq_Socket));
|
|
|
86 int i;
|
|
|
87 psocket->socket = s;
|
|
|
88
|
|
|
89 for (i=0; i<ICQ_SOCKET_MAX; i++)
|
|
|
90 psocket->handlers[i] = NULL;
|
|
|
91
|
|
|
92 list_enqueue(icq_SocketList, psocket);
|
|
|
93 }
|
|
|
94 }
|
|
|
95
|
|
|
96 /**
|
|
|
97 * Closes a socket. This function will notify the library client
|
|
|
98 * through the icq_SocketNotify callback if the socket had an installed
|
|
|
99 * read or write handler.
|
|
|
100 */
|
|
|
101 int icq_SocketDelete(int socket)
|
|
|
102 {
|
|
|
103 #ifdef _WIN32
|
|
|
104 int result = closesocket(socket);
|
|
|
105 #else
|
|
|
106 int result = close(socket);
|
|
|
107 #endif
|
|
|
108
|
|
|
109 if (result != -1)
|
|
|
110 {
|
|
|
111 icq_Socket *s = icq_FindSocket(socket);
|
|
|
112 int i;
|
|
|
113
|
|
|
114 /* uninstall all handlers - this will take care of notifing library
|
|
|
115 * client */
|
|
|
116 for (i=0; i<ICQ_SOCKET_MAX; i++)
|
|
|
117 {
|
|
|
118 if (s->handlers[i])
|
|
|
119 icq_SocketSetHandler(s->socket, i, NULL, NULL);
|
|
|
120 }
|
|
|
121
|
|
|
122 list_remove(icq_SocketList, s);
|
|
|
123 free(s);
|
|
|
124 }
|
|
|
125
|
|
|
126 return result;
|
|
|
127 }
|
|
|
128
|
|
|
129 /**
|
|
|
130 * Installs a socket event handler. The handler will be called when
|
|
|
131 * the socket is ready for reading or writing, depending on the type
|
|
|
132 * which should be either ICQ_SOCKET_READ or ICQ_SOCKET_WRITE. In
|
|
|
133 * addition, user data can be passed to the callback function through
|
|
|
134 * the data member.
|
|
|
135 */
|
|
|
136 void icq_SocketSetHandler(int socket, int type, icq_SocketHandler handler,
|
|
|
137 void *data)
|
|
|
138 {
|
|
|
139 icq_Socket *s = icq_FindSocket(socket);
|
|
|
140 if (s)
|
|
|
141 {
|
|
|
142 s->data[type] = data;
|
|
|
143 s->handlers[type] = handler;
|
|
|
144 if (icq_SocketNotify)
|
|
|
145 (*icq_SocketNotify)(socket, type, handler ? 1 : 0);
|
|
|
146 icq_SocketBuildFdSets();
|
|
|
147 }
|
|
|
148 }
|
|
|
149
|
|
|
150 /**
|
|
|
151 * Handles a socket ready event by calling the installed callback
|
|
|
152 * function, if any.
|
|
|
153 */
|
|
|
154 void icq_SocketReady(icq_Socket *s, int type)
|
|
|
155 {
|
|
|
156 if (s && s->handlers[type])
|
|
|
157 {
|
|
|
158 (*s->handlers[type])(s->data[type]);
|
|
|
159 }
|
|
|
160 }
|
|
|
161
|
|
|
162 void icq_HandleReadySocket(int socket, int type)
|
|
|
163 {
|
|
|
164 icq_SocketReady(icq_FindSocket(socket), type);
|
|
|
165 }
|
|
|
166
|
|
|
167 int _icq_SocketBuildFdSets(void *p, va_list data)
|
|
|
168 {
|
|
|
169 icq_Socket *s = p;
|
|
|
170 int i;
|
|
|
171
|
|
|
172 for (i=0; i<ICQ_SOCKET_MAX; i++)
|
|
|
173 if (s->handlers[i])
|
|
|
174 FD_SET(s->socket, &icq_FdSets[i]);
|
|
|
175
|
|
|
176 if (s->socket > icq_MaxSocket)
|
|
|
177 icq_MaxSocket = s->socket;
|
|
|
178
|
|
|
179 return 0; /* traverse entire list */
|
|
|
180 }
|
|
|
181
|
|
|
182 void icq_SocketBuildFdSets()
|
|
|
183 {
|
|
|
184 int i;
|
|
|
185
|
|
|
186 /* clear fdsets */
|
|
|
187 for (i=0; i<ICQ_SOCKET_MAX; i++)
|
|
|
188 FD_ZERO(&icq_FdSets[i]);
|
|
|
189
|
|
|
190 icq_MaxSocket = 0;
|
|
|
191
|
|
|
192 /* build fd lists for open sockets */
|
|
|
193 (void)list_traverse(icq_SocketList, _icq_SocketBuildFdSets);
|
|
|
194 }
|
|
|
195
|
|
|
196 int _icq_SocketHandleReady(void *p, va_list data)
|
|
|
197 {
|
|
|
198 icq_Socket *s = p;
|
|
|
199 int i;
|
|
|
200
|
|
|
201 for (i=0; i<ICQ_SOCKET_MAX; i++)
|
|
|
202 if (FD_ISSET(s->socket, &icq_FdSets[i]))
|
|
|
203 icq_SocketReady(s, i);
|
|
|
204
|
|
|
205 return 0; /* traverse entire list */
|
|
|
206 }
|
|
|
207
|
|
|
208 void icq_SocketPoll()
|
|
|
209 {
|
|
|
210 struct timeval tv;
|
|
|
211 int max_socket = 0;
|
|
|
212 int i;
|
|
|
213
|
|
|
214 tv.tv_sec = 0; tv.tv_usec = 0;
|
|
|
215
|
|
|
216 /* determine which sockets require maintenance */
|
|
|
217 select(icq_MaxSocket+1, &icq_FdSets[0], &icq_FdSets[1], NULL, &tv);
|
|
|
218
|
|
|
219 /* handle ready sockets */
|
|
|
220 (void)list_traverse(icq_SocketList, _icq_SocketHandleReady);
|
|
|
221 }
|
|
|
222
|
|
|
223 int _icq_FindSocket(void *p, va_list data)
|
|
|
224 {
|
|
|
225 int socket = va_arg(data, int);
|
|
|
226 return (((icq_Socket *)p)->socket == socket);
|
|
|
227 }
|
|
|
228
|
|
|
229 icq_Socket *icq_FindSocket(int socket)
|
|
|
230 {
|
|
|
231 return list_traverse(icq_SocketList, _icq_FindSocket, socket);
|
|
|
232 }
|
|
|
233
|
|
|
234
|