Sat, 21 Apr 2001 23:53:06 +0000
[gaim-migrate @ 1751]
thanks decklin :)
committer: Eric Warmenhoven <warmenhoven@yahoo.com>
| 1432 | 1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | ||
| 3 | /* | |
|
1529
7d6a1c81af16
[gaim-migrate @ 1539]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1498
diff
changeset
|
4 | $Id: socketmanager.c 1539 2001-03-03 12:56:25Z warmenhoven $ |
| 1432 | 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 | ||
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
50 | void (*icq_SocketNotify)(int socket, int type, int status); |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
51 | |
| 1432 | 52 | /** |
| 53 | * Creates a new socket using the operating system's socket creation | |
| 54 | * facitily. | |
| 55 | */ | |
| 56 | int icq_SocketNew(int domain, int type, int protocol) | |
| 57 | { | |
| 58 | int s = socket(domain, type, protocol); | |
| 59 | ||
| 60 | icq_SocketAlloc(s); | |
| 61 | ||
| 62 | return s; | |
| 63 | } | |
| 64 | ||
| 65 | ||
| 66 | /** | |
| 67 | * Creates a new socket by accepting a connection from a listening | |
| 68 | * socket. | |
| 69 | */ | |
|
1529
7d6a1c81af16
[gaim-migrate @ 1539]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1498
diff
changeset
|
70 | int icq_SocketAccept(int listens, struct sockaddr *addr, int *addrlen) |
| 1432 | 71 | { |
| 72 | int s = accept(listens, addr, addrlen); | |
| 73 | ||
| 74 | icq_SocketAlloc(s); | |
| 75 | ||
| 76 | return s; | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Creates a new icq_Socket structure, and appends it to the | |
| 81 | * socketmanager's global socket list. | |
| 82 | */ | |
| 83 | void icq_SocketAlloc(int s) | |
| 84 | { | |
| 85 | if (s != -1) | |
| 86 | { | |
| 87 | icq_Socket *psocket = (icq_Socket *)malloc(sizeof(icq_Socket)); | |
| 88 | int i; | |
| 89 | psocket->socket = s; | |
| 90 | ||
| 91 | for (i=0; i<ICQ_SOCKET_MAX; i++) | |
| 92 | psocket->handlers[i] = NULL; | |
| 93 | ||
| 94 | list_enqueue(icq_SocketList, psocket); | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Closes a socket. This function will notify the library client | |
| 100 | * through the icq_SocketNotify callback if the socket had an installed | |
| 101 | * read or write handler. | |
| 102 | */ | |
| 103 | int icq_SocketDelete(int socket) | |
| 104 | { | |
| 105 | #ifdef _WIN32 | |
| 106 | int result = closesocket(socket); | |
| 107 | #else | |
| 108 | int result = close(socket); | |
| 109 | #endif | |
| 110 | ||
| 111 | if (result != -1) | |
| 112 | { | |
| 113 | icq_Socket *s = icq_FindSocket(socket); | |
| 114 | int i; | |
| 115 | ||
| 116 | /* uninstall all handlers - this will take care of notifing library | |
| 117 | * client */ | |
| 118 | for (i=0; i<ICQ_SOCKET_MAX; i++) | |
| 119 | { | |
| 120 | if (s->handlers[i]) | |
| 121 | icq_SocketSetHandler(s->socket, i, NULL, NULL); | |
| 122 | } | |
| 123 | ||
| 124 | list_remove(icq_SocketList, s); | |
| 125 | free(s); | |
| 126 | } | |
| 127 | ||
| 128 | return result; | |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Installs a socket event handler. The handler will be called when | |
| 133 | * the socket is ready for reading or writing, depending on the type | |
| 134 | * which should be either ICQ_SOCKET_READ or ICQ_SOCKET_WRITE. In | |
| 135 | * addition, user data can be passed to the callback function through | |
| 136 | * the data member. | |
| 137 | */ | |
| 138 | void icq_SocketSetHandler(int socket, int type, icq_SocketHandler handler, | |
| 139 | void *data) | |
| 140 | { | |
| 141 | icq_Socket *s = icq_FindSocket(socket); | |
| 142 | if (s) | |
| 143 | { | |
| 144 | s->data[type] = data; | |
| 145 | s->handlers[type] = handler; | |
| 146 | if (icq_SocketNotify) | |
| 147 | (*icq_SocketNotify)(socket, type, handler ? 1 : 0); | |
| 148 | } | |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * Handles a socket ready event by calling the installed callback | |
| 153 | * function, if any. | |
| 154 | */ | |
| 155 | void icq_SocketReady(icq_Socket *s, int type) | |
| 156 | { | |
| 157 | if (s && s->handlers[type]) | |
| 158 | { | |
| 159 | (*s->handlers[type])(s->data[type]); | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | void icq_HandleReadySocket(int socket, int type) | |
| 164 | { | |
| 165 | icq_SocketReady(icq_FindSocket(socket), type); | |
| 166 | } | |
| 167 | ||
| 168 | int _icq_SocketBuildFdSets(void *p, va_list data) | |
| 169 | { | |
| 170 | icq_Socket *s = p; | |
| 171 | int i; | |
| 172 | ||
| 173 | for (i=0; i<ICQ_SOCKET_MAX; i++) | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
174 | if (s->handlers[i]) { |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
175 | FD_SET(s->socket, &(icq_FdSets[i])); |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
176 | if (s->socket > icq_MaxSocket) |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
177 | icq_MaxSocket = s->socket; |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
178 | } |
| 1432 | 179 | |
| 180 | return 0; /* traverse entire list */ | |
| 181 | } | |
| 182 | ||
| 183 | void icq_SocketBuildFdSets() | |
| 184 | { | |
| 185 | int i; | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
186 | |
| 1432 | 187 | /* clear fdsets */ |
| 188 | for (i=0; i<ICQ_SOCKET_MAX; i++) | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
189 | FD_ZERO(&(icq_FdSets[i])); |
| 1432 | 190 | |
| 191 | icq_MaxSocket = 0; | |
| 192 | ||
| 193 | /* build fd lists for open sockets */ | |
| 194 | (void)list_traverse(icq_SocketList, _icq_SocketBuildFdSets); | |
| 195 | } | |
| 196 | ||
| 197 | int _icq_SocketHandleReady(void *p, va_list data) | |
| 198 | { | |
| 199 | icq_Socket *s = p; | |
| 200 | int i; | |
| 201 | ||
| 202 | for (i=0; i<ICQ_SOCKET_MAX; i++) | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
203 | if (FD_ISSET(s->socket, &(icq_FdSets[i]))) { |
| 1432 | 204 | icq_SocketReady(s, i); |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
205 | } |
| 1432 | 206 | |
| 207 | return 0; /* traverse entire list */ | |
| 208 | } | |
| 209 | ||
| 210 | void icq_SocketPoll() | |
| 211 | { | |
| 212 | struct timeval tv; | |
| 213 | int max_socket = 0; | |
| 214 | int i; | |
| 215 | ||
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
216 | icq_SocketBuildFdSets(); |
| 1432 | 217 | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
218 | tv.tv_sec = 0; tv.tv_usec = 0; |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
219 | |
| 1432 | 220 | /* determine which sockets require maintenance */ |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
221 | select(icq_MaxSocket+1, &(icq_FdSets[ICQ_SOCKET_READ]), |
|
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
222 | &(icq_FdSets[ICQ_SOCKET_WRITE]), NULL, &tv); |
| 1432 | 223 | |
| 224 | /* handle ready sockets */ | |
| 225 | (void)list_traverse(icq_SocketList, _icq_SocketHandleReady); | |
| 226 | } | |
| 227 | ||
| 228 | int _icq_FindSocket(void *p, va_list data) | |
| 229 | { | |
| 230 | int socket = va_arg(data, int); | |
| 231 | return (((icq_Socket *)p)->socket == socket); | |
| 232 | } | |
| 233 | ||
| 234 | icq_Socket *icq_FindSocket(int socket) | |
| 235 | { | |
| 236 | return list_traverse(icq_SocketList, _icq_FindSocket, socket); | |
| 237 | } | |
| 238 | ||
| 239 |