[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / icq / socketmanager.c
blob01970d6de7eee3515121c2445ac8cbc9a66a260d
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /*
4 * $Id: socketmanager.c 2405 2001-09-29 02:08:00Z warmenhoven $
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
7 * Bill Soudan <soudan@kde.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /**
26 * The icqlib socket manager is a simple socket abstraction layer, which
27 * supports opening and closing sockets as well as installing handler
28 * functions for read ready and write ready events. Its purpose is to
29 * both unify socket handling in icqlib and expose icqlib's socket
30 * requirements so the library client can assist with socket housekeeping.
32 * Library clients have two options to support icqlib:
34 * 1. Periodically call icq_Main. This will handle all select logic
35 * internally. Advantage is implementation ease, disadvantage is wasted
36 * CPU cycles because of polling and poor TCP file transfer performance.
38 * 2. Install a icq_SocketNotify callback, perform your own socket
39 * management, and notify icqlib using the icq_SocketReady method when
40 * a socket is ready for reading or writing. Advantage is efficiency,
41 * disadvantage is extra code.
45 #include <stdlib.h>
47 #ifdef _WIN32
48 #include <winsock.h>
49 #endif
51 #include "socketmanager.h"
53 icq_List *icq_SocketList = NULL;
54 fd_set icq_FdSets[ICQ_SOCKET_MAX];
55 int icq_MaxSocket;
57 void (*icq_SocketNotify)(int socket_fd, int type, int status);
59 /**
60 * Creates a new socket using the operating system's socket creation
61 * facility.
63 int icq_SocketNew(int domain, int type, int protocol)
65 int s = socket(domain, type, protocol);
67 icq_SocketAlloc(s);
69 return s;
73 /**
74 * Creates a new socket by accepting a connection from a listening
75 * socket.
77 int icq_SocketAccept(int listens, struct sockaddr *addr, int *addrlen)
79 int s = accept(listens, addr, addrlen);
81 icq_SocketAlloc(s);
83 return s;
86 /**
87 * Creates a new icq_Socket structure, and appends it to the
88 * socketmanager's global socket list.
90 void icq_SocketAlloc(int s)
92 if (s != -1)
94 icq_Socket *psocket = (icq_Socket *)malloc(sizeof(icq_Socket));
95 int i;
96 psocket->socket = s;
98 for (i=0; i<ICQ_SOCKET_MAX; i++)
99 psocket->handlers[i] = NULL;
101 icq_ListEnqueue(icq_SocketList, psocket);
106 * Closes a socket. This function will notify the library client
107 * through the icq_SocketNotify callback if the socket had an installed
108 * read or write handler.
110 int icq_SocketDelete(int socket_fd)
112 #ifdef _WIN32
113 int result = closesocket(socket_fd);
114 #else
115 int result = close(socket_fd);
116 #endif
118 if (result != -1)
120 icq_Socket *s = icq_FindSocket(socket_fd);
121 int i;
123 /* uninstall all handlers - this will take care of notifing library
124 * client */
125 for (i=0; i<ICQ_SOCKET_MAX; i++)
127 if (s->handlers[i])
128 icq_SocketSetHandler(s->socket, i, NULL, NULL);
131 icq_ListRemove(icq_SocketList, s);
132 free(s);
135 return result;
139 * Installs a socket event handler. The handler will be called when
140 * the socket is ready for reading or writing, depending on the type
141 * which should be either ICQ_SOCKET_READ or ICQ_SOCKET_WRITE. In
142 * addition, user data can be passed to the callback function through
143 * the data member.
145 void icq_SocketSetHandler(int socket_fd, int type, icq_SocketHandler handler,
146 void *data)
148 icq_Socket *s = icq_FindSocket(socket_fd);
149 if (s)
151 s->data[type] = data;
152 s->handlers[type] = handler;
153 if (icq_SocketNotify)
154 (*icq_SocketNotify)(socket_fd, type, handler ? 1 : 0);
159 * Handles a socket ready event by calling the installed callback
160 * function, if any.
162 void icq_SocketReady(icq_Socket *s, int type)
164 if (s && s->handlers[type])
166 (*s->handlers[type])(s->data[type]);
170 void icq_HandleReadySocket(int socket_fd, int type)
172 icq_SocketReady(icq_FindSocket(socket_fd), type);
175 int _icq_SocketBuildFdSets(void *p, va_list data)
177 icq_Socket *s = p;
178 int i;
179 (void)data;
181 for (i=0; i<ICQ_SOCKET_MAX; i++)
182 if (s->handlers[i]) {
183 FD_SET(s->socket, &(icq_FdSets[i]));
184 if (s->socket > icq_MaxSocket)
185 icq_MaxSocket = s->socket;
188 return 0; /* traverse entire icq_List */
191 void icq_SocketBuildFdSets()
193 int i;
195 /* clear fdsets */
196 for (i=0; i<ICQ_SOCKET_MAX; i++)
197 FD_ZERO(&(icq_FdSets[i]));
199 icq_MaxSocket = 0;
201 /* build fd lists for open sockets */
202 (void)icq_ListTraverse(icq_SocketList, _icq_SocketBuildFdSets);
205 int _icq_SocketHandleReady(void *p, va_list data)
207 icq_Socket *s = p;
208 int i;
209 (void)data;
211 for (i=0; i<ICQ_SOCKET_MAX; i++)
212 if (FD_ISSET(s->socket, &(icq_FdSets[i]))) {
213 icq_SocketReady(s, i);
216 return 0; /* traverse entire icq_List */
219 void icq_SocketPoll()
221 struct timeval tv;
223 icq_SocketBuildFdSets();
225 tv.tv_sec = 0; tv.tv_usec = 0;
227 /* determine which sockets require maintenance */
228 select(icq_MaxSocket+1, &(icq_FdSets[ICQ_SOCKET_READ]),
229 &(icq_FdSets[ICQ_SOCKET_WRITE]), NULL, &tv);
231 /* handle ready sockets */
232 (void)icq_ListTraverse(icq_SocketList, _icq_SocketHandleReady);
235 int _icq_FindSocket(void *p, va_list data)
237 int socket_fd = va_arg(data, int);
238 return (((icq_Socket *)p)->socket == socket_fd);
241 icq_Socket *icq_FindSocket(int socket_fd)
243 return icq_ListTraverse(icq_SocketList, _icq_FindSocket, socket_fd);