1 // Copyright Daniel Wallin 2007. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include <libtorrent/extensions.hpp>
9 #include <libtorrent/entry.hpp>
10 #include <libtorrent/lazy_entry.hpp>
11 #include <libtorrent/peer_request.hpp>
12 #include <libtorrent/disk_buffer_holder.hpp>
13 #include <libtorrent/bitfield.hpp>
14 #include <boost/python.hpp>
16 using namespace boost::python
;
17 using namespace libtorrent
;
21 struct peer_plugin_wrap
: peer_plugin
, wrapper
<peer_plugin
>
23 void add_handshake(entry
& e
)
25 if (override f
= this->get_override("add_handshake"))
26 e
= call
<entry
>(f
.ptr(), e
);
28 peer_plugin::add_handshake(e
);
31 void default_add_handshake(entry
& e
)
33 this->peer_plugin::add_handshake(e
);
36 bool on_handshake(char const* reserved_bits
)
38 if (override f
= this->get_override("on_handshake"))
41 return peer_plugin::on_handshake(reserved_bits
);
44 bool default_on_handshake(char const* reserved_bits
)
46 return this->peer_plugin::on_handshake(reserved_bits
);
49 bool on_extension_handshake(lazy_entry
const& e
)
51 if (override f
= this->get_override("on_extension_handshake"))
54 return peer_plugin::on_extension_handshake(e
);
57 bool default_on_extension_handshake(lazy_entry
const& e
)
59 return this->peer_plugin::on_extension_handshake(e
);
64 if (override f
= this->get_override("on_choke"))
67 return peer_plugin::on_choke();
70 bool default_on_choke()
72 return this->peer_plugin::on_choke();
77 if (override f
= this->get_override("on_unchoke"))
80 return peer_plugin::on_unchoke();
83 bool default_on_unchoke()
85 return this->peer_plugin::on_unchoke();
90 if (override f
= this->get_override("on_interested"))
93 return peer_plugin::on_interested();
96 bool default_on_interested()
98 return this->peer_plugin::on_interested();
101 bool on_not_interested()
103 if (override f
= this->get_override("on_not_interested"))
106 return peer_plugin::on_not_interested();
109 bool default_on_not_interested()
111 return this->peer_plugin::on_not_interested();
114 bool on_have(int index
)
116 if (override f
= this->get_override("on_have"))
119 return peer_plugin::on_have(index
);
122 bool default_on_have(int index
)
124 return this->peer_plugin::on_have(index
);
127 bool on_bitfield(list _bf
)
129 //Convert list to a bitfield
130 bitfield
bf(len(_bf
));
131 for (int i
= 0; i
< len(_bf
); ++i
)
138 if (override f
= this->get_override("on_bitfield"))
141 return peer_plugin::on_bitfield(bf
);
144 bool default_on_bitfield(const bitfield
&bf
)
146 return this->peer_plugin::on_bitfield(bf
);
149 bool on_request(peer_request
const& req
)
151 if (override f
= this->get_override("on_request"))
154 return peer_plugin::on_request(req
);
157 bool default_on_request(peer_request
const& req
)
159 return this->peer_plugin::on_request(req
);
162 bool on_piece(peer_request
const& piece
, disk_buffer_holder
& data
)
164 if (override f
= this->get_override("on_piece"))
165 return f(piece
, data
);
167 return peer_plugin::on_piece(piece
, data
);
170 bool default_on_piece(peer_request
const& piece
, disk_buffer_holder
& data
)
172 return this->peer_plugin::on_piece(piece
, data
);
175 bool on_cancel(peer_request
const& req
)
177 if (override f
= this->get_override("on_cancel"))
180 return peer_plugin::on_cancel(req
);
183 bool default_on_cancel(peer_request
const& req
)
185 return this->peer_plugin::on_cancel(req
);
188 bool on_extended(int length
, int msg
, buffer::const_interval body
)
190 if (override f
= this->get_override("on_extended"))
191 return f(length
, msg
, body
);
193 return peer_plugin::on_extended(length
, msg
, body
);
196 bool default_on_extended(int length
, int msg
, buffer::const_interval body
)
198 return this->peer_plugin::on_extended(length
, msg
, body
);
201 bool on_unknown_message(int length
, int msg
, buffer::const_interval body
)
203 if (override f
= this->get_override("on_unknown_message"))
204 return f(length
, msg
, body
);
206 return peer_plugin::on_unknown_message(length
, msg
, body
);
209 bool default_on_unknown_message(int length
, int msg
, buffer::const_interval body
)
211 return this->peer_plugin::on_unknown_message(length
, msg
, body
);
214 void on_piece_pass(int index
)
216 if (override f
= this->get_override("on_piece_pass"))
219 peer_plugin::on_piece_pass(index
);
222 void default_on_piece_pass(int index
)
224 this->peer_plugin::on_piece_pass(index
);
227 void on_piece_failed(int index
)
229 if (override f
= this->get_override("on_piece_failed"))
232 peer_plugin::on_piece_failed(index
);
235 void default_on_piece_failed(int index
)
237 this->peer_plugin::on_piece_failed(index
);
242 if (override f
= this->get_override("tick"))
250 this->peer_plugin::tick();
253 bool write_request(peer_request
const& req
)
255 if (override f
= this->get_override("write_request"))
258 return peer_plugin::write_request(req
);
261 bool default_write_request(peer_request
const& req
)
263 return this->peer_plugin::write_request(req
);
269 static char const data
[] = "foobar";
270 return object(handle
<>(PyBuffer_FromMemory((void*)data
, 6)));
273 } // namespace unnamed
275 void bind_peer_plugin()
278 peer_plugin_wrap
, boost::shared_ptr
<peer_plugin_wrap
>, boost::noncopyable
282 , &peer_plugin::add_handshake
, &peer_plugin_wrap::default_add_handshake
286 , &peer_plugin::on_handshake
, &peer_plugin_wrap::default_on_handshake
289 "on_extension_handshake"
290 , &peer_plugin::on_extension_handshake
291 , &peer_plugin_wrap::default_on_extension_handshake
295 , &peer_plugin::on_choke
, &peer_plugin_wrap::default_on_choke
299 , &peer_plugin::on_unchoke
, &peer_plugin_wrap::default_on_unchoke
303 , &peer_plugin::on_interested
, &peer_plugin_wrap::default_on_interested
307 , &peer_plugin::on_not_interested
, &peer_plugin_wrap::default_on_not_interested
311 , &peer_plugin::on_have
, &peer_plugin_wrap::default_on_have
315 , &peer_plugin::on_bitfield
, &peer_plugin_wrap::default_on_bitfield
319 , &peer_plugin::on_request
, &peer_plugin_wrap::default_on_request
323 , &peer_plugin::on_piece
, &peer_plugin_wrap::default_on_piece
327 , &peer_plugin::on_cancel
, &peer_plugin_wrap::default_on_cancel
331 , &peer_plugin::on_piece_pass
, &peer_plugin_wrap::default_on_piece_pass
335 , &peer_plugin::on_piece_failed
, &peer_plugin_wrap::default_on_piece_failed
339 , &peer_plugin::tick
, &peer_plugin_wrap::default_tick
343 , &peer_plugin::write_request
, &peer_plugin_wrap::default_write_request
345 // These seem to make VC7.1 freeze. Needs special handling.
349 , &peer_plugin::on_extended, &peer_plugin_wrap::default_on_extended
353 , &peer_plugin::on_unknown_message, &peer_plugin_wrap::default_on_unknown_message
357 def("get_buffer", &get_buffer
);