2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
net-disconnect.c :
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
/* when quitting, wait for max. 5 seconds before forcing to close the socket */
|
|
|
|
#define MAX_QUIT_CLOSE_WAIT 5
|
|
|
|
|
|
|
|
/* wait for max. 2 minutes for other side to close the socket */
|
|
|
|
#define MAX_CLOSE_WAIT (60*2)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
time_t created;
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *handle;
|
2000-04-26 04:03:38 -04:00
|
|
|
int tag;
|
|
|
|
} NET_DISCONNECT_REC;
|
|
|
|
|
|
|
|
static GSList *disconnects;
|
|
|
|
|
|
|
|
static int timeout_tag;
|
|
|
|
|
|
|
|
static void net_disconnect_remove(NET_DISCONNECT_REC *rec)
|
|
|
|
{
|
|
|
|
disconnects = g_slist_remove(disconnects, rec);
|
|
|
|
|
|
|
|
g_source_remove(rec->tag);
|
2001-10-14 10:14:54 -04:00
|
|
|
net_disconnect(rec->handle);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_disconnect(NET_DISCONNECT_REC *rec)
|
|
|
|
{
|
2001-02-18 21:12:06 -05:00
|
|
|
char buf[512];
|
|
|
|
int count, ret;
|
|
|
|
|
|
|
|
/* check if there's any data waiting in socket. read max. 5kB so
|
|
|
|
if server just keeps sending us stuff we won't get stuck */
|
|
|
|
count = 0;
|
|
|
|
do {
|
|
|
|
ret = net_receive(rec->handle, buf, sizeof(buf));
|
|
|
|
if (ret == -1) {
|
|
|
|
/* socket was closed */
|
|
|
|
net_disconnect_remove(rec);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
} while (ret == sizeof(buf) && count < 10);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sig_timeout_disconnect(void)
|
|
|
|
{
|
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
GSList *tmp, *next;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
/* check if we've waited enough for sockets to close themselves */
|
|
|
|
now = time(NULL);
|
|
|
|
for (tmp = disconnects; tmp != NULL; tmp = next) {
|
|
|
|
rec = tmp->data;
|
|
|
|
next = tmp->next;
|
|
|
|
|
|
|
|
if (rec->created+MAX_CLOSE_WAIT <= now)
|
2000-07-16 16:18:05 -04:00
|
|
|
net_disconnect_remove(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disconnects == NULL) {
|
|
|
|
/* no more sockets in disconnect queue, stop calling this
|
|
|
|
function */
|
|
|
|
timeout_tag = -1;
|
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
return disconnects != NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to let the other side close the connection, if it still isn't
|
|
|
|
disconnected after certain amount of time, close it ourself */
|
2000-12-04 17:57:18 -05:00
|
|
|
void net_disconnect_later(GIOChannel *handle)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
|
|
|
|
rec = g_new(NET_DISCONNECT_REC, 1);
|
|
|
|
rec->created = time(NULL);
|
|
|
|
rec->handle = handle;
|
2000-07-16 16:18:05 -04:00
|
|
|
rec->tag = g_input_add(handle, G_INPUT_READ,
|
|
|
|
(GInputFunction) sig_disconnect, rec);
|
|
|
|
|
|
|
|
if (timeout_tag == -1) {
|
|
|
|
timeout_tag = g_timeout_add(10000, (GSourceFunc)
|
|
|
|
sig_timeout_disconnect, NULL);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
disconnects = g_slist_append(disconnects, rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void net_disconnect_init(void)
|
|
|
|
{
|
|
|
|
disconnects = NULL;
|
|
|
|
timeout_tag = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_disconnect_deinit(void)
|
|
|
|
{
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
time_t now, max;
|
2000-12-04 17:57:18 -05:00
|
|
|
int first, fd;
|
2000-04-26 04:03:38 -04:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set set;
|
|
|
|
|
|
|
|
/* give the sockets a chance to disconnect themselves.. */
|
|
|
|
max = time(NULL)+MAX_QUIT_CLOSE_WAIT;
|
|
|
|
first = 1;
|
|
|
|
while (disconnects != NULL) {
|
|
|
|
rec = disconnects->data;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
if (rec->created+MAX_QUIT_CLOSE_WAIT <= now || max <= now) {
|
|
|
|
/* this one has waited enough */
|
|
|
|
net_disconnect_remove(rec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
fd = g_io_channel_unix_get_fd(rec->handle);
|
2000-04-26 04:03:38 -04:00
|
|
|
FD_ZERO(&set);
|
2000-12-04 17:57:18 -05:00
|
|
|
FD_SET(fd, &set);
|
2000-04-26 04:03:38 -04:00
|
|
|
tv.tv_sec = first ? 0 : max-now;
|
|
|
|
tv.tv_usec = first ? 100000 : 0;
|
2000-12-04 17:57:18 -05:00
|
|
|
if (select(fd+1, &set, NULL, NULL, &tv) > 0 &&
|
|
|
|
FD_ISSET(fd, &set)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* data coming .. check if we can close the handle */
|
|
|
|
sig_disconnect(rec);
|
|
|
|
} else if (first) {
|
2000-07-16 16:18:05 -04:00
|
|
|
/* Display the text when we have already waited
|
|
|
|
for a while */
|
2001-05-17 16:13:59 -04:00
|
|
|
printf("Please wait, waiting for servers to close "
|
|
|
|
"connections..\n");
|
2000-04-26 04:03:38 -04:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
}
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|