1 /* 2 3 Copyright (c) 2011-2018, 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_PEER_CLASS_HPP_INCLUDED 34 #define TORRENT_PEER_CLASS_HPP_INCLUDED 35 36 #include "libtorrent/config.hpp" 37 #include "libtorrent/assert.hpp" 38 #include "libtorrent/bandwidth_limit.hpp" 39 #include "libtorrent/units.hpp" 40 #include "libtorrent/aux_/deque.hpp" 41 42 #include <vector> 43 #include <string> 44 #include <cstdint> 45 #include <memory> 46 47 namespace libtorrent { 48 49 using peer_class_t = aux::strong_typedef<std::uint32_t, struct peer_class_tag>; 50 51 // holds settings for a peer class. Used in set_peer_class() and 52 // get_peer_class() calls. 53 struct TORRENT_EXPORT peer_class_info 54 { 55 // ``ignore_unchoke_slots`` determines whether peers should always 56 // unchoke a peer, regardless of the choking algorithm, or if it should 57 // honor the unchoke slot limits. It's used for local peers by default. 58 // If *any* of the peer classes a peer belongs to has this set to true, 59 // that peer will be unchoked at all times. 60 bool ignore_unchoke_slots; 61 62 // adjusts the connection limit (global and per torrent) that applies to 63 // this peer class. By default, local peers are allowed to exceed the 64 // normal connection limit for instance. This is specified as a percent 65 // factor. 100 makes the peer class apply normally to the limit. 200 66 // means as long as there are fewer connections than twice the limit, we 67 // accept this peer. This factor applies both to the global connection 68 // limit and the per-torrent limit. Note that if not used carefully one 69 // peer class can potentially completely starve out all other over time. 70 int connection_limit_factor; 71 72 // not used by libtorrent. It's intended as a potentially user-facing 73 // identifier of this peer class. 74 std::string label; 75 76 // transfer rates limits for the whole peer class. They are specified in 77 // bytes per second and apply to the sum of all peers that are members of 78 // this class. 79 int upload_limit; 80 int download_limit; 81 82 // relative priorities used by the bandwidth allocator in the rate 83 // limiter. If no rate limits are in use, the priority is not used 84 // either. Priorities start at 1 (0 is not a valid priority) and may not 85 // exceed 255. 86 int upload_priority; 87 int download_priority; 88 }; 89 90 struct TORRENT_EXTRA_EXPORT peer_class 91 { 92 friend struct peer_class_pool; 93 peer_classlibtorrent::peer_class94 explicit peer_class(std::string l) 95 : ignore_unchoke_slots(false) 96 , connection_limit_factor(100) 97 , label(std::move(l)) 98 , in_use(true) 99 , references(1) 100 { 101 priority[0] = 1; 102 priority[1] = 1; 103 } 104 clearlibtorrent::peer_class105 void clear() 106 { 107 in_use = false; 108 label.clear(); 109 } 110 111 void set_info(peer_class_info const* pci); 112 void get_info(peer_class_info* pci) const; 113 114 void set_upload_limit(int limit); 115 void set_download_limit(int limit); 116 117 // the bandwidth channels, upload and download 118 // keeps track of the current quotas 119 bandwidth_channel channel[2]; 120 121 bool ignore_unchoke_slots; 122 int connection_limit_factor; 123 124 // priority for bandwidth allocation 125 // in rate limiter. One for upload and one 126 // for download 127 int priority[2]; 128 129 // the name of this peer class 130 std::string label; 131 132 private: 133 // this is set to false when this slot is not in use for a peer_class 134 bool in_use; 135 136 int references; 137 }; 138 139 struct TORRENT_EXTRA_EXPORT peer_class_pool 140 { 141 peer_class_t new_peer_class(std::string label); 142 void decref(peer_class_t c); 143 void incref(peer_class_t c); 144 peer_class* at(peer_class_t c); 145 peer_class const* at(peer_class_t c) const; 146 147 private: 148 149 // state for peer classes (a peer can belong to multiple classes) 150 // this can control 151 aux::deque<peer_class, peer_class_t> m_peer_classes; 152 153 // indices in m_peer_classes that are no longer used 154 std::vector<peer_class_t> m_free_list; 155 }; 156 } 157 158 #endif // TORRENT_PEER_CLASS_HPP_INCLUDED 159