3 Copyright (c) 2006, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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.
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.
33 #ifndef TORRENT_EXTENSIONS_HPP_INCLUDED
34 #define TORRENT_EXTENSIONS_HPP_INCLUDED
36 #ifndef TORRENT_DISABLE_EXTENSIONS
39 #pragma warning(push, 1)
42 #include <boost/shared_ptr.hpp>
49 #include "libtorrent/config.hpp"
50 #include "libtorrent/buffer.hpp"
55 class bt_peer_connection
;
57 class peer_connection
;
60 struct disk_buffer_holder
;
63 struct TORRENT_EXPORT torrent_plugin
65 virtual ~torrent_plugin() {}
66 // throwing an exception closes the connection
67 // returning a 0 pointer is valid and will not add
68 // the peer_plugin to the peer_connection
69 virtual boost::shared_ptr
<peer_plugin
> new_connection(peer_connection
*)
70 { return boost::shared_ptr
<peer_plugin
>(); }
72 virtual void on_piece_pass(int index
) {}
73 virtual void on_piece_failed(int index
) {}
75 // called aproximately once every second
76 virtual void tick() {}
78 // if true is returned, it means the handler handled the event,
79 // and no other plugins will have their handlers called, and the
80 // default behavior will be skipped
81 virtual bool on_pause() { return false; }
82 virtual bool on_resume() { return false;}
84 // this is called when the initial checking of
85 // files is completed.
86 virtual void on_files_checked() {}
89 struct TORRENT_EXPORT peer_plugin
91 virtual ~peer_plugin() {}
93 // can add entries to the extension handshake
94 // this is not called for web seeds
95 virtual void add_handshake(entry
&) {}
97 // throwing an exception from any of the handlers (except add_handshake)
98 // closes the connection
100 // this is called when the initial BT handshake is received. Returning false
101 // means that the other end doesn't support this extension and will remove
102 // it from the list of plugins.
103 // this is not called for web seeds
104 virtual bool on_handshake(char const* reserved_bits
) { return true; }
106 // called when the extension handshake from the other end is received
107 // if this returns false, it means that this extension isn't
108 // supported by this peer. It will result in this peer_plugin
109 // being removed from the peer_connection and destructed.
110 // this is not called for web seeds
111 virtual bool on_extension_handshake(lazy_entry
const& h
) { return true; }
113 // returning true from any of the message handlers
114 // indicates that the plugin has handeled the message.
115 // it will break the plugin chain traversing and not let
116 // anyone else handle the message, including the default
119 virtual bool on_choke()
122 virtual bool on_unchoke()
125 virtual bool on_interested()
128 virtual bool on_not_interested()
131 virtual bool on_have(int index
)
134 virtual bool on_bitfield(bitfield
const& bitfield
)
137 virtual bool on_have_all()
140 virtual bool on_have_none()
143 virtual bool on_allowed_fast(int index
)
146 virtual bool on_request(peer_request
const& req
)
149 virtual bool on_piece(peer_request
const& piece
, disk_buffer_holder
& data
)
152 virtual bool on_cancel(peer_request
const& req
)
155 virtual bool on_reject(peer_request
const& req
)
158 virtual bool on_suggest(int index
)
161 // called when an extended message is received. If returning true,
162 // the message is not processed by any other plugin and if false
163 // is returned the next plugin in the chain will receive it to
164 // be able to handle it
165 // this is not called for web seeds
166 virtual bool on_extended(int length
167 , int msg
, buffer::const_interval body
)
170 // this is not called for web seeds
171 virtual bool on_unknown_message(int length
, int msg
172 , buffer::const_interval body
)
175 // called when a piece that this peer participated in either
176 // fails or passes the hash_check
177 virtual void on_piece_pass(int index
) {}
178 virtual void on_piece_failed(int index
) {}
180 // called aproximately once every second
181 virtual void tick() {}
183 // called each time a request message is to be sent. If true
184 // is returned, the original request message won't be sent and
185 // no other plugin will have this function called.
186 virtual bool write_request(peer_request
const& r
) { return false; }
193 #endif // TORRENT_EXTENSIONS_HPP_INCLUDED