【★Update★] High-performance windows socket server and client components (HP-socket v2.0.1 source code and test case download)

Source: Internet
Author: User
Tags set socket windows x64
HP-socket


In the past, a general-purpose Windows Socket TCP underlying communication component was developed for a large-scale communication project. The component code is HP-socket. NowCodeOpen it to the public, hoping to help everyone. In addition, in order to make it easier for everyone to learn HP-socket, we have carefully produced a function test example (test echo) with a test echo-PFM, you can use these two test examples to quickly master the design ideas and usage of components. HP-socket includes the server component (iocp model) and the client component (event select model). It has the following important features:



Versatility


    • The only responsibility of a communication component is to accept and send byte streams. It cannot participate in upper-layer protocol parsing;
    • Decoupled from upper-layer users and independent from each other, components interact with users through the operation interface and listener interface, and the component implementation operation interface provides the upper-layer operation method; the user registers himself as the listener of the component through the listener interface to receive notifications from the component. Therefore, any user can use components as long as the listener interface is implemented. On the other hand, you can even re-write a component implementation method that is completely different for the user to call, as long as the component complies with the operation interface of the component, this is also the embodiment of the dip design principles.


Availability



Availability is crucial to all common components. If it is too difficult to use, it is easier to rewrite it. Therefore, the Operation interfaces and listener interfaces of components are designed to be as simple as possible (in general, they are "dumbfounded"). There are no more than five main methods for these two interfaces. In addition, the component completely encapsulates all the underlying socket communication, and the upper-layer applications do not see any communication details and do not have to intervene in any communication operations. The socket connection is abstracted as the connection ID, this parameter is used as a connection identifier for upper-layer applications to identify different connections.



High Performance



As a general component at the underlying layer, performance issues must be considered and cannot be a bottleneck of the system. On the other hand, different Socket models are used according to the performance requirements of client components and server components. Components fully consider factors such as performance, actual use cases, availability, and implementation complexity to ensure that the performance requirements are not too complex. Make the following two design decisions:


    • Client: Implement socket communication interaction in a separate thread. This avoids interference with the main thread or other threads. Select the event select Communication Model for the I/O model.
    • Server: The iocp communication model with the highest efficiency on the Windows platform is adopted. The cache pool technology usually requires frequent application and release of the memory buffer during communication and a dynamic cache pool is established, A new object is created only when there are no available objects in the cache pool, and when there are too many cached objects, the cache pool is compressed. In addition, the dynamic memory of the component is allocated through the private heap mechanism, avoid competition with new/malloc and reduce memory holes.


Scalability



You can set the performance parameters of the component according to the actual usage environment requirements (for example: the number of working threads, the size of various cache pools, the size of the sending and receiving buffer, the size of the socket listening queue, the number of Accep distributions, and the interval of Heartbeat checks ).

HP-socket source code, examples, and documentation resources:Please visit the project homepage ^ _*
 




 V2.0.1 update: ========================================================== =====   > Server:  -----------------
1. The onpreparesocket (connid, socket) notification method added by iserversocketlistener is used to set socket options or filter client connections before using socket.
2. The method for adding isocketserver disconnect (connid) is used to actively disconnect the client.
3. added the cserversocketlistener subclass of iserversocketlistener to provide the default (null) Notification processing method.  > Client:  -----------------
1. The onpreparesocket (connid, socket) notification method added to iclientsocketlistener is used to set socket options before using socket.
2. Support for asynchronous CONNECT: Add a parameter (bool basyncconnect) to the START () method of isocketserver to set whether asynchronous connect is used
3. Added cclientsocketlistener, a subclass of iclientsocketlistener, to provide the default (null) Notification processing method.
4. bug fixing: packet loss occurs under ultra-high loads  > Other updates:  -----------------
1. Supports windows x64 Platform
2. Optimize testecho and testecho-PFM tests Program 3. Add "Asynchronous connection" to the testecho client. 4. Add "connection filtering" and "active disconnection" to the testecho server.


 Original article: high-performance windows socket server and client components (download of source code and Test Cases)"



Since I published two articles on the implementation of the Windows Socket communication componentArticleAfter receiving comments from many readers, I hope to share the completeSource code. At this time, this seat does not harm you. The complete code of the server component and client component is provided here. In addition, it took a little time to carefully prepare two test cases for readers to learn and understand, one for functional testing (testecho) and the other for performance testing (testecho-PFM ). Readers can use these two test cases to quickly learn how to use components. Thank you for your help ~ Pai_^ ~



(Click here, You know ^ _*)



 



 



Original article: Design and Implementation of iocp-based general asynchronous Windows Socket TCP high-performance server component"



 



Design Overview



The Design of server-side communication components is a very rigorous task. Performance, scalability, and stability are hard and hard quality indicators that must be considered, to design a component as a general component for a variety of known or unknown upper-layer applications, the design becomes more difficult and the versatility, availability, and flexibility must be taken into account.



Taking a general asynchronous Windows Socket TCP Server Component Based on iocp as an example, this article describes its design and implementation problems, hoping to stimulate everyone's thinking, it will be helpful for you to carry out similar work in the future. The topic about universality, availability, socket model selection, and interface model design has been published in 《Design and Implementation of Common asynchronous Windows Socket tcp client componentsAs described in, we will not repeat it here. At present, the design and implementation problems of the communication components on the server are described.



 



I. Thread Structure



There are three types of threads related to components: User thread, accept thread, and worker thread. The latter two are implemented by components.


      1. User thread:Call the start, stop, send, and other component methods to operate on one or more threads of the component, usually the main thread of the program or other business logic threads.
      2. Accept thread:Use acceptex () to receive client connection requests and create a client socket thread. Separate the thread into a separate thread to make the module division of the component clearer, it is more important to avoid interaction with business logic and communication processing.
      3. Worker thread:Use getqueuedcompletionstatus () to listen to network events and process multiple threads for network interaction. After the worker thread finishes processing network events, it will send onaccept, onsend, onreceive, and other component notifications to the application at the upper layer. The number of worker threads can be set based on the actual situation (generally recommended: CPU core number * 2 + 2 ).


Note:If the upper-layer application directly processes the business logic and operates the components when receiving notifications from the onaccept/onsend/onreceive components, the working thread becomes the user thread. In addition, if the business logic to be processed is time-consuming, the upper-layer application should submit it to other threads for processing after receiving the component notification.



 



Ii. Performance



Components use the iocp socket communication model with the highest efficiency on the Windows platform. Therefore, the performance of the communication interfaces is guaranteed. I will not talk about it here. Performance optimization is explained from the perspective of component design and implementation. Components have been optimized a lot at the code level, and some seemingly redundant or tedious code is actually for performance services; components mainly adopt the 2 optimization strategy in design: cache pool and private heap.


      1. Cache pool:In the communication process, it is usually necessary to frequently apply for and release the memory buffer (tbufferobj) and socket-related struct (tsocketobj), which will greatly affect the component performance. Therefore, the components are tbufferobj and tsocketobj. A dynamic cache pool is created only when there are no available objects in the cache pool. When there are too many cached objects, the cache pool is compressed.
      2. Private heap ):In the operating system, new/malloc and other operations are serialized, although general applications do not need to care too much about this problem, however, a high-concurrency Server is a problem that cannot be ignored. In addition, both tbufferobj and tsocketobj are fixed-size structs. Therefore, it is very suitable for allocating memory in a private heap, avoid competition with new/malloc and reduce memory holes. (For details about how to use a private stack, refer to pai_^)





Iii. versatility and availability



And 《Design and Implementation of Common asynchronous Windows Socket tcp client componentsSimilar to the client interface described in, the server component also provides two sets of interfaces: The isocketserver interface provides component operation methods, which are directly called by upper-layer applications; the iserversocketlistener interface provides component notification methods, implemented by upper-layer applications, these two interfaces are designed very simple, with no more than five main methods. Because the components are fully functional (no additional libraries or code is required) and have a single responsibility (only communication is required and not involved in the business logic ), therefore, it is very convenient to integrate into any type of applications.



 



Iv. scalability



You can set the number of working threads, the size of the tbufferobj and tsocketobj cache pools, the size of the tbufferobj buffer, the size of the socket listening queue, the number of accepex distributions, and the heartbeat check according to actual environment requirements. interval.



 



5. Connection ID



The component completely encapsulates all the underlying socket communication, and the upper-layer applications do not see any communication details and cannot intervene in any communication operations. In addition, the component has a connection ID parameter in all methods of the iserversocketlistener notification interface. This parameter is used as the connection identifier for upper-layer applications to identify different connections.



 



 



Original article: design and implementation of common asynchronous Windows Socket tcp client components"



 



Design Overview



Writing a Windows Socket tcp client is not difficult. Windows provides six I/O communication models. However, I have seen many client programs mix the socket communication and business logic, and the logic is still messy. Every program can copy/parse similar code and then modify it. Therefore, this book uses some leisure time to writeIocp-based general asynchronous Windows Socket TCP high-performance server componentAnd oneGeneral asynchronous Windows Socket tcp client componentWe hope that you will be inspired by our reference. This article describes the client components. Let's talk less about it. Now we are on the right corner.


    • The most important first question: how can we achieve general purpose?


A: It's easy.



1. Restrict the functions of components. To put it bluntly,The only responsibility of the communication component is to accept and send byte streams.And cannot participate in upper-layer protocol parsing. This is what it means.



2. decoupled from upper-layer users and independent from each other. components interact with users through interface methods, and components implement the isocketclient interface to provide upper-layer operation methods; the user registers himself as the listener of the component through the iclientsocketlistener interface and receives the component notification. Therefore, any user can use components as long as the iclientsocketlistener interface is implemented. On the other hand, you can even re-write a component implementation method that is completely different to the user to call, as long as the component complies with the isocketclient interface. This is also the embodiment of DIP design principles (if you want to learn more about the design principles, please slam here ^_^ ).



 


    • The second most important question: how is availability, that is, is it convenient to use?


A: This is a good question. availability is crucial to all common components. If it is too difficult to use, it is better to write it again. Therefore, the interfaces of isocketclient and iclientsocketlistener are designed to be as simple as possible (in general, they are "dumbfounded"). There are no more than five main methods for these two interfaces.



 


    • The third most important question: how is the component performance?


As a general component at the underlying layer, performance issues must be considered and cannot be a bottleneck of the system. On the other hand, starting from the reality, after all, it is only a client component, and its concurrency requirements are far less high than that of the server. Therefore, the components fully consider factors such as performance, actual use cases, availability, and implementation complexity to ensure that the performance requirements are not too complex. Make the following two design decisions:


      1. Implement socket communication interaction in a separate thread. This avoids interference with the main thread or other threads.
      2. Select wsaeventselect for the I/O model. The reason for choosing this I/O model: (for the performance comparison of various I/O models, refer to Windows Network Programming (Chinese version 2), pp. 154th)
    • Blocking model: (do not parse, you know ^_^)
    • Non-blocking model: (performance is too low)
    • Wsaasyncselect: (two reasons: a. The performance is too low; B. It is really difficult to bear hwnd for pure console programs !)
    • Overlapping I/O: (a bit complicated)
    • Port completed: (Why ?)


 



Codeproject


Related Article

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.