1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 01:03:01 +00:00

* some work on lessening the way we hammer the router.

* move strlcpy and strlcat out of their own files and into utils.c
This commit is contained in:
Charles Kerr 2007-11-08 04:11:09 +00:00
parent a7e92e501e
commit bc3eff899f
5 changed files with 106 additions and 125 deletions

View file

@ -26,8 +26,6 @@ libtransmission_a_SOURCES = \
publish.c \
ratecontrol.c \
shared.c \
strlcat.c \
strlcpy.c \
torrent.c \
tracker.c \
transmission.c \

View file

@ -913,6 +913,12 @@ myHandshakeDoneCB( tr_handshake * handshake,
if( !ok || !t || !t->isRunning )
{
if( t ) {
struct peer_atom * atom = getExistingAtom( t, addr );
if( atom )
++atom->numFails;
}
tr_peerIoFree( io );
}
else /* looking good */
@ -1583,12 +1589,18 @@ getPeersToClose( Torrent * t, int * setmeSize )
}
static int
compareAtomByTime( const void * va, const void * vb )
compareCandidates( const void * va, const void * vb )
{
const struct peer_atom * a = * (const struct peer_atom**) va;
const struct peer_atom * b = * (const struct peer_atom**) vb;
if( a->time < b->time ) return -1;
if( a->time > b->time ) return 1;
int i;
if(( i = tr_compareUint16( a->numFails, b->numFails )))
return i;
if( a->time != b->time )
return a->time < b->time ? -1 : 1;
return 0;
}
@ -1632,10 +1644,17 @@ getPeerCandidates( Torrent * t, int * setmeSize )
continue;
}
/* we're wasting our time trying to connect to this bozo. */
if( atom->numFails > 10 ) {
tordbg( t, "RECONNECT peer %d (%s) gives us nothing but failure.",
i, tr_peerIoAddrStr(&atom->addr,atom->port) );
continue;
}
/* if we used this peer recently, give someone else a turn */
minWait = 60; /* one minute */
maxWait = (60 * 10); /* ten minutes */
wait = atom->numFails * 15; /* add 15 secs to the wait interval for each consecutive failure*/
minWait = 10; /* ten seconds */
maxWait = (60 * 20); /* twenty minutes */
wait = atom->numFails * 30; /* add 15 secs to the wait interval for each consecutive failure*/
if( wait < minWait ) wait = minWait;
if( wait > maxWait ) wait = maxWait;
if( ( now - atom->time ) < wait ) {
@ -1647,7 +1666,7 @@ getPeerCandidates( Torrent * t, int * setmeSize )
ret[retCount++] = atom;
}
qsort( ret, retCount, sizeof(struct peer_atom*), compareAtomByTime );
qsort( ret, retCount, sizeof(struct peer_atom*), compareCandidates );
*setmeSize = retCount;
return ret;
}

View file

@ -1,60 +0,0 @@
/* $Id$ */
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
#ifndef HAVE_STRLCAT
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif /* HAVE_STRLCAT */

View file

@ -1,56 +0,0 @@
/* $Id$ */
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
#ifndef HAVE_STRLCPY
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif /* HAVE_STRLCPY */

View file

@ -760,3 +760,83 @@ tr_wait( uint64_t delay_milliseconds )
usleep( 1000 * delay_milliseconds );
#endif
}
/***
****
***/
#ifndef HAVE_STRLCPY
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif /* HAVE_STRLCPY */
#ifndef HAVE_STRLCAT
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif /* HAVE_STRLCAT */