In this example, the communication channel between the server and a client is a connection, encapsulated as a class cconnect. It is created after the server receives a client connection request. It is mainly used to manage the connection lifecycle and receive and send data. In terms of the life cycle, when a connection is created, then when will it die? The design of this class is very good and clever, because it does not exist for a long time, nor will it die in advance, leading to illegal memory access. The main benefit of this feature is its inheritance class enable_shared_from_this <cconnect>. The magic of this class enable_shared_from_this is to use shared_ptr in class internal functions to maintain class instances without losing control, leading to early death. Maybe you will say that using shared_ptr <cconnect> (this) in the class for forced conversion, isn't it a smart pointer? However, smart pointers such as shared_ptr cannot be forcibly converted because forced conversions do not increase the life length of class objects. You may say that there are other methods to add a shared_ptr <cconnect> member variable to the class so that it can be saved and the lifecycle can be prolonged. However, although this scheme can extend the object's lifecycle, it also causes the object to never be deleted. At this time, you should also think of a major defect in the shared_ptr smart pointer that you have learned before, that is, it cannot be referenced cyclically. Therefore, we need to use weak_ptr smart pointer as a member variable. This is the solution implemented by enable_shared_from_this, which is why we need to inherit enable_shared_from_this. In this way, smart pointers can be referenced inside the class, but there is also a restriction that shared_from_this () cannot be used in the class constructor (), it is too early to create shared_ptr In the constructor.
With this magic class, the lifecycle of an object can be converted from one local scope to another. For example, the handleaccept function is called when the server receives a connection, after calling the start function of cconnect, The shared_from_this () function is called in this function, so that the cconnect object will be added to its life when the local scope is about to end, therefore, the cconnect object can decide whether to continue to survive each time it receives or sends data. It is implemented by calling the shared_from_this () function. In this way, when a connection error occurs, the shared_from_this () function is no longer called. As a result, this object will be automatically deleted when the lifetime of the object is reached to release the asset.
In this example, we will not only learn how the network server receives client connections, but also learn how to maintain the existence of a shared smart pointer in the class member function, this not only makes the management memory simpler, but also makes the resource utilization reach the extreme, which is completely allocated on demand. It is not too early or too late.