mirror of
https://github.com/weechat/weechat.git
synced 2026-06-29 22:36:38 +02:00
Add speed limit for DCC files sending (task #6178)
This commit is contained in:
@@ -49,6 +49,7 @@ struct t_config_option *xfer_config_network_blocksize;
|
||||
struct t_config_option *xfer_config_network_fast_send;
|
||||
struct t_config_option *xfer_config_network_port_range;
|
||||
struct t_config_option *xfer_config_network_own_ip;
|
||||
struct t_config_option *xfer_config_network_speed_limit;
|
||||
|
||||
/* xfer config, file section */
|
||||
|
||||
@@ -234,6 +235,12 @@ xfer_config_init ()
|
||||
N_("IP or DNS address used for sending files/chats "
|
||||
"(if empty, local interface IP is used)"),
|
||||
NULL, 0, 0, "", NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
xfer_config_network_speed_limit = weechat_config_new_option (
|
||||
xfer_config_file, ptr_section,
|
||||
"speed_limit", "integer",
|
||||
N_("speed limit for sending files, in kilo-bytes by second (0 means "
|
||||
"no limit)"),
|
||||
NULL, 0, INT_MAX, "0", NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
|
||||
ptr_section = weechat_config_new_section (xfer_config_file, "file",
|
||||
0, 0,
|
||||
|
||||
@@ -39,6 +39,7 @@ extern struct t_config_option *xfer_config_network_blocksize;
|
||||
extern struct t_config_option *xfer_config_network_fast_send;
|
||||
extern struct t_config_option *xfer_config_network_port_range;
|
||||
extern struct t_config_option *xfer_config_network_own_ip;
|
||||
extern struct t_config_option *xfer_config_network_speed_limit;
|
||||
|
||||
extern struct t_config_option *xfer_config_file_download_path;
|
||||
extern struct t_config_option *xfer_config_file_upload_path;
|
||||
|
||||
+53
-25
@@ -32,6 +32,8 @@
|
||||
|
||||
#include "../weechat-plugin.h"
|
||||
#include "xfer.h"
|
||||
#include "xfer-config.h"
|
||||
#include "xfer-file.h"
|
||||
#include "xfer-network.h"
|
||||
|
||||
|
||||
@@ -42,12 +44,22 @@
|
||||
void
|
||||
xfer_dcc_send_file_child (struct t_xfer *xfer)
|
||||
{
|
||||
int num_read, num_sent;
|
||||
int num_read, num_sent, blocksize;
|
||||
static char buffer[XFER_BLOCKSIZE_MAX];
|
||||
uint32_t ack;
|
||||
time_t last_sent, new_time;
|
||||
time_t last_sent, new_time, last_second;
|
||||
long sent_last_second;
|
||||
|
||||
blocksize = xfer->blocksize;
|
||||
if (weechat_config_integer (xfer_config_network_speed_limit) > 0)
|
||||
{
|
||||
if (blocksize > weechat_config_integer (xfer_config_network_speed_limit) * 1024)
|
||||
blocksize = weechat_config_integer (xfer_config_network_speed_limit) * 1024;
|
||||
}
|
||||
|
||||
last_sent = time (NULL);
|
||||
last_second = time (NULL);
|
||||
sent_last_second = 0;
|
||||
while (1)
|
||||
{
|
||||
/* read DCC ACK (sent by receiver) */
|
||||
@@ -87,42 +99,58 @@ xfer_dcc_send_file_child (struct t_xfer *xfer)
|
||||
if ((xfer->pos < xfer->size) &&
|
||||
(xfer->fast_send || (xfer->pos <= xfer->ack)))
|
||||
{
|
||||
lseek (xfer->file, xfer->pos, SEEK_SET);
|
||||
num_read = read (xfer->file, buffer, xfer->blocksize);
|
||||
if (num_read < 1)
|
||||
if ((weechat_config_integer (xfer_config_network_speed_limit) > 0)
|
||||
&& (sent_last_second >= weechat_config_integer (xfer_config_network_speed_limit) * 1024))
|
||||
{
|
||||
xfer_network_write_pipe (xfer, XFER_STATUS_FAILED,
|
||||
XFER_ERROR_READ_LOCAL);
|
||||
return;
|
||||
/* we're sending too fast (according to speed limit set by user) */
|
||||
usleep (100);
|
||||
}
|
||||
num_sent = send (xfer->sock, buffer, num_read, 0);
|
||||
if (num_sent < 0)
|
||||
else
|
||||
{
|
||||
/* socket is temporarily not available (receiver can't receive
|
||||
amount of data we sent ?!) */
|
||||
if (errno == EAGAIN)
|
||||
usleep (1000);
|
||||
else
|
||||
lseek (xfer->file, xfer->pos, SEEK_SET);
|
||||
num_read = read (xfer->file, buffer, blocksize);
|
||||
if (num_read < 1)
|
||||
{
|
||||
xfer_network_write_pipe (xfer, XFER_STATUS_FAILED,
|
||||
XFER_ERROR_SEND_BLOCK);
|
||||
XFER_ERROR_READ_LOCAL);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (num_sent > 0)
|
||||
{
|
||||
xfer->pos += (unsigned long) num_sent;
|
||||
new_time = time (NULL);
|
||||
if (last_sent != new_time)
|
||||
num_sent = send (xfer->sock, buffer, num_read, 0);
|
||||
if (num_sent < 0)
|
||||
{
|
||||
last_sent = new_time;
|
||||
xfer_network_write_pipe (xfer, XFER_STATUS_ACTIVE,
|
||||
XFER_NO_ERROR);
|
||||
/* socket is temporarily not available (receiver can't receive
|
||||
amount of data we sent ?!) */
|
||||
if (errno == EAGAIN)
|
||||
usleep (1000);
|
||||
else
|
||||
{
|
||||
xfer_network_write_pipe (xfer, XFER_STATUS_FAILED,
|
||||
XFER_ERROR_SEND_BLOCK);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (num_sent > 0)
|
||||
{
|
||||
xfer->pos += (unsigned long) num_sent;
|
||||
sent_last_second += (unsigned long) num_sent;
|
||||
new_time = time (NULL);
|
||||
if (last_sent != new_time)
|
||||
{
|
||||
last_sent = new_time;
|
||||
xfer_network_write_pipe (xfer, XFER_STATUS_ACTIVE,
|
||||
XFER_NO_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
usleep (1000);
|
||||
|
||||
if (time (NULL) > last_second)
|
||||
{
|
||||
last_second = time (NULL);
|
||||
sent_last_second = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -473,9 +473,9 @@ xfer_network_connect (struct t_xfer *xfer)
|
||||
return 0;
|
||||
|
||||
xfer->hook_fd = weechat_hook_fd (xfer->sock,
|
||||
1, 0, 0,
|
||||
&xfer_network_fd_cb,
|
||||
xfer);
|
||||
1, 0, 0,
|
||||
&xfer_network_fd_cb,
|
||||
xfer);
|
||||
|
||||
/* add timeout */
|
||||
if (weechat_config_integer (xfer_config_network_timeout) > 0)
|
||||
|
||||
Reference in New Issue
Block a user