Esframework introduction (16)-TCP Automatic Data Transmitter itcpautosender

Source: Internet
Author: User

We have accumulated this experience: if a large piece of data needs to be sent over TCP, we will adopt an Asynchronous Method to avoid blocking of the current Working thread. So, if we have multiple threads that need to send large pieces of data at the same time? Each thread submits asynchronous data sending requests on networkstream or socket, which can lead to confusion in data sending (multiple threads simultaneously perform write operations on one socket). Therefore, I introduced the thread-safe network stream introduced earlier. After this class is introduced, it seems that it has been a good time, but the new functional requirements make me need to find another solution.
Consider this scenario: Our instant messaging software agileim also transmits text information, audio data, important file data, and control commands while chatting with friends in a video. The information to be transmitted has a priority order. For example, the control command has the highest priority and the file data has a lower priority, however, video and audio data can be discarded when the network is busy. To manage these priorities, isafenetworkstream alone cannot meet our requirements. We need another component to solve this problem. The itcpautosender component provided by esframework is for this purpose.

We know that in a multi-threaded environment, thread security must be ensured for access to critical resources such as socket. from another perspective, we can say that, access to a critical resource must be synchronized or "like a single thread. Itcpautosender simplifies the problem by taking advantage of this "Imitation single thread" feature. Let's see how this component works?
Our applicationsProgramWhen the data in each thread needs to be sent, it submits the data to itcpautosender. The priority of the data must be specified during the submission. The itcpautosender component puts the data into the corresponding Queue Based on the specified priority. Note that the data to be sent to the itcpautosender must be thread-safe, which has been guaranteed by the itcpautosender component, users do not need to care.

The itcpautosender contains a circular thread that selects a packet from the high-priority queue for sending each time. When the high-priority queue is empty, it then sends data from the second high-priority queue. Each time a packet is sent, it is checked from the queue with a high priority.
As can be seen from the above description, the itcpautosender component only sends data in a thread, so the itcpautosender component does not need to use the isafenetworkstream component, but can directly use networkstream. So does esframework not need to provide isafenetworkstream components? No. In the esframework, The isafenetworkstream component is mainly used by the server. Generally, the probability that the server actively sends data to the client is relatively small, and the server needs to manage thousands of connections, therefore, it is very suitable to use the isafenetworkstream component. The itcpautosender component is most commonly used on the client, providing a priority Mechanism for the data sent by the client application. If the priority of all data is the same for the client in your application, there is no need to use the itcpautosender component. Simply use isafenetworkstream.
 
In the esframework, the data priority is divided into four levels, as shown in the following enumeration definition:

1 Public   Enum Datemediority
2 {
3 High, // Emergency Command
4 Common, // Such as common messages, such as chat messages
5 Low, // For example, File Transfer
6 Canbediscarded // Such as video data and audio data
7 }

For each priority, the implementation of the itcpautosender component has a corresponding queue, and each queue has an initial size. For the first three priority queues, when they are full, the threads in the application must wait until the data is submitted. For canbediscarded Queues with the lowest priority, if data is submitted at full, the waiting data in the queue header will be deleted. The following describes the Interface Definition of the itcpautosender component:

1 Public   Interface Itcpautosender: idisposable
2 {
3 Void Initialize ();
4 Void Senddata ( Byte [] Data, datemediority );
5 Void Clearqueue (datemediority queuetype );
6 Event Cbdatadiscarded datadiscarded;
7 Event Cbdatalacked datalacked;
8 Event Cbsimple connectioninterrupted;
9
10 Int Queuesizeofdiscarded { Get ; Set ;}
11 Int Queuesizeofnondiscarded { Get ; Set ;}
12 Networkstream { Set ;}
13 }
14
15 Public   Delegate   Void Cbdatadiscarded ( Byte [] Data );
16 Public   Delegate   Void Cbdatalacked ();

Note: The datadiscarded event and datalacked event are released in this interface. when data is discarded during busy network hours, the datadiscarded event is triggered. When all the queues to be sent are empty, the datalacked event is triggered. Our application can schedule these two events as appropriate. For example, in agileim, when a datadiscarded event occurs, the frame rate captured by the video is reduced as appropriate; When a datalacked event occurs, the frame rate captured by the video is increased as appropriate, to achieve better video session performance.

Finally, a lot of friends want to get esframeworkSource codeThis will not be a long time before esframework is open-source. Here, I will provide esframework. DLL for you to download and apply it. Of course, you can also use the reflection tool to see the structure. If you want to use esframework in your project, I will be very happy to answer your various questions and you can contact me via AgileSoft@163.com.
Thank you for your attention!

PreviousArticle: Esframework introduction (15) -- IRAs

Go to: esframework reusable Communication Framework (sequence)

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.