Example of popular WCF programming (3): whether to use or support iocp to complete the port (sort and supplement)

Source: Internet
Author: User

Iocp is the most complex and mature model in the windows I/O model. It is used to solve large-scale concurrent client requests. This model has been applied in many windows APIs. In the. NET Framework class library, there are also many classes using the iocp model. The most notable example is the Thread class. There are also issues with large-scale concurrent requests in WCF, but the words we hear from many official documents are often concurrency control or throttling. As a communication framework in a distributed environment, WCF does face such problems as how to handle large-scale concurrent client requests. The iocp model is so good that WCF is useless or does it support iocp in internal mechanisms.

This problem was raised by @, because I personally am not very professional with iocp, I have been checking the information to confirm this problem, and I also benefited a lot from this process. I will sort it out today and share it with you. I am also very grateful to ice black tea and friends who participated in the discussion. My basic structure is: problem, verification process, analysis, conclusion, reference. I have translated this question into an English post and put it in another one.Classic TechnologyArticle(2) Does WCF use or supports iocp? Whether or not WCF supports or uses iocp to complete the port.This article provides a detailed explanation of this issue. It is a supplement.

When I first came into contact with this issue, I took a lot of detours. Here, we will first introduce the problem to avoid getting familiar with the concept of WCF and iocp, next, I will introduce what the iocp model is and what advantages it has. By the way, I will introduce other windows I/O models. I don't know why iocp is a good choice. Next we will start with the problem of ice black tea.

[1] the original problem is:

Hello, landlord:
I have a question to ask you!
Is there iocp in the communication mechanism of WCF? Or is iocp embedded in WCF? If not, can we add iocp ourselves to respond to client requests?

1. original problem address:Http://www.cnblogs.com/frank_xl/archive/2009/06/25/1509840.html#1585252

2. English post address:Http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3f45500d-6de0-4c90-8a62-f08e1ede053e/

3. translate Chinese posts:(2) Does WCF use or supports iocp? Whether or not WCF supports or uses iocp to complete the port.

[2] Problem Analysis:

Because I have not done any development related to iocp, let's take a look at the concept of iocp, find a lot of posts, and also implement iocp.Code. There is a major misunderstanding here: iocp. The Chinese translation is the IO completion port. The first misunderstanding is that Io only refers to Io devices, such as disks. The second misunderstanding is that port refers to the network port.

I didn't know it at first, but later I realized that I/O is short for your input/output, that is, the input and output of data. It can read and write data from hardware or other software systems. It is an action of data interaction. In addition, the port may also refer to the device segment slogan, which is only a misunderstanding caused by Chinese translation.

Finally, I found a good book Windows Network Programming version 2nd. This section describes the main windows I/O models. There are five types of socket I/O models for Winsock applicationsProgramManage I/O, including select, wsaasyncselect, wsaeventselect, overlapped, and Io completion port ). Here I will make a reference. Review concepts:
[2.1] basic concepts:

(1) Select: select is the most common Io model in Wi n s o c k. This is called the "select model" because
Its central idea is to use the select function to manage Io.

(2) wsaasyncselect: Winsock provides a useful asynchronous Io model. With this model, the application can be on a socket,
Receives network event notifications Based on Windows messages.

(3) wsaeventselect: Winsock provides another useful asynchronous Io model. Similar to the wsaeventselect model, it also allows applications to receive event-based network event notifications on one or more sockets.

(4) overlapped I/O: in WinSock, I/O (overlapped I/O) overlaps)
The model enables applications to achieve better system performance. The basic design principle of the overlap model is to allow applications to use an overlapping data structure to deliver one or more Winsock I/O requests at a time. Applications can provide services for submitted requests after they are completed.

(5) iocp: in essence, the completion of the port model requires us to create a Win32 completion port object through a specified number of threads,
Manages overlapping I/O requests to provide services for completed overlapping IO requests. Before using this model, you must first create an I/O port object to manage multiple I/O Requests targeting any number of socket handles. If your server is expected to provide services for a large number of I/O requests at any given time, you should consider using I/O to complete the port model for better performance.

[2.2] verification process: Everything starts from 0. I am not familiar with the concept of iocp. At that time, I tried to use reflection to view the WCF class library, but no clue was found. After learning about the basic concept of iocp, I guess that WCF uses iocp. At that time, a post on csdn was also discussed. Some people used the post, but there was no evidence. Foreigners once posted posts in the WCF English forum, but there are also many ideas about the results. There are also some support for iocp, and there are also some objections. However, there is no strong evidence, and many posts are fruitless. When I finally gave up, a user called Rodrigo replied, prompting to reflect the code of this class:

System. servicemodel. channels. iothreadscheduler. As prompted, I re-checked the class library code and found the answer. For the discussion process, refer to the original English post.

[2.3] reflection code:

Use. Net reflector to view the WCF-related class libraries. here you need to reflect and view the code on the premise that the machine is installed with a class library above. Net 3.0. Otherwise, the reflection cannot find the class library information.

Here we can view the information of several important classes through reflectors:

 

Internal   Static   Class Iothreadscheduler
{
// Methods
[Securitycritical, securitytreatassafe]
Public   Static   Void Schedulecallback (waitcallback callback, Object State );
[Securitycritical]
Public   Static   Void Schedulecallbacklowprinoflow (waitcallback callback, Object State );

// Nested types
[Securitycritical (securitycriticalscope. Everything)]
Private   Static   Class Criticalhelper
{
// Fields
Private   Static   Object Lockobject;
Private   Static Queue < Workitem > Lowpriqueue;
Private   Static Scheduledoverlapped overlapped;
Private   Static   Bool Queuedcompletion;
Private   Static Queue < Workitem > Workqueue;

// Methods
Static Criticalhelper ();
Private   Static   Void Completioncallback ( Object State );
Private   Static   Void Processcallbacks ();
Public   Static   Void Schedulecallback (waitcallback callback, Object State, Bool Flow, Bool Lowpri );

// Nested types
Private   Class Scheduledoverlapped
{
// Fields
Private Waitcallback callback;
Private   Unsafe Nativeoverlapped * Nativeoverlapped;

// Methods
Public Scheduledoverlapped (waitcallback callback );
Private   Unsafe   Void Iocallback ( Uint Errorcode, Uint Numbytes, nativeoverlapped * Nativeoverlapped );
Public   Void Post ();
}

Private   Class Workitem
{
// Fields
Private Waitcallback callback;
Private Securitycontext context;
Private   Static Contextcallback securitycontextcallback;
Private   Object State;

// Methods
Static Workitem ();
Public Workitem (waitcallback callback, Object State, Bool Flow );
Public   Void Invoke ();
Private   Void Invoke2 ();
Private   Static   Void Onsecuritycontextcallback ( Object O );
}
}
}

 
Expand Methods
 

There are several important internal classes in iothreadscheduler:Scheduledoverlapped. It contains an important method:

Public Unsafe Scheduledoverlapped(WaitcallbackCallback) to implement the scheduling of overlapping Io threads.

1. Private Static void completioncallback (Object State );
2. Public static void schedulecallback (waitcallback callback, object state, bool flow, bool lowpri );
3. iocompletioncallback,
Another resident type: private class scheduledoverlapped. New iocompletioncallback (this. iocallback) is evidence that WCF uses the iocp model to improve large-scale concurrent clients. Like windows APIs, they also use the iocp model.

[3] Conclusion:

The discussion in the English forum is over. The answer is: the WCF framework uses the iocp model.
During this period, I also discussed with many people and found a lotTechnical materialsAnd books.
I plan to organize an article. Many people who participated in the discussion helped. Finally, use reflcetor to view the source code and find the evidence. In essence, ICOP still uses the overlapped overlapping port technology. To complete the port model, you must create a Windows Port object to manage overlapping I/O requests through a specified number of threads, to provide services for completed overlapping I/O requests .. Net Framework has encapsulated the implementation of iocp, which you can use directly.
If you want to implement it by yourself, you can completely rewrite the code of system. servicemodel. channels. iothreadscheduler.
In addition, Zhou Yong replied that Microsoft intends to use a new model to replace iocp. We also look forward to this.
Your questions are very valuable! This problem was actually raised by a foreigner in the WCF English forum, but it was not solved. At that time, many people guessed that WCF was using iocp, but no evidence was found.
I personally learned a lot about your problem, including several major windows I/O models: overlapped overlapping ports and iocp.
I believe that this question will also help those who have learned from WCF. I would like to thank you and all those who have followed and participated in this discussion ~
For those who have worked on Windows Network Programming, iocp is certainly no stranger. Examples of C # applications are also provided for the materials I searched. I will not provide specific implementation code here.

This issue is also discussed in the Post. You are welcome to comment on those who are interested and experienced in the WCF or iocp model. Communicate with everyone and learn with humility.

References:
1.Http://topic.csdn.net/u/20071106/11/adb93395-7997-4b9c-9e31-ed7927cb42f9.html(Forum discussion posts)
2.Http://www.techrss.cn/html/2008/12-07/184452.htm
3.Http://www.uudo.net/csharp/139717page10.html(C # code ).
4.Http://baike.baidu.com/view/1256215.htm(Concept of iocp)
5.Bytes)

6. Windows Network Programming

7.Network Programming for Microsoft Windows

8.Http://blog.pfan.cn/xman/44361.html

 

【Xu's blog]

[Author ]:Frank Xu lei

[Address ]:Http://www.cnblogs.com/frank_xl/archive/2009/07/18/1525201.html

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.