Single-thread multi-point download socket model considerations (-)

xiaoxiao2021-03-06  14

HTTP download is the most important way to download in the network, and download the number of tools, like flashget, network ant, Thunder, etc., the most important advantages of these tools are capable of renewing multiple breakpoints, and we are the most important The model of multi-point download model is explained in the HTTP download protocol.

Under the asynchronous socket model (CasyncSocket derived), we build a single-thread multi-point download model, considering the most popular like the most popular like Flashget (multi-thread multi-point download), the most important advantage of its Socket model, each Socket dynamically assigned task sizes, which can be dynamically increased and reducing the number of sockets, and other Socket can complete their unfinished tasks after a Socket is hung. . After half a day, my own expression is very poor, or directly with the numbers, huh, huh.

First, our Socket model must do the following basic few:

1 . Complete your own task.

2. After completing your own tasks, you will assist other sockets to complete the remaining tasks.

3. Guaranteed your stability and retry mechanism.

4. When the network instability causes a Socket to hang (DEAD, NEVER CAN BE RETRY AGAIN), other sockets should be able to detect and help them complete the remaining tasks.

With the above, I consider this Socket model, I have gone a lot of bending path, the previous idea is the task of static allocation socket (the disadvantage is obvious, if a socket is hung, this Socket task has no way to complete)

Here is a better model: use arrays. When we send HTTP Header to get the server's responsed, analyze the size of the file to download, establish the following arrays:

M_NSocketTaskArray [[Max_Socket] records the starting point of each socket

M_NSocketCompletedArray [max_socket], record the number of completions for each socket

M_BsocketrunningArray [max_socket], record a Socket is running

M_BsocketFinishedAdArray [max_socket], logging if a socket has been completed.

My idea is: Initial, initialize all arrays, like flashget, start, we only take a socket to get file size NTOTALSIZE, (we only consider there is no error), when this Socket gets the file size, Other Socket starts working. Each Socket task is to assign: get the start position of each socket by NTOTALSIZE / NTOTALBLOCK * i, and the end position is NTOTALSIZE, in which we prevent the server to limit the thread, but there is a problem : How to determine the actual task of each socket. At this time, the array above is sent, and we have a principle to the array operation: when a socket gets the correct http responsed, the m_nsockettaskaRray array is inserted, and the M_NSocketTaskArray array is made by small to large.

Other arrays are positioned by m_nsockettaskaarray, such as M_NSocketCompletedArray [i] records is the number of complements starting with M_NSockettaskArray [i]. At this time, we update each Socket task whenever inserted a socket task. If the task of Socket [i] is starting with M_NSockettaskArray [i], M_NSocketTaskArray [i 1] -1 is end. If you are divided into 10 files download length is 1000: m_nsockettaskarray 0 100 200 300 400 1000 0 0

M_NsocketCompletedArray 51 41 21 100 200 0 0 0

M_Bsocketrunning 1 0 1 0 1 0 0 0

M_BsocketCompleted 0 0 0 0 0 0 0 0

The m_nsockettaskArray only records the beginning of the Socket after response, Socket6, 7, 8, 9 may start unsuccessful because of the limitations of the server, so in the array, our arrangement guarantees that each piece has socket to complete, The server restriction thread is excluded. As for the Socket from 400 to 1000 tasks too heavy, we will consider the intelligent mechanism of Socket, such as the Socket from 0 to 100 to complete the task, should help the most heavy task, this Let's talk about it next time.

The ability to express is not very good, more see

转载请注明原文地址:https://www.9cbs.com/read-48037.html

New Post(0)