Thứ Ba, 6 tháng 11, 2007
Allway Sync 5.5.8 - Đồng bộ dữ liệu quá dễ dàng
Chủ Nhật, 4 tháng 11, 2007
Công nghệ ADSL
- Tổng quan về ADSL
- Cơ chế hoạt động với ADSL
- Các thành phần của ADSL
- Kết nối mạng ADSL
- Cấu trúc của ADSL
- Mối tương quan giữa điện thoại và ADSL
Tổng quan về ADSL
ADSL là gì ? Hiểu một cách đơn giản nhất, ADSL là sự thay thế với tốc độ cao cho thiết bị Modem hoặc ISDN giúp truy nhập Internet với tốc độ cao và nhanh hơn. Các biểu đồ sau chỉ ra các tốc độ cao nhất có thể đạt được giữa các dịch vụ cung cấp
ADSL có nghĩa ntn?
ADSL viết tắt của Asymmetric Digital Subscriber Line-đó là đường thuê bao số không đối xứng, kỹ thuật truyền được sử dụng trên đường dây từ Modem của thuê bao tới Nhà cung cấp dịch vụ.Asymmetric: Tốc độ truyền không giống nhau ở hai chiều. Tốc độ của chiều xuống (từ mạng tới thuê bao) có thể nhanh gấp hơn 10 lần so với tốc độ của chiều lên (từ thuê bao tới mạng). Ðiều này phù hợp một cách tuyệt vời cho việc khai thác dịch vụ Internet khi mà chỉ cần nhấn chuột (tương ứng với lưu lượng nhỏ thông tin mà thuê bao gửi đi) là có thể nhận được một lưu lượng lớn dữ liệu tải về từ Internet.
Digital: Các Modem ADSL hoạt động ở mức bít (0 & 1) và dùng để chuyển thông tin số hoá giữa các thiết bị số như các máy tính PC. Chính ở khía cạnh này thì ADSL không có gì khác với các Modem thông thường.
Subscriber Line: ADSL tự nó chỉ hoạt động trên đường dây thuê bao bình thường nối tới tổng đài nội hạt. Ðường dây thuê bao này vẫn có thể được tiếp tục sử dụng cho các cuộc gọi đi hoặc nghe điện thoại cùng một thời điểm thông qua thiết bị gọi là "Splitters" có chức năng tách thoại và dữ liệu trên đường dây.
ADSL dùng để làm gì ?
ADSL xác lập cách thức dữ liệu được truyền giữa thuê bao (nhà riêng hoặc công sở) và tổng đài thoại nội hạt trên chính đường dây điện thoại bình thường. Chúng ta vẫn thường gọi các đường dây này là local loop.
Thực chất của ứng dụng ADSL không phải ở việc truyền dữ liệu đi/đến tổng đài điện thoại nội hạt mà là tạo ra khả năng truy nhập Internet với tốc độ cao. Như vậy, vấn đề nằm ở việc xác lập kết nối dữ liệu tới Nhà cung cấp dịch vụ Internet. Mặc dù chúng ta cho rằng ADSL được sử dụng để truyền dữ liệu bằng các giao thức Internet, nhưng trên thực tế việc thực hiện điều đó như thế nào lại không phải là đặc trưng kỹ thuật của ADSL. Hiện nay, phần lớn người ta ứng dụng ADSL cho truy nhập Internet tốc độ cao và sử dụng các dịch vụ trên Internet một cách nhanh hơn.
Cơ chế hoạt động với ADSL
ADSL vận hành ra sao?
ADSL tìm cách khai thác phần băng thông tương tự còn chưa được sử dụng trên đường dây nối từ thuê bao tới tổng đài nội hạt. Ðường dây này được thiết kế để chuyển tải dải phổ tần số (frequency spectrum) chiếm bởi cuộc thoại bình thường. Tuy nhiên, nó cũng có thể chuyển tải các tần số cao hơn dải phổ tương đối hạn chế dành cho thoại. Ðó là dải phổ mà ADSL sử dụng.
Thoại cơ bản sử dụng dải tần số từ 300Hz tới 3,400Hz.
Bây giờ chúng ta sẽ xem xét, thoại và dữ liệu ADSL chia xẻ cùng một đường dây thuê bao ra sao. Trên thực tế, các Splitter được sử dụng để đảm bảo dữ liệu và thoại không xâm phạm lẫn nhau trên đường truyền. Các tần số mà mạch vòng có thể chuyển tải, hay nói cách khác là khối lượng dữ liệu có thể chuyển tải sẽ phụ thuộc vào các nhân tố sau:
-
- Khoảng cách từ tổng đài nội hạt.
-
- Kiểu và độ dầy đường dây.
-
- Kiểu và số lượng các mối nối trên đường dây.
-
- Mật độ các đường dây chuyển tải ADSL, ISDN và các tín hiệu phi thoại khác.
-
- Mật độ các đường dây chuyển tải tín hiệu radio.
ƯU ĐIỂM CỦA ADSL
ADSL: So sánh với PSTN & ISDN
Vậy sự khác nhau cố hữu giữa ADSL với Modem quay số truyền thống và ISDN là như thế nào?
-
PSTN và ISDN là các công nghệ quay số (Dial-up).
-
ADSL là 'liên tục/always-on" kết nối trực tiếp.
-
PSTN và ISDN cho phép chúng ta sử dụng Fax, dữ liệu, thoại, dữ liệu tới Internet, dữ liệu tới các thiết bị khác.
-
ADSL chỉ chuyển tải dữ liệu tới Internet.
-
PSTN và ISDN cho phép chúng ta tuỳ chọn ISP nào mà ta muốn kết nối.
-
ADSL kết nối chúng ta tới một ISP định trước.
-
ISDN chạy ở tốc độ cơ sở 64kbps hoặc 128kbps.
-
ADSL có thể tải dữ liệu về với tốc độ tới 8Mbps.
-
PSTN ngắt truy nhập tới Internet khi chúng ta thực hiện cuộc gọi.
-
ADSL cho phép vừa sử dụng Internet trong khi vẫn có thể thực hiện cuộc gọi đồng thời.
-
Kết nối Internet qua đường PSTN và ISDN bằng phương thức quay số có tính cước nội hạt.
-
ADSL không tính cước nội hạt.
Ghi chú:
-
Mặc dù Modem ADSL luôn ở chế độ kết nối thường trực, nhưng vẫn có thể cần phải thực hiện lệnh kết nối Internet trên máy PC.
-
Các dịch vụ như Fax và thoại có thể được thực hiện cũng trên kết nối dữ liệu ADSL tới Internet.
-
Trên thực tế, tốc độ Download tiêu biểu đối với dịch vụ ADSL gia đình thường đạt tới (up to) 400kbps
-
Dùng bao nhiêu, trả bấy nhiêu. Cấu trúc cước theo lưu lượng sử dụng (Hoặc theo thời gian sử dụng).
-
Không hạn chế số người sử dụng khi chia sẻ kết nối Internet trong mạng
Modem ADSL kết nối vào đường dây điện thoại (còn gọi là local loop) và đường dây này nối tới thiết bị tổng đài nội hạt. Modem ADSL sử dụng kết hợp một loạt các kỹ thuật xử lý tín hiệu tiên tiến nhằm đạt được tốc độ băng thông cần thiết trên đường dây điện thoại thông thường với khoảng cách tới vài km giữa thuê bao và tổng đài nội hạt. Modem ADSL làm việc như thế nào?
ADSL hoạt động bằng cách vận hành cùng lúc nhiều Modem, trong đó mỗi Modem sử dụng phần băng thông riêng có thể. Sơ đồ trên đây chỉ mô phỏng một cách tương đối, nhưng qua đó ta có thể nhận thấy ADSL sử dụng rất nhiều Modem riêng lẻ hoạt động song song để khai thác băng thông tối đa và cung cấp một tốc độ rất cao. Mỗi đường kẻ sọc đen ở trên thể hiện một Modem và chúng hoạt động tại các tần số hoàn toàn khác nhau. Trên thực tế có thể tới 255 Modem hoạt động trên một đường ADSL. Ðiểm đặc biệt ở chỗ ADSL sử dụng dải tần số từ 26kHz tới 1.1MHz trong 10MHz của băng thông thoại. Tất cả 255 Modems này được vận hành chỉ trên một con chíp đơn. Lượng dữ liệu mà mỗi Modem có thể truyền tải phụ thuộc vào các đặc điểm của đường dây tại tần số mà Modem đó chiếm. Một số Modem có thể không làm việc một chút nào vì sự gây nhiễu từ nguồn tín hiệu bên ngoài chẳng hạn như bởi một đường dây (local loop) khác hoặc nguồn phát vô tuyến nào đó. Các Modem ở tần số cao hơn thông thường lại truyền tải được ít dữ liệu hơn bởi lý ở tần số càng cao thì sự suy hao càng lớn, đặc biệt là trên một khoảng cách dài.Mạch vòng / Local Loop là gì ?
'Local loop' là thuật ngữ dùng để chỉ các đường dây điện thoại bình thường nối từ vị trí người sử dụng tới công ty điện thoại. Nguyên nhân xuất hiện thuật ngữ local loop - đó là người nghe (điện thoại) được kết nối vào hai đường dây mà nếu nhìn từ tổng đài điện thoại thì chúng tạo ra một mạch vòng local loop.CÁC THÀNH PHẦN ADSL TỪ PHÍA NHÀ CUNG CẤP DỊCH VỤ
Bây giờ chúng ta sẽ tìm hiểu xem các ISP thực hiện cung cấp ADSL như thế nào. Như chỉ ra trong khối vàng ở trên, phạm vi Nhà cung cấp dịch vụ gồm có ba thành phần quan trọng- DSLAM - DSL Access Multiplexer.
- BAS - Broadband Access Server.
- ISP - Internet Service Provider.
Một thiết bị DSLAM có thể tập hợp nhiều kết nối thuê bao ADSL - có thể nhiều tới hàng trăm thuê bao - và tụ lại trên một kết nối cáp quang. Sợi cáp quang này thường được nối tới thiết bị gọi là BAS - Broadband Access Server, nhưng nó cũng có thể không nối trực tiếp tới BAS vì BAS có thể được đặt tại bất cứ đâu.
DSLAM là thiết bị đặt ở phía tổng đài, là điểm cuối của kết nối ADSL. Nó chứa vô số các Modem ADSL bố trí về một phía hướng tới các mạch vòng và phía kia là kết nối cáp quang.Vậy BAS là gì?
Broadband Access Server (BAS) là thiết bị đặt giữa DSLAM và POP của ISP. Một thiết bị BAS có thể phục vụ cho nhiều DSLAM. Các giao thức truyền thông được đóng gói để truyền dữ liệu thông qua kết nối ADSL, vì vậy mục đích của BAS là mở gói để hoàn trả lại các giao thức đó trước khi đi vào Internet. Nó cũng đảm bảo cho kết nối của bạn tới ISP được chính xác giống như khi bạn sử dụng Modem quay số hoặc ISDN. Như chú giải ở trên, ADSL không chỉ rõ các giao thức được sử dụng để tạo thành kết nối tới Internet. Phương pháp mà PC và Modem sử dụng bắt buộc phải giống như BAS sử dụng để cho kết nối thực hiện được. Thông thường ADSL sử dụng hai giao thức chính là :- PPPoE - PPP over Ethernet Protocol.
- PPPoA - Point to Point Protocol over ATM.
Các thành phần kết nối như thế nào?
Dưới đây sẽ trình bày về những giao thức truyền thông được sử dụng trên kết nối ADSL. Khi kết nối vào Internet, bạn sử dụng các giao thức chạy ở tầng vận chuyển TCP/IP (chẳng hạn như HTTP - giao thức được sử dụng bởi các Web Browser). Quá trình này là giống nhau với các kiểu truy nhập quay số qua PSTN, ISDN và ADSL.Các giao thức được sử dụng giữa Modem và BAS
Khi quay số PSTN/ISDN để truy nhập vào Internet, chúng ta sử dụng giao thức gọi là PPP để vận chuyển dữ liệu TCP/IP và kiểm tra cũng như xác thực tên và mật khẩu người truy nhập. Trong ADSL, PPP cũng thường được sử dụng để kiểm tra tên và mật khẩu truy nhập, và ATM thì luôn được sử dụng ở mức thấp nhất. Kết nối điển hình như dưới đây : Vai trò của ATM ATM - Asynchronous Transfer Mode - được sử dụng như là công cụ chuyển tải cho ADSL ở mức thấp. Lý do vì đó là cách thuận tiện và mềm dẻo đối với các công ty thoại muốn kéo dài khoảng cách kết nối từ DSLAM tới BAS giúp họ có thể đặt BAS ở bất cứ đâu trên mạng. Các tham số thiết lập cấu hình ATM Có hai tham số cần phải thiết lập cấu hình một cách chính xác trên Modem ADSL để đảm bảo kết nối thành công tại mức ATM với DSLAM:- VPI - the Virtual Path Identifier.
- VCI - the Virtual Channel Identifier.
PPP là giao thức dùng để vận chuyển lưu lượng Internet tới ISP dọc theo các kết nối Modem và ISDN. PPP kết hợp chặt chẽ các yếu tố xác thực - kiểm tra tên/mật khẩu - và đó là lý do chính mà người ta dùng PPP với ADSL. Mặc dù BAS thực thi giao thức PPP và tiến hành việc xác thực, nhưng thực ra việc đó được thực hiện bằng cách truy nhập vào các cơ sở dữ liệu khách hàng đặt tại ISP. Bằng cách đó, ISP biết được rằng các kết nối do BAS định tuyến tới - đã được xác thực thông qua giao dịch với cơ sở dữ liệu riêng của ISP.
MODEM ADSL TRÊN THỰC TẾCác loại modem ADSL thông minh và thụ động
Modem ADSL thông minh bản thân nó đã tích hợp sẵn các giao thức truyền thông cần thiết (Như thiết bị Modem ADSL Router hoặc Modem được sử dụng kết nối qua cổng Card Ethernet 10/100Mb) nên chỉ việc lựa chọn và khai báo VPI/VCI cho Modem. Còn Modem ADSL thụ động thì phải hoạt động dựa trên hệ điều hành của máy tính để cung cấp các giao thức cần thiết. Các loại Modem này bắt buộc phải cài đặt phần mềm điều khiển Modem và thiết lập các giao thức PPP, VPI/VCI. Việc cấu hình như vậy phức tạp và đòi hỏi thời gian nhiều hơn. Chỉ có Windows 98SE, Windows ME và Windows 2000/XP là có cài sẵn cơ chế thực thi ATM, vì thế người ta ít sử dụng các Modem thụ động trên thực tế. Mặc dù các Modem thông minh có hỗ trợ các giao thức cần thiết nhưng chúng vẫn có thể được dùng cho các hệ điều hành nói trên. Các Modem thụ động có thể nối với PC thông qua giao diện USB, hoặc có thể được sản xuất dưới dạng PCI Card để cắm thẳng trên bảng mạch chủ của PC. Lưu ý là việc khai thác giao thức ATM không có nghĩa là cần phải có Card mạng ATM cho PC - đó chỉ là cơ chế hỗ trợ bằng phần mềm trong hệ điều hành.Mối tương quan giữa điện thoại và ADSL
Thoại và ADSL cùng chung sống ra sao?ADSL cho phép cùng lúc vừa truy nhập Internet tốc độ cao lại vừa có thể thực hiện cuộc gọi cũng trên đường dây đó. Thiết bị chuyên dụng Splitters được sử dụng để tách riêng các tần số cao dùng cho ADSL và các tần số thấp dùng cho thoại. Như vậy, người ta thường đặt các Splitters tại mỗi đầu của đường dây - phía thuê bao và phía DSLAM.
Tại phía thuê bao, các tần số thấp được chuyển đến máy điện thoại còn các tần số cao đi đến modem ADSL. Tại các tổng đài, các tần số thấp được chuyển sang mạng thoại PSTN còn các tần số cao đi đến ISP.Tốc độ đa dạng
Tốc độ của kết nối giữa modem ADSL và DSLAM phụ thuộc vào khoảng cách đường truyền và tốc độ tối đa được cấu hình sẵn trên cổng của DSLAM. Còn tốc độ kết nối vào Internet lại còn phụ thuộc vào nhiều yếu tố khác nữa như dưới đây : Số người dùng kết nối vào cùng một DSLAM và thực tế có bao nhiều người dùng đang khai thác kết nối.Tốc độ kết nối giữa DSLAM và BAS.
Bao nhiêu card DSLAM cùng nối vào một BAS và bao nhiêu người dùng đang khai thác thực tế kết nối.Tốc độ kết nối giữa BAS và ISP.
Bao nhiêu BAS kết nối vào ISP và bao nhiêu người dùng thực tế đang khai thác.Tốc độ của kết nối từ ISP tới mạng Internet toàn cầu.
Bao nhiêu thuê bao của ISP đang khai thác (qua các giao tiếp khác nhau như quay số PSTN/ ISDN và ADSL).ISP tổ chức Caching và Proxy ra sao, liệu thông tin mà bạn cần khai thác đã được lưu trữ trên Cache chưa hay phải tải về từ Internet.
Thứ Sáu, 2 tháng 11, 2007
BitTorrent
BitTorrent is a protocol for distributing files. It identifies content by URL and is designed to integrate seamlessly with the web. Its advantage over plain HTTP is that when multiple downloads of the same file happen concurrently, the downloaders upload to each other, making it possible for the file source to support very large numbers of downloaders with only a modest increase in its load.
A BitTorrent file distribution consists of these entities:
- An ordinary web server
- A static 'metainfo' file
- A BitTorrent tracker
- An 'original' downloader
- The end user web browsers
- The end user downloaders
There are ideally many end users for a single file.
To start serving, a host goes through the following steps:
- Start running a tracker (or, more likely, have one running already).
- Start running an ordinary web server, such as apache, or have one already.
- Associate the extension .torrent with mimetype
application/x-bittorrenton their web server (or have done so already). - Generate a metainfo (.torrent) file using the complete file to be served and the URL of the tracker.
- Put the metainfo file on the web server.
- Link to the metainfo (.torrent) file from some other web page.
- Start a downloader which already has the complete file (the 'origin').
To start downloading, a user does the following:
- Install BitTorrent (or have done so already).
- Surf the web.
- Click on a link to a .torrent file.
- Select where to save the file locally, or select a partial download to resume.
- Wait for download to complete.
- Tell downloader to exit (it keeps uploading until this happens).
The connectivity is as follows:
- Strings are length-prefixed base ten followed by a colon and the string. For example
4:spamcorresponds to 'spam'. - Integers are represented by an 'i' followed by the number in base 10 followed by an 'e'. For example
i3ecorresponds to 3 andi-3ecorresponds to -3. Integers have no size limitation.i-0eis invalid. All encodings with a leading zero, such asi03e, are invalid, other thani0e, which of course corresponds to 0. - Lists are encoded as an 'l' followed by their elements (also bencoded) followed by an 'e'. For example
l4:spam4:eggsecorresponds to ['spam', 'eggs']. - Dictionaries are encoded as a 'd' followed by a list of alternating keys and their corresponding values followed by an 'e'. For example,
d3:cow3:moo4:spam4:eggsecorresponds to {'cow': 'moo', 'spam': 'eggs'} andd4:spaml1:a1:beecorresponds to {'spam': ['a', 'b']}. Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics).
Metainfo files are bencoded dictionaries with the following keys:
announce- The URL of the tracker.
info- This maps to a dictionary, with keys described below.
- The
namekey maps to a string which is the suggested name to save the file (or directory) as. It is purely advisory. piece lengthmaps to the number of bytes in each piece the file is split into. For the purposes of transfer, files are split into fixed-size pieces which are all the same length except for possibly the last one which may be truncated.piece lengthis almost always a power of two, most commonly 2 18 = 256 K (BitTorrent prior to version 3.2 uses 2 20 = 1 M as default).piecesmaps to a string whose length is a multiple of 20. It is to be subdivided into strings of length 20, each of which is the SHA1 hash of the piece at the corresponding index.- There is also a key
lengthor a keyfiles, but not both or neither. Iflengthis present then the download represents a single file, otherwise it represents a set of files which go in a directory structure. - In the single file case,
lengthmaps to the length of the file in bytes. - For the purposes of the other keys, the multi-file case is treated as only having a single file by concatenating the files in the order they appear in the files list. The files list is the value
filesmaps to, and is a list of dictionaries containing the following keys: length- The length of the file, in bytes.path- A list of strings corresponding to subdirectory names, the last of which is the actual file name (a zero length list is an error case).- In the single file case, the name key is the name of a file, in the muliple file case, it's the name of a directory.
Tracker GET requests have the following keys:
info_hash- The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. Note that this is a substring of the metainfo file. This value will almost certainly have to be escaped.
peer_id- A string of length 20 which this downloader uses as its id. Each downloader generates its own id at random at the start of a new download. This value will also almost certainly have to be escaped.
ip- An optional parameter giving the IP (or dns name) which this peer is at. Generally used for the origin if it's on the same machine as the tracker.
port- The port number this peer is listening on. Common behavior is for a downloader to try to listen on port 6881 and if that port is taken try 6882, then 6883, etc. and give up after 6889.
uploaded- The total amount uploaded so far, encoded in base ten ascii.
downloaded- The total amount downloaded so far, encoded in base ten ascii.
left- The number of bytes this peer still has to download, encoded in base ten ascii. Note that this can't be computed from downloaded and the file length since it might be a resume, and there's a chance that some of the downloaded data failed an integrity check and had to be re-downloaded.
event- This is an optional key which maps to
started,completed, orstopped(orempty, which is the same as not being present). If not present, this is one of the announcements done at regular intervals. An announcement usingstartedis sent when a download first begins, and one usingcompletedis sent when the download is complete. Nocompletedis sent if the file was complete when started. Downloaders send an announcement usingstoppedwhen they cease downloading.
Tracker responses are bencoded dictionaries. If a tracker response has a key failure reason, then that maps to a human readable string which explains why the query failed, and no other keys are required. Otherwise, it must have two keys: interval, which maps to the number of seconds the downloader should wait between regular rerequests, and peers. peers maps to a list of dictionaries corresponding to peers, each of which contains the keys peer id, ip, and port, which map to the peer's self-selected ID, IP address or dns name as a string, and port number, respectively. Note that downloaders may rerequest on nonscheduled times if an event happens or they need more peers.
If you want to make any extensions to metainfo files or tracker queries, please coordinate with Bram Cohen to make sure that all extensions are done compatibly.
BitTorrent's peer protocol operates over TCP. It performs efficiently without setting any socket options.
Peer connections are symmetrical. Messages sent in both directions look the same, and data can flow in either direction.
The peer protocol refers to pieces of the file by index as described in the metainfo file, starting at zero. When a peer finishes downloading a piece and checks that the hash matches, it announces that it has that piece to all of its peers.
Connections contain two bits of state on either end: choked or not, and interested or not. Choking is a notification that no data will be sent until unchoking happens. The reasoning and common techniques behind choking are explained later in this document.
Data transfer takes place whenever one side is interested and the other side is not choking. Interest state must be kept up to date at all times - whenever a downloader doesn't have something they currently would ask a peer for in unchoked, they must express lack of interest, despite being choked. Implementing this properly is tricky, but makes it possible for downloaders to know which peers will start downloading immediately if unchoked.
Connections start out choked and not interested.
When data is being transferred, downloaders should keep several piece requests queued up at once in order to get good TCP performance (this is called 'pipelining'.) On the other side, requests which can't be written out to the TCP buffer immediately should be queued up in memory rather than kept in an application-level network buffer, so they can all be thrown out when a choke happens.
The peer wire protocol consists of a handshake followed by a never-ending stream of length-prefixed messages. The handshake starts with character ninteen (decimal) followed by the string 'BitTorrent protocol'. The leading character is a length prefix, put there in the hope that other new protocols may do the same and thus be trivially distinguishable from each other.
All later integers sent in the protocol are encoded as four bytes big-endian.
After the fixed headers come eight reserved bytes, which are all zero in all current implementations. If you wish to extend the protocol using these bytes, please coordinate with Bram Cohen to make sure all extensions are done compatibly.
Next comes the 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. (This is the same value which is announced as info_hash to the tracker, only here it's raw instead of quoted here). If both sides don't send the same value, they sever the connection. The one possible exception is if a downloader wants to do multiple downloads over a single port, they may wait for incoming connections to give a download hash first, and respond with the same one if it's in their list.
After the download hash comes the 20-byte peer id which is reported in tracker requests and contained in peer lists in tracker responses. If the receiving side's peer id doesn't match the one the initiating side expects, it severs the connection.
That's it for handshaking, next comes an alternating stream of length prefixes and messages. Messages of length zero are keepalives, and ignored. Keepalives are generally sent once every two minutes, but note that timeouts can be done much more quickly when data is expected.
All non-keepalive messages start with a single byte which gives their type.
The possible values are:
- 0 - choke
- 1 - unchoke
- 2 - interested
- 3 - not interested
- 4 - have
- 5 - bitfield
- 6 - request
- 7 - piece
- 8 - cancel
'choke', 'unchoke', 'interested', and 'not interested' have no payload.
'bitfield' is only ever sent as the first message. Its payload is a bitfield with each index that downloader has sent set to one and the rest set to zero. Downloaders which don't have anything yet may skip the 'bitfield' message. The first byte of the bitfield corresponds to indices 0 - 7 from high bit to low bit, respectively. The next one 8-15, etc. Spare bits at the end are set to zero.
The 'have' message's payload is a single number, the index which that downloader just completed and checked the hash of.
'request' messages contain an index, begin, and length. The last two are byte offsets. Length is generally a power of two unless it gets truncated by the end of the file. All current implementations use 2 15 , and close connections which request an amount greater than 2 17.
'cancel' messages have the same payload as request messages. They are generally only sent towards the end of a download, during what's called 'endgame mode'. When a download is almost complete, there's a tendency for the last few pieces to all be downloaded off a single hosed modem line, taking a very long time. To make sure the last few pieces come in quickly, once requests for all pieces a given downloader doesn't have yet are currently pending, it sends requests for everything to everyone it's downloading from. To keep this from becoming horribly inefficient, it sends cancels to everyone else every time a piece arrives.
'piece' messages contain an index, begin, and piece. Note that they are correlated with request messages implicitly. It's possible for an unexpected piece to arrive if choke and unchoke messages are sent in quick succession and/or transfer is going very slowly.
Downloaders generally download pieces in random order, which does a reasonably good job of keeping them from having a strict subset or superset of the pieces of any of their peers.
Choking is done for several reasons. TCP congestion control behaves very poorly when sending over many connections at once. Also, choking lets each peer use a tit-for-tat-ish algorithm to ensure that they get a consistent download rate.
The choking algorithm described below is the currently deployed one. It is very important that all new algorithms work well both in a network consisting entirely of themselves and in a network consisting mostly of this one.
There are several criteria a good choking algorithm should meet. It should cap the number of simultaneous uploads for good TCP performance. It should avoid choking and unchoking quickly, known as 'fibrillation'. It should reciprocate to peers who let it download. Finally, it should try out unused connections once in a while to find out if they might be better than the currently used ones, known as optimistic unchoking.
The currently deployed choking algorithm avoids fibrillation by only changing who's choked once every ten seconds. It does reciprocation and number of uploads capping by unchoking the four peers which it has the best download rates from and are interested. Peers which have a better upload rate but aren't interested get unchoked and if they become interested the worst uploader gets choked. If a downloader has a complete file, it uses its upload rate rather than its download rate to decide who to unchoke.
For optimistic unchoking, at any one time there is a single peer which is unchoked regardless of it's upload rate (if interested, it counts as one of the four allowed downloaders.) Which peer is optimistically unchoked rotates every 30 seconds. To give them a decent chance of getting a complete piece to upload, new connections are three times as likely to start as the current optimistic unchoke as anywhere else in the rotation.
from BitTorrent.org

















