4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* when quitting, wait for max. 5 seconds before forcing to close the socket */
25 #define MAX_QUIT_CLOSE_WAIT 5
27 /* wait for max. 2 minutes for other side to close the socket */
28 #define MAX_CLOSE_WAIT (60*2)
36 static GSList *disconnects;
38 static int timeout_tag;
40 static void net_disconnect_remove(NET_DISCONNECT_REC *rec)
42 disconnects = g_slist_remove(disconnects, rec);
44 g_source_remove(rec->tag);
45 net_disconnect(rec->handle);
49 static void sig_disconnect(NET_DISCONNECT_REC *rec)
54 /* check if there's any data waiting in socket. read max. 5kB so
55 if server just keeps sending us stuff we won't get stuck */
58 ret = net_receive(rec->handle, buf, sizeof(buf));
60 /* socket was closed */
61 net_disconnect_remove(rec);
64 } while (ret == sizeof(buf) && count < 10);
67 static int sig_timeout_disconnect(void)
69 NET_DISCONNECT_REC *rec;
73 /* check if we've waited enough for sockets to close themselves */
75 for (tmp = disconnects; tmp != NULL; tmp = next) {
79 if (rec->created+MAX_CLOSE_WAIT <= now)
80 net_disconnect_remove(rec);
83 if (disconnects == NULL) {
84 /* no more sockets in disconnect queue, stop calling this
88 return disconnects != NULL;
91 /* Try to let the other side close the connection, if it still isn't
92 disconnected after certain amount of time, close it ourself */
93 void net_disconnect_later(GIOChannel *handle)
95 NET_DISCONNECT_REC *rec;
97 rec = g_new(NET_DISCONNECT_REC, 1);
98 rec->created = time(NULL);
100 rec->tag = g_input_add(handle, G_INPUT_READ,
101 (GInputFunction) sig_disconnect, rec);
103 if (timeout_tag == -1) {
104 timeout_tag = g_timeout_add(10000, (GSourceFunc)
105 sig_timeout_disconnect, NULL);
108 disconnects = g_slist_append(disconnects, rec);
111 void net_disconnect_init(void)
117 void net_disconnect_deinit(void)
120 NET_DISCONNECT_REC *rec;
126 /* give the sockets a chance to disconnect themselves.. */
127 max = time(NULL)+MAX_QUIT_CLOSE_WAIT;
129 while (disconnects != NULL) {
130 rec = disconnects->data;
133 if (rec->created+MAX_QUIT_CLOSE_WAIT <= now || max <= now) {
134 /* this one has waited enough */
135 net_disconnect_remove(rec);
139 fd = g_io_channel_unix_get_fd(rec->handle);
142 tv.tv_sec = first ? 0 : max-now;
143 tv.tv_usec = first ? 100000 : 0;
144 if (select(fd+1, &set, NULL, NULL, &tv) > 0 &&
145 FD_ISSET(fd, &set)) {
146 /* data coming .. check if we can close the handle */
149 /* Display the text when we have already waited
151 printf("Please wait, waiting for servers to close "