BT client implements peer protocol design

Source: Internet
Author: User
Tags socket blocking

After establishing a TCP connection with peer, first send a handshake message for handshake

The handshake message format is as follows:

One byte 0x19 + one string ' BitTorrent protocol ' + 8 byte reserved byte default value is 0 (reserved bytes are defined in draft)

+ SHA1 Word in the Info section of the seed file, size 20 bytes + 20 own Peer ID (most of the peer information obtained from tracker is not peerid, this can use the local peer ID)

If the handshake information is not negotiated, the TCP connection will be closed.

BT Standard BEP-3 defines 8 peer messages: The message format is Msg_len (4 bytes) + msg_type (11 bytes) + payload

0-choke--sends this message to indicate that this segment is blocked, the peer will not get to the piece data, payload is 0

1-unchoke--Sends the message to indicate that the solution is blocked, the peer can start sending a request to get piece data, payload 0

2-interested--sends this message to show interest in pieces data on the peer, payload to 0

3-not Interested---Send this message, indicating no interest in pieces data on the end, payload 0

4-have---Send this message, advertise that the pieces,payload of the end of this segment is 4 bytes of piece index

5-bitfield---Send this message to advertise the pieces that is owned by this segment, indicating whether each piece index is owned on the local side as a bit map. Piece index has a bit bit of 1, indicating ownership.

The message is the first message after the handshake succeeds.

6-request---Piece request message, payload is: index, begin, and length, are 4 bytes, length is generally implemented as 0x8000, the maximum can not exceed 0x20000.

7-piece---piece data, payload is: index, Begin,data

8-cancel---Send this message, indicating that a piece request was canceled by this side. Payload: index, begin, and length

Using Python's asynchronous socket interface implementation, in order to reduce processing by socket blocking, using multiple threads to process each peer.

Each peer consists of 3 threads: Request Timeout timer, socket send data thread, socket receive data thread, use the Select function to determine if the socket is readable and writable.

When using Rlock for socket read and write operations, select blocks the process without locking and prevents other threads from blocking.

When sending data data, write a queue and then set threading. The Event variable starts with the socket send data thread, which guarantees that the thread sending the data does not block

Since Python does not have an end thread interface, socket send data thread, the socket receive data thread needs to rely on the assignment of special variables to end the socket processing process.

Use synchronous tune to trigger the download process and try not to use the timer poll to reduce CPU usage and speed up the download process.

However, synchronous calls between multiple threads due to the presence of locks can lead to degraded performance and easy introduction of semaphore deadlocks. You need to carefully design a multi-threaded running trajectory to avoid deadlocks.

The functions defined in draft BEP are not implemented, peer upload flow control is not implemented, peer quality rating is not implemented.

BT client implements peer protocol design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.