| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | | Supported Targets | ESP32 | | 
					
						
							|  |  |  | | ----------------- | ----- | | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Async request using ASIO
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (See the README.md file in the upper level 'examples' directory for more information about examples.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The application aims to show how to compose async operations using ASIO to build network protocols and operations. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Configure and Building example
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | This example doesn't require any configuration, just build it with | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | idf.py build | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Async operations composition and automatic lifetime control
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | On this example we compose the operation by starting the next step in the chain inside the completion handler of the | 
					
						
							|  |  |  | previous operation. Also we pass the `Connection` class itself as the parameter of its final handler to be owned by | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | the following operation. This is possible due to the control of lifetime by the usage of `std::shared_ptr`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | The control of lifetime of the class, done by `std::shared_ptr` usage, guarantee that the data will be available for | 
					
						
							|  |  |  | async operations until it's not needed any more. This makes necessary that all of the async operation class must start | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | its lifetime as a `std::shared_ptr` due to the usage of `std::enable_shared_from_this`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 09:24:38 +01:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |      User creates a shared_ptr──┐ | 
					
						
							|  |  |  |      of AddressResolution and   │ | 
					
						
							|  |  |  |      ask for resolve.           │ | 
					
						
							|  |  |  |      The handler for the       ┌▼─────────────────────┐ | 
					
						
							|  |  |  |      complete operation is sent│   AddressResolution  │  In the completion of resolve a connection is created. | 
					
						
							|  |  |  |                                └─────────────────┬────┘  AddressResolution is automaticly destroyed since it's | 
					
						
							|  |  |  |                                                  │       no longer needed | 
					
						
							|  |  |  |                                                ┌─▼────────────────────────────────────┐ | 
					
						
							|  |  |  |                                                │         Connection                   │ | 
					
						
							|  |  |  |                                                └──────┬───────────────────────────────┘ | 
					
						
							|  |  |  |   Http::Session is created once we have a Connection. │ | 
					
						
							|  |  |  |   Connection is passed to Http::Session that holds it │ | 
					
						
							|  |  |  |   avoiding it's destruction.                          │ | 
					
						
							|  |  |  |                                                     ┌─▼───────────────────────────────┐ | 
					
						
							|  |  |  |                                                     │       Http::Session             │ | 
					
						
							|  |  |  |                                                     └────────┬────────────────────────┘ | 
					
						
							|  |  |  |                                 After the HTTP request is    │ | 
					
						
							|  |  |  |                                 sent the completion handler  │ | 
					
						
							|  |  |  |                                 is called.                   │ | 
					
						
							|  |  |  |                                                              └────►Completion Handler() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 09:24:38 +01:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 16:31:57 +02:00
										 |  |  | The previous diagram shows the process and the life span of each of the tasks in this examples. At each stage the | 
					
						
							| 
									
										
										
										
											2021-09-20 15:25:02 -03:00
										 |  |  | object responsible for the last action inject itself to the completion handler of the next stage for reuse. |