u l int process_handshake_msg (Peer *peer,unsigned char *buff,int len)
Function: Handles a received handshake message.
Parameters: Receive this handshake message from peer; Buff point to handshake message; Len is the length of buff. The Code for the function implementation is as follows:
int process_handshake_msg (Peer *peer,unsigned char *buff,int len)
{
if (Peer==null | | buff==null) return-1;
if (memcmp (info_hash,buff+28,20)!= 0) {//Close the connection if Info_hash inconsistent
Peer->state = CLOSING;
Discard data from the send buffer
Discard_send_buffer (peer);
Clear_btcache_before_peer_close (peer);
Close (Peer->socket);
return-1;
}
Save the peer_id of the peer
memcpy (peer->id,buff+48,20);
(Peer->id) [20] = ' n ';
If you are currently in a initial state, send a handshake message to peer
if (peer->state = = INITIAL) {
Create_handshake_msg (Info_hash,peer_id,peer);
Peer->state = handshaked;
}
If the handshake message has been sent, the state is converted to a handshake state
if (peer->state = = halfshaked) peer->state = handshaked;
Record when the peer message was recently received
If no messages from the peer are received within a certain period of time (for example, two minutes), close the connection
Peer->start_timestamp = time (NULL);
return 0;
}
u l int process_keep_alive_msg (Peer *peer,unsigned char *buff,int len)
Function: Process the keepv_alive message from peer that you just received. The Code for the function implementation is as follows:
int process_keep_alive_msg (Peer *peer,unsigned char *buff,int len)
{
if (Peer==null | | buff==null) return-1;
Record when the peer message was recently received
If no messages from the peer are received within a certain period of time (such as 2min), close the connection
Peer->start_timestamp = time (NULL);
return 0;
}
u l int process_choke_msg (Peer *peer,unsigned char *buff,int len)
Function: Process the received choke message. The Code for the function implementation is as follows:
int process_choke_msg (Peer *peer,unsigned char *buff,int len)
{
if (Peer==null | | buff==null) return-1;
If you are in a unchoke state, the value of some variables in peer is updated after you receive the message
if (peer->state!=closing && peer->peer_choking==0) {
peer->peer_choking = 1;
Peer->last_down_timestamp = 0; Zero time to receive data from the peer recently
Peer->down_count = 0; Zero the number of bytes recently downloaded from the peer
peer->down_rate = 0; To peer the most recent download of data from this
}