📄 dhcp-rtt.c
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-rtt.c,v 1.3 2002/11/16 00:23:44 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */#define MODULE_NAME "dhcp-rtt"#include "dhcp-local.h"#include "dhcp-libutil.h"static struct timeval make_timeval(time_t milliseconds){ struct timeval tm; tm.tv_sec = MSECS_TO_SECS(milliseconds); tm.tv_usec = MSECS_TO_SECS_REM(milliseconds); return tm;}static struct timeval init_timeout(void){ struct timeval timeout; timeout = make_timeval(MILLISECOND_TIMEOUT + rand() % MILLISECOND_RAND_TIMEOUT); return timeout;}rtt_t *rtt_create(time_t max_timeout){ rtt_t *rtt; rtt = xmalloc(sizeof(rtt_t)); rtt->timeout = init_timeout(); rtt->max_timeout = max_timeout; rtt->start_time = time(NULL); rtt->elapsed_time = 0; /* we hope :-) */ return rtt;}struct timeval rtt_get_timeout(rtt_t *rtt){ return rtt->timeout;}void rtt_timeout(rtt_t *rtt){ time_t milliseconds; int rand_value; milliseconds = SECS_TO_MSECS(rtt->timeout.tv_sec); milliseconds += rtt->timeout.tv_usec; milliseconds *= 2; rand_value = rand() % MILLISECOND_RAND_TIMEOUT; /* Add or subtract random small value. */ if(rand() % 10 > 5) /* we're safe adding. */ milliseconds += rand() % MILLISECOND_RAND_TIMEOUT; else { /* we're not safe subtracting. don't go below 0. */ if(rand_value <= milliseconds) milliseconds -= rand_value; else milliseconds += rand_value; } rtt->timeout = make_timeval(milliseconds); return;}int rtt_can_retry(rtt_t *rtt){ time_t milliseconds; rtt->elapsed_time = time(NULL) - rtt->start_time; if(rtt->elapsed_time >= rtt->max_timeout) { return 0; } milliseconds = SECS_TO_MSECS(rtt->timeout.tv_sec); milliseconds += rtt->timeout.tv_usec; if((milliseconds >= (SECS_TO_MSECS(RECOMMENDED_MAX_SECS_WAIT))) || (milliseconds >= (rtt->max_timeout - rtt->elapsed_time))) { /* It's perfectly possible for this to put us over the top * of our max_timeout but it's no big deal since our initial * timeout is pretty small anyway. */ rtt->timeout = init_timeout(); } else { rtt->timeout = make_timeval(milliseconds); } return 1;}void rtt_destroy(rtt_t *rtt){ xfree(rtt); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -