00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "RateLimitedSocket.h"
00019
00020 namespace oasys {
00021
00022
00023 RateLimitedSocket::RateLimitedSocket(const char* logpath,
00024 u_int32_t rate,
00025 IPSocket* socket)
00026 : Logger("RateLimitedSocket", logpath),
00027 bucket_(logpath, rate, 65535 * 8 ),
00028 socket_(socket)
00029 {
00030 }
00031
00032
00033 int
00034 RateLimitedSocket::send(const char* bp, size_t len, int flags)
00035 {
00036 ASSERT(socket_ != NULL);
00037
00038 if (bucket_.rate() != 0) {
00039 bool can_send = bucket_.drain(len * 8);
00040 if (!can_send) {
00041 log_debug("can't send %zu byte packet since only %u tokens in bucket",
00042 len, bucket_.tokens());
00043 return IORATELIMIT;
00044 }
00045
00046 log_debug("%u tokens sufficient for %zu byte packet",
00047 bucket_.tokens(), len);
00048 }
00049
00050 return socket_->send(bp, len, flags);
00051 }
00052
00053
00054 int
00055 RateLimitedSocket::sendto(char* bp, size_t len, int flags,
00056 in_addr_t addr, u_int16_t port)
00057 {
00058 ASSERT(socket_ != NULL);
00059
00060 if (bucket_.rate() != 0) {
00061 bool can_send = bucket_.drain(len * 8);
00062 if (!can_send) {
00063 log_debug("can't send %zu byte packet since only %u tokens in bucket",
00064 len, bucket_.tokens());
00065 return IORATELIMIT;
00066 }
00067
00068 log_debug("%u tokens sufficient for %zu byte packet",
00069 bucket_.tokens(), len);
00070 }
00071
00072 return socket_->sendto(bp, len, flags, addr, port);
00073 }
00074
00075 }