From 5874d32a342cfaf5d387ddcad563d8f313e5a1a6 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 26 May 2009 01:03:56 +0000 Subject: [PATCH] updates to C bindings --- bindings/c/library.cpp | 34 +++++++++++++++++++++++++++------- bindings/c/libtorrent.h | 22 ++++++++++++++++++++++ bindings/c/simple_client.c | 7 +++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/bindings/c/library.cpp b/bindings/c/library.cpp index 85be0f43c..b40131537 100644 --- a/bindings/c/library.cpp +++ b/bindings/c/library.cpp @@ -282,7 +282,7 @@ TORRENT_EXPORT int session_add_torrent(void* ses, int tag, ...) return i; } -void session_remove_torrent(void* ses, int tor, int flags) +TORRENT_EXPORT void session_remove_torrent(void* ses, int tor, int flags) { using namespace libtorrent; torrent_handle h = get_handle(tor); @@ -292,7 +292,23 @@ void session_remove_torrent(void* ses, int tor, int flags) s->remove_torrent(h, flags); } -int session_set_settings(void* ses, int tag, ...) +TORRENT_EXPORT int session_pop_alert(void* ses, char* dest, int len, int* category) +{ + using namespace libtorrent; + + session* s = (session*)ses; + + std::auto_ptr a = s->pop_alert(); + if (!a.get()) return -1; + + if (category) *category = a->category(); + strncpy(dest, a->message().c_str(), len - 1); + dest[len - 1] = 0; + + return 0; // for now +} + +TORRENT_EXPORT int session_set_settings(void* ses, int tag, ...) { using namespace libtorrent; @@ -344,6 +360,10 @@ int session_set_settings(void* ses, int tag, ...) copy_proxy_setting(&ps, va_arg(lp, struct proxy_setting const*)); s->set_tracker_proxy(ps); } + case SET_ALERT_MASK: + { + s->set_alert_mask(va_arg(lp, int)); + } #ifndef TORRENT_DISABLE_DHT case SET_DHT_PROXY: { @@ -374,7 +394,7 @@ int session_set_settings(void* ses, int tag, ...) return 0; } -int session_get_setting(void* ses, int tag, void* value, int* value_size) +TORRENT_EXPORT int session_get_setting(void* ses, int tag, void* value, int* value_size) { using namespace libtorrent; session* s = (session*)ses; @@ -400,7 +420,7 @@ int session_get_setting(void* ses, int tag, void* value, int* value_size) } } -int session_get_status(void* sesptr, struct session_status* s, int struct_size) +TORRENT_EXPORT int session_get_status(void* sesptr, struct session_status* s, int struct_size) { libtorrent::session* ses = (libtorrent::session*)sesptr; @@ -457,7 +477,7 @@ int session_get_status(void* sesptr, struct session_status* s, int struct_size) return 0; } -int torrent_get_status(int tor, torrent_status* s, int struct_size) +TORRENT_EXPORT int torrent_get_status(int tor, torrent_status* s, int struct_size) { libtorrent::torrent_handle h = get_handle(tor); if (!h.is_valid()) return -1; @@ -515,7 +535,7 @@ int torrent_get_status(int tor, torrent_status* s, int struct_size) return 0; } -int torrent_set_settings(int tor, int tag, ...) +TORRENT_EXPORT int torrent_set_settings(int tor, int tag, ...) { using namespace libtorrent; torrent_handle h = get_handle(tor); @@ -557,7 +577,7 @@ int torrent_set_settings(int tor, int tag, ...) return 0; } -int torrent_get_setting(int tor, int tag, void* value, int* value_size) +TORRENT_EXPORT int torrent_get_setting(int tor, int tag, void* value, int* value_size) { using namespace libtorrent; torrent_handle h = get_handle(tor); diff --git a/bindings/c/libtorrent.h b/bindings/c/libtorrent.h index e7e744a34..c80906d05 100644 --- a/bindings/c/libtorrent.h +++ b/bindings/c/libtorrent.h @@ -85,6 +85,7 @@ enum tags SET_TRACKER_PROXY, // proxy_setting const*, session_only SET_DHT_PROXY, // proxy_setting const*, session_only SET_PROXY, // proxy_setting const*, session_only + SET_ALERT_MASK, // int, session_only }; struct proxy_setting @@ -98,6 +99,23 @@ struct proxy_setting int type; }; +enum category_t +{ + cat_error = 0x1, + cat_peer = 0x2, + cat_port_mapping = 0x4, + cat_storage = 0x8, + cat_tracker = 0x10, + cat_debug = 0x20, + cat_status = 0x40, + cat_progress = 0x80, + cat_ip_block = 0x100, + cat_performance_warning = 0x200, + cat_dht = 0x400, + + cat_all_categories = 0xffffffff +}; + enum proxy_type_t { proxy_none, @@ -256,6 +274,10 @@ void session_close(void* ses); int session_add_torrent(void* ses, int first_tag, ...); void session_remove_torrent(void* ses, int tor, int flags); +// return < 0 if there are no alerts. Otherwise returns the +// type of alert that was returned +int session_pop_alert(void* ses, char* dest, int len, int* category); + int session_get_status(void* ses, struct session_status* s, int struct_size); // use SET_* tags in tag list diff --git a/bindings/c/simple_client.c b/bindings/c/simple_client.c index cce5fe078..3c1d7a2be 100644 --- a/bindings/c/simple_client.c +++ b/bindings/c/simple_client.c @@ -55,6 +55,7 @@ int main(int argc, char* argv[]) void* ses = session_create( SES_LISTENPORT, 6881, SES_LISTENPORT_END, 6889, + SES_ALERT_MASK, ~(cat_progress | cat_port_mapping | cat_debug | cat_performance_warning | cat_peer), TAG_END); int t = session_add_torrent(ses, @@ -97,6 +98,12 @@ int main(int argc, char* argv[]) , message); + char msg[400]; + while (session_pop_alert(ses, msg, sizeof(msg), 0) >= 0) + { + printf("%s\n", msg); + } + if (strlen(st.error) > 0) { fprintf(stderr, "\nERROR: %s\n", st.error);