8. Designed: April 2001
First released: July 2001 Bram Cohen
A
Peer-To-Peer Protocol
#1 usage:
Sharing Large Media Files
9. 170M active users* 40% of all Internet traffic**
10 popular clients
uTorrent, Xunlei, Transmission, Vuze, qBittorent,
Deluge, BitComet and Tixatit
* Wikipedia
CompaniesPiracy
** BitTorrent Inc.
10. Facilitates transfer of large/multiple files
Works on unreliable network connections
Downloads can be resumed any time
✔
✔
✔
17. 2
WebSeed
Similar to HTTP direct download
Guarantees availability of resources
Independent of swarm sanity
Extension on bitTorrent protocol
Introduced in 2006
22. Pros
Easily configurable
Supports trackerless mode, with
DHT
Supports Web Seeds (i.e. a seed can
be a HTTP host), and SSL Torrents
Supports Local Service Discovery
Pros and Cons
Cons
No CocoaPods/Carthage distribution
Depends on other libraries
Not based on URLConnection
Does not support iOS background
sessions
29. How to Build
https://github.com/xebia-france/libtorrent-ios-resources
Option A: Pre-configured Xcode Project
- https://github.com/chublix/libtorrent-ios
- Adapt for OpenSSL
Option B: Qt Creator
- Build OpenSSL and Boost separately
- Build libtorrent, for all the platforms (e.g. iphoneos and iphonesimulator)
- lipo (i.e., merge the slices) of the different platforms
Option C: Bash Script
- cf. xebia-france/libtorrent-ios-resources/build-script
35. How Incremental Download Works
No modification in the content: No Download
Modification in the content referenced changed: Download Starts
The SHA1 of each chunk is compared to the version on disk:
37. C++ / Swift Bridging
03
04
01
02 Add wrapper class’s header file into bridging header
Implement a wrapper class in .mm file (Obj C++ )
Use wrapper class in Swift03
38. C++ / Swift Bridging
02 Implement a wrapper class
Wrapper Class (Header)
#import <Foundation/Foundation.h>
@interface LibTorrentWrapper : NSObject {
- (void)initSession:(NSInteger)fromPort toPort:(NSInteger)toPort;
- (void)addNewTorrentFromFile:(NSString *)filePath;
}
39. C++ / Swift Bridging
02 Implement a wrapper class
Wrapper Class (Implementation)
@interface LibtorrentWrapper()
@property libtorrent::session *session;
@end
@implementation LibtorrentWrapper
- (void)initSession:(NSInteger)fromPort toPort:(NSInteger)toPort {
self.session = new libtorrent::session();
session_settings settings = self.session->settings();
// Apply Settings here...
self.session->set_settings(settings);
libtorrent::error_code ec;
self.session->listen_on(std::make_pair(fromPort, toPort), ec)
}
40. C++ / Swift Bridging
02 Implement a wrapper class
Wrapper Class (Implementation)
//...
- (void)addNewTorrentWithData:(NSData *)data {
libtorrent::add_torrent_params params;
params.save_path = // Some dir...
libtorrent::error_code ec;
params.ti = new libtorrent::torrent_info((const char*)data.bytes, data.length, ec);
self.session->add_torrent(params, ec);
}
@end
41. C++ / Swift Bridging
02 Implement a wrapper class
Invoke Wrapper Class in Swift
func startTorrentSession() {
var torrentWrapper = LibtorrentWrapper()
torrentWrapper.initSession(6881, toPort: 6889)
torrentWrapper.addNewTorrentWithData(/* Some Data */)
}
42. Event loop
03
04
01
02 Pop alert events each time callback is invoked
Set-up a timer that triggers a callback periodically
Feed torrent progress info03
43. Fetch alerts
- (void)fetchAlerts {
std::deque<alert *> deque;
_session->pop_alerts(&deque);
for (std::deque<alert *>::iterator it=deque.begin(); it != deque.end(); ++it) {
std::unique_ptr<alert> alert(*it);
switch (alert->type()) {
case metadata_received_alert::alert_type:
//**
case piece_finished_alert::alert_type:
//** You can feed progress info here
case torrent_finished_alert::alert_type:
//**
default: break;
}
}
deque.clear();
}
46. Caveats
1 Needs fine tuning, i.e. for avoiding heavy
battery consumption
No background sessions
Almost no iOS help resource available
System proxy support to be manually
implemented (via
CFNetworkCopySystemProxySettings)
2
3
4