summaryrefslogtreecommitdiff
path: root/src/irc
diff options
context:
space:
mode:
Diffstat (limited to 'src/irc')
-rw-r--r--src/irc/irc-dcc.c47
-rw-r--r--src/irc/irc.h4
2 files changed, 50 insertions, 1 deletions
diff --git a/src/irc/irc-dcc.c b/src/irc/irc-dcc.c
index 3e9ea0209..09515f007 100644
--- a/src/irc/irc-dcc.c
+++ b/src/irc/irc-dcc.c
@@ -64,6 +64,40 @@ dcc_redraw (int highlight)
}
/*
+ * dcc_calculate_speed: calculate DCC speed (for files only)
+ */
+
+void
+dcc_calculate_speed (t_irc_dcc *ptr_dcc, int ended)
+{
+ time_t local_time, elapsed;
+
+ local_time = time (NULL);
+ if (ended || local_time > ptr_dcc->last_check_time)
+ {
+
+ if (ended)
+ {
+ elapsed = local_time - ptr_dcc->start_transfer;
+ if (elapsed == 0)
+ elapsed = 1;
+ ptr_dcc->bytes_per_sec = ptr_dcc->pos / elapsed;
+ }
+ else
+ {
+ elapsed = local_time - ptr_dcc->last_check_time;
+ if (elapsed == 0)
+ elapsed = 1;
+ ptr_dcc->bytes_per_sec = (ptr_dcc->pos - ptr_dcc->last_check_pos) / elapsed;
+ }
+ ptr_dcc->last_check_time = local_time;
+ ptr_dcc->last_check_pos = ptr_dcc->pos;
+ wee_log_printf ("bytes per sec calculé: %lu\n", ptr_dcc->bytes_per_sec);
+ }
+ wee_log_printf ("bytes per sec calculé pas bon !!!\n");
+}
+
+/*
* dcc_connect: connect to another host
*/
@@ -200,6 +234,9 @@ dcc_close (t_irc_dcc *ptr_dcc, int status)
if (DCC_IS_CHAT(ptr_dcc->type))
channel_remove_dcc (ptr_dcc);
+ if (DCC_IS_FILE(ptr_dcc->type))
+ dcc_calculate_speed (ptr_dcc, 1);
+
if (ptr_dcc->sock != -1)
{
close (ptr_dcc->sock);
@@ -327,6 +364,7 @@ dcc_accept (t_irc_dcc *ptr_dcc)
ptr_dcc->file = open (ptr_dcc->local_filename,
O_CREAT | O_TRUNC | O_WRONLY | O_NONBLOCK,
0644);
+ ptr_dcc->start_transfer = time (NULL);
}
else
{
@@ -363,6 +401,7 @@ dcc_add (t_irc_server *server, int type, unsigned long addr, int port, char *nic
new_dcc->type = type;
new_dcc->status = DCC_WAITING;
new_dcc->start_time = time (NULL);
+ new_dcc->start_transfer = time (NULL);
new_dcc->addr = addr;
new_dcc->port = port;
new_dcc->nick = strdup (nick);
@@ -378,6 +417,9 @@ dcc_add (t_irc_server *server, int type, unsigned long addr, int port, char *nic
new_dcc->size = size;
new_dcc->pos = 0;
new_dcc->ack = 0;
+ new_dcc->last_check_time = 0;
+ new_dcc->last_check_pos = 0;
+ new_dcc->bytes_per_sec = 0;
new_dcc->prev_dcc = NULL;
new_dcc->next_dcc = dcc_list;
if (dcc_list)
@@ -786,7 +828,7 @@ dcc_chat_recv (t_irc_dcc *ptr_dcc)
}
/*
- * dcc_handle: receive/send data for each active DCC (files only)
+ * dcc_handle: receive/send data for all active DCC
*/
void
@@ -838,6 +880,7 @@ dcc_handle ()
ptr_dcc->addr = ntohl (addr.sin_addr.s_addr);
ptr_dcc->status = DCC_ACTIVE;
ptr_dcc->file = open (ptr_dcc->local_filename, O_RDONLY | O_NONBLOCK, 0644);
+ ptr_dcc->start_transfer = time (NULL);
dcc_redraw (1);
}
}
@@ -921,6 +964,7 @@ dcc_handle ()
ptr_dcc->pos += (unsigned long) num_read;
pos = htonl (ptr_dcc->pos);
send (ptr_dcc->sock, (char *) &pos, 4, 0);
+ dcc_calculate_speed (ptr_dcc, 0);
if (ptr_dcc->pos >= ptr_dcc->size)
{
dcc_close (ptr_dcc, DCC_DONE);
@@ -987,6 +1031,7 @@ dcc_handle ()
return;
}
ptr_dcc->pos += (unsigned long) num_sent;
+ dcc_calculate_speed (ptr_dcc, 0);
dcc_redraw (0);
}
}
diff --git a/src/irc/irc.h b/src/irc/irc.h
index f2bbb4692..5a16ac9ab 100644
--- a/src/irc/irc.h
+++ b/src/irc/irc.h
@@ -212,6 +212,7 @@ struct t_irc_dcc
int type; /* DCC type (send or receive) */
int status; /* DCC status (waiting, sending, ..) */
time_t start_time; /* the time when DCC started */
+ time_t start_transfer; /* the time when DCC transfer started */
unsigned long addr; /* IP address */
int port; /* port */
char *nick; /* remote nick */
@@ -224,6 +225,9 @@ struct t_irc_dcc
unsigned long size; /* file size */
unsigned long pos; /* number of bytes received/sent */
unsigned long ack; /* number of bytes received OK */
+ time_t last_check_time; /* last time we looked at bytes sent/rcv*/
+ unsigned long last_check_pos; /* bytes sent/recv at last check */
+ unsigned long bytes_per_sec; /* bytes per second */
t_irc_dcc *prev_dcc; /* link to previous dcc file/chat */
t_irc_dcc *next_dcc; /* link to next dcc file/chat */
};