lib/eggdrop/throttle.c File Reference

#include <eggdrop/eggdrop.h>
#include <unistd.h>

Go to the source code of this file.

Data Structures

struct  throttle

Defines

#define THROTTLE_LEVEL   SOCKBUF_LEVEL_THROTTLE

Typedefs

typedef struct throttle throttle_t

Functions

static void throttle_remove (throttle_t *t)
static int throttle_on_read (void *client_data, int idx, char *data, int len)
static int throttle_on_write (void *client_data, int idx, const char *data, int len)
static int throttle_on_written (void *client_data, int idx, int len, int remaining)
static int throttle_on_delete (void *client_data, int idx)
static throttle_tthrottle_lookup (int idx)
static void throttle_add (throttle_t *t)
static int throttle_secondly (void *ignore)
int throttle_on (int idx)
int throttle_off (int idx)
int throttle_set (int idx, int dir, int speed)

Variables

static const char rcsid [] = "$Id: throttle.c,v 1.5 2004-10-17 05:14:06 stdarg Exp $"
static throttle_tthrottle_head = NULL
static throttle_tthrottle_next = NULL
static int throttle_timer = -1
static sockbuf_filter_t throttle_filter


Define Documentation

#define THROTTLE_LEVEL   SOCKBUF_LEVEL_THROTTLE


Typedef Documentation

typedef struct throttle throttle_t


Function Documentation

static void throttle_add ( throttle_t t  )  [static]

Definition at line 99 of file throttle.c.

References throttle::next, NULL, and throttle::prev.

Referenced by throttle_on().

00100 {
00101   t->next = throttle_head;
00102   t->prev = NULL;
00103   if (throttle_head) throttle_head->prev = t;
00104   throttle_head = t;
00105 }

static throttle_t* throttle_lookup ( int  idx  )  [static]

Definition at line 89 of file throttle.c.

References throttle::idx, and throttle::next.

Referenced by throttle_off(), and throttle_set().

00090 {
00091   throttle_t *t;
00092 
00093   for (t = throttle_head; t; t = t->next) {
00094     if (t->idx == idx) break;
00095   }
00096   return(t);
00097 }

int throttle_off ( int  idx  ) 

Definition at line 171 of file throttle.c.

References throttle::buf, throttle::len, NULL, sockbuf_detach_filter(), sockbuf_isvalid(), sockbuf_on_write(), THROTTLE_LEVEL, throttle_lookup(), throttle_remove(), throttle_timer, and timer_destroy().

Referenced by throttle_on_delete().

00172 {
00173   throttle_t *t;
00174 
00175   t = throttle_lookup(idx);
00176   if (!t) return(-1);
00177 
00178   throttle_remove(t);
00179   if (sockbuf_isvalid(idx)) {
00180     sockbuf_detach_filter(idx, &throttle_filter, NULL);
00181     if (t->len) sockbuf_on_write(idx, THROTTLE_LEVEL, t->buf, t->len);
00182   }
00183 
00184   if (t->buf) free(t->buf);
00185   free(t);
00186 
00187   if (!throttle_head) {
00188     timer_destroy(throttle_timer);
00189     throttle_timer = -1;
00190   }
00191   return(0);
00192 }

int throttle_on ( int  idx  ) 

Definition at line 153 of file throttle.c.

References throttle::idx, egg_timeval::sec, sockbuf_attach_filter(), throttle_add(), throttle_secondly(), throttle_timer, timer_create_repeater, and egg_timeval::usec.

Referenced by script_net_throttle().

00154 {
00155   throttle_t *t;
00156   egg_timeval_t howlong;
00157 
00158   t = calloc(1, sizeof(*t));
00159   t->idx = idx;
00160   throttle_add(t);
00161   sockbuf_attach_filter(idx, &throttle_filter, t);
00162 
00163   if (throttle_timer < 0) {
00164     howlong.sec = 1;
00165     howlong.usec = 0;
00166     throttle_timer = timer_create_repeater(&howlong, "bandwidth throttler", throttle_secondly);
00167   }
00168   return(0);
00169 }

static int throttle_on_delete ( void *  client_data,
int  idx 
) [static]

Definition at line 75 of file throttle.c.

References throttle_off().

00076 {
00077   throttle_off(idx);
00078   return(0);
00079 }

static int throttle_on_read ( void *  client_data,
int  idx,
char *  data,
int  len 
) [static]

Definition at line 44 of file throttle.c.

References throttle::bytes_in, sockbuf_noread(), sockbuf_on_read(), throttle::speed_in, and THROTTLE_LEVEL.

00045 {
00046   throttle_t *t = client_data;
00047 
00048   t->bytes_in += len;
00049   if (t->speed_in > 0 && t->bytes_in > t->speed_in) {
00050     sockbuf_noread(idx);
00051   }
00052   return sockbuf_on_read(idx, THROTTLE_LEVEL, data, len);
00053 }

static int throttle_on_write ( void *  client_data,
int  idx,
const char *  data,
int  len 
) [static]

Definition at line 55 of file throttle.c.

References throttle::buf, throttle::len, sockbuf_on_write(), throttle::speed_out, and THROTTLE_LEVEL.

00056 {
00057   throttle_t *t = client_data;
00058 
00059   if (t->speed_out <= 0) return sockbuf_on_write(idx, THROTTLE_LEVEL, data, len);
00060 
00061   t->buf = realloc(t->buf, t->len + len);
00062   memcpy(t->buf + t->len, data, len);
00063   t->len += len;
00064 
00065   return(0);
00066 }

static int throttle_on_written ( void *  client_data,
int  idx,
int  len,
int  remaining 
) [static]

Definition at line 68 of file throttle.c.

References throttle::len, sockbuf_on_written(), and THROTTLE_LEVEL.

00069 {
00070   throttle_t *t = client_data;
00071 
00072   return sockbuf_on_written(idx, THROTTLE_LEVEL, len, remaining + t->len);
00073 }

static void throttle_remove ( throttle_t t  )  [static]

Definition at line 107 of file throttle.c.

References throttle::next, and throttle::prev.

Referenced by throttle_off().

00108 {
00109   /* If we get removed during throttle_secondly() we have to update
00110    * the next pointer to a valid one. */
00111   if (throttle_next == t) {
00112     throttle_next = t->next;
00113   }
00114 
00115   if (t->prev) t->prev->next = t->next;
00116   else throttle_head = t->next;
00117 
00118   if (t->next) t->next->prev = t->prev;
00119 }

static int throttle_secondly ( void *  ignore  )  [static]

Definition at line 122 of file throttle.c.

References throttle::buf, throttle::bytes_in, throttle::idx, throttle::len, throttle::next, sockbuf_on_write(), sockbuf_on_written(), sockbuf_read(), throttle::speed_in, throttle::speed_out, and THROTTLE_LEVEL.

Referenced by throttle_on().

00123 {
00124   throttle_t *t;
00125   int avail, r, out;
00126 
00127   for (t = throttle_head; t; t = throttle_next) {
00128     throttle_next = t->next;
00129     t->bytes_in -= t->speed_in;
00130     if (t->bytes_in < 0) t->bytes_in = 0;
00131     else if (t->bytes_in < t->speed_in) sockbuf_read(t->idx);
00132 
00133     out = 0;
00134     while (t->len && out < t->speed_out) {
00135       /* How many bytes can we send? */
00136       avail = t->speed_out - out;
00137       if (avail > t->len) avail = t->len;
00138 
00139       r = sockbuf_on_write(t->idx, THROTTLE_LEVEL, t->buf, avail);
00140       if (r < 0) break;
00141 
00142       memmove(t->buf, t->buf+avail, t->len-avail);
00143       t->len -= avail;
00144       out += avail;
00145 
00146       if (r > 0) sockbuf_on_written(t->idx, THROTTLE_LEVEL, r, t->len + avail - r);
00147 
00148     }
00149   }
00150   return(0);
00151 }

int throttle_set ( int  idx,
int  dir,
int  speed 
)

Definition at line 194 of file throttle.c.

References throttle::buf, throttle::len, sockbuf_on_write(), sockbuf_on_written(), throttle::speed_in, throttle::speed_out, THROTTLE_LEVEL, and throttle_lookup().

Referenced by script_net_throttle(), and script_net_throttle_set().

00195 {
00196   throttle_t *t;
00197   int r;
00198 
00199   t = throttle_lookup(idx);
00200   if (!t) return(-1);
00201   if (dir) {
00202     t->speed_out = speed;
00203     if (speed <= 0 && t->len) {
00204       r = sockbuf_on_write(idx, THROTTLE_LEVEL, t->buf, t->len);
00205       if (r > 0) sockbuf_on_written(idx, THROTTLE_LEVEL, r, t->len - r);
00206       t->len = 0;
00207     }
00208   }
00209   else t->speed_in = speed;
00210   return(0);
00211 }


Variable Documentation

const char rcsid[] = "$Id: throttle.c,v 1.5 2004-10-17 05:14:06 stdarg Exp $" [static]

Definition at line 21 of file throttle.c.

Initial value:

Definition at line 81 of file throttle.c.

throttle_t* throttle_head = NULL [static]

Definition at line 38 of file throttle.c.

throttle_t* throttle_next = NULL [static]

Definition at line 39 of file throttle.c.

int throttle_timer = -1 [static]

Definition at line 40 of file throttle.c.

Referenced by throttle_off(), and throttle_on().


Generated on Sun Nov 30 18:43:35 2008 for eggdrop1.9 by  doxygen 1.5.6