~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/bandwidth_limit.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2007, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
10
    * Redistributions of source code must retain the above copyright
 
11
      notice, this list of conditions and the following disclaimer.
 
12
    * Redistributions in binary form must reproduce the above copyright
 
13
      notice, this list of conditions and the following disclaimer in
 
14
      the documentation and/or other materials provided with the distribution.
 
15
    * Neither the name of the author nor the names of its
 
16
      contributors may be used to endorse or promote products derived
 
17
      from this software without specific prior written permission.
 
18
 
 
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
29
POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
*/
 
32
 
 
33
#ifndef TORRENT_BANDWIDTH_LIMIT_HPP_INCLUDED
 
34
#define TORRENT_BANDWIDTH_LIMIT_HPP_INCLUDED
 
35
 
 
36
#include <boost/integer_traits.hpp>
 
37
 
 
38
#include "libtorrent/assert.hpp"
 
39
 
 
40
namespace libtorrent {
 
41
 
 
42
// member of peer_connection
 
43
struct bandwidth_limit
 
44
{
 
45
        static const int inf = boost::integer_traits<int>::const_max;
 
46
 
 
47
        bandwidth_limit()
 
48
                : m_quota_left(0)
 
49
                , m_local_limit(inf)
 
50
                , m_current_rate(0)
 
51
        {}
 
52
 
 
53
        void throttle(int limit)
 
54
        {
 
55
                TORRENT_ASSERT(limit > 0);
 
56
                m_local_limit = limit;
 
57
        }
 
58
        
 
59
        int throttle() const
 
60
        {
 
61
                return m_local_limit;
 
62
        }
 
63
 
 
64
        void assign(int amount)
 
65
        {
 
66
                TORRENT_ASSERT(amount >= 0);
 
67
                m_current_rate += amount;
 
68
                m_quota_left += amount;
 
69
        }
 
70
 
 
71
        void use_quota(int amount)
 
72
        {
 
73
                TORRENT_ASSERT(amount <= m_quota_left);
 
74
                m_quota_left -= amount;
 
75
        }
 
76
 
 
77
        int quota_left() const
 
78
        {
 
79
                return (std::max)(m_quota_left, 0);
 
80
        }
 
81
 
 
82
        void expire(int amount)
 
83
        {
 
84
                TORRENT_ASSERT(amount >= 0);
 
85
                m_current_rate -= amount;
 
86
        }
 
87
 
 
88
        int max_assignable() const
 
89
        {
 
90
                if (m_local_limit == inf) return inf;
 
91
                if (m_local_limit <= m_current_rate) return 0;
 
92
                return m_local_limit - m_current_rate;
 
93
        }
 
94
 
 
95
private:
 
96
 
 
97
        // this is the amount of bandwidth we have
 
98
        // been assigned without using yet. i.e.
 
99
        // the bandwidth that we use up every time
 
100
        // we receive or send a message. Once this
 
101
        // hits zero, we need to request more
 
102
        // bandwidth from the torrent which
 
103
        // in turn will request bandwidth from
 
104
        // the bandwidth manager
 
105
        int m_quota_left;
 
106
 
 
107
        // the local limit is the number of bytes
 
108
        // per window size we are allowed to use.
 
109
        int m_local_limit;
 
110
 
 
111
        // the current rate is the number of
 
112
        // bytes we have been assigned within
 
113
        // the window size.
 
114
        int m_current_rate;
 
115
};
 
116
 
 
117
}
 
118
 
 
119
#endif
 
120