00001 /* 00002 00003 Copyright (c) 2006, Arvid Norberg 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 00010 * Redistributions of source code must retain the above copyright 00011 notice, this list of conditions and the following disclaimer. 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the distribution. 00015 * Neither the name of the author nor the names of its 00016 contributors may be used to endorse or promote products derived 00017 from this software without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00023 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 POSSIBILITY OF SUCH DAMAGE. 00030 00031 */ 00032 00033 #ifndef TORRENT_EXTENSIONS_HPP_INCLUDED 00034 #define TORRENT_EXTENSIONS_HPP_INCLUDED 00035 00036 #ifndef TORRENT_DISABLE_EXTENSIONS 00037 00038 #ifdef _MSC_VER 00039 #pragma warning(push, 1) 00040 #endif 00041 00042 #include <boost/shared_ptr.hpp> 00043 00044 #ifdef _MSC_VER 00045 #pragma warning(pop) 00046 #endif 00047 00048 #include <vector> 00049 #include "libtorrent/config.hpp" 00050 #include "libtorrent/buffer.hpp" 00051 00052 namespace libtorrent 00053 { 00054 struct peer_plugin; 00055 class bt_peer_connection; 00056 struct peer_request; 00057 class peer_connection; 00058 class entry; 00059 00060 struct TORRENT_EXPORT torrent_plugin 00061 { 00062 virtual ~torrent_plugin() {} 00063 // throwing an exception closes the connection 00064 // returning a 0 pointer is valid and will not add 00065 // the peer_plugin to the peer_connection 00066 virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection*) 00067 { return boost::shared_ptr<peer_plugin>(); } 00068 00069 virtual void on_piece_pass(int index) {} 00070 virtual void on_piece_failed(int index) {} 00071 00072 // called aproximately once every second 00073 virtual void tick() {} 00074 00075 // if true is returned, it means the handler handled the event, 00076 // and no other plugins will have their handlers called, and the 00077 // default behavior will be skipped 00078 virtual bool on_pause() { return false; } 00079 virtual bool on_resume() { return false;} 00080 }; 00081 00082 struct TORRENT_EXPORT peer_plugin 00083 { 00084 virtual ~peer_plugin() {} 00085 00086 // can add entries to the extension handshake 00087 virtual void add_handshake(entry&) {} 00088 00089 // throwing an exception from any of the handlers (except add_handshake) 00090 // closes the connection 00091 00092 // this is called when the initial BT handshake is received. Returning false 00093 // means that the other end doesn't support this extension and will remove 00094 // it from the list of plugins. 00095 virtual bool on_handshake() { return true; } 00096 00097 // called when the extension handshake from the other end is received 00098 // if this returns false, it means that this extension isn't 00099 // supported by this peer. It will result in this peer_plugin 00100 // being removed from the peer_connection and destructed. 00101 virtual bool on_extension_handshake(entry const& h) { return true; } 00102 00103 // returning true from any of the message handlers 00104 // indicates that the plugin has handeled the message. 00105 // it will break the plugin chain traversing and not let 00106 // anyone else handle the message, including the default 00107 // handler. 00108 00109 virtual bool on_choke() 00110 { return false; } 00111 00112 virtual bool on_unchoke() 00113 { return false; } 00114 00115 virtual bool on_interested() 00116 { return false; } 00117 00118 virtual bool on_not_interested() 00119 { return false; } 00120 00121 virtual bool on_have(int index) 00122 { return false; } 00123 00124 virtual bool on_bitfield(std::vector<bool> const& bitfield) 00125 { return false; } 00126 00127 virtual bool on_request(peer_request const& req) 00128 { return false; } 00129 00130 virtual bool on_piece(peer_request const& piece, char const* data) 00131 { return false; } 00132 00133 virtual bool on_cancel(peer_request const& req) 00134 { return false; } 00135 00136 // called when an extended message is received. If returning true, 00137 // the message is not processed by any other plugin and if false 00138 // is returned the next plugin in the chain will receive it to 00139 // be able to handle it 00140 virtual bool on_extended(int length 00141 , int msg, buffer::const_interval body) 00142 { return false; } 00143 00144 virtual bool on_unknown_message(int length, int msg 00145 , buffer::const_interval body) 00146 { return false; } 00147 00148 // called when a piece that this peer participated in either 00149 // fails or passes the hash_check 00150 virtual void on_piece_pass(int index) {} 00151 virtual void on_piece_failed(int index) {} 00152 00153 // called aproximately once every second 00154 virtual void tick() {} 00155 00156 // called each time a request message is to be sent. If true 00157 // is returned, the original request message won't be sent and 00158 // no other plugin will have this function called. 00159 virtual bool write_request(peer_request const& r) { return false; } 00160 }; 00161 00162 } 00163 00164 #endif 00165 00166 #endif // TORRENT_EXTENSIONS_HPP_INCLUDED 00167
1.5.6