@@ -36,7 +36,7 @@ use std::{env, sync::Arc, thread, time::*};
36
36
37
37
use anyhow:: { bail, Result } ;
38
38
39
- use async_io:: Async ;
39
+ use async_io:: { Async , Timer } ;
40
40
use esp_idf_svc:: http:: server:: { EspHttpConnection , Request } ;
41
41
use esp_idf_svc:: io:: Write ;
42
42
use log:: * ;
@@ -312,6 +312,18 @@ fn main() -> Result<()> {
312
312
313
313
let _timer = test_timer ( eventloop, mqtt_client) ?;
314
314
315
+ #[ allow( clippy:: needless_update) ]
316
+ {
317
+ esp_idf_svc:: sys:: esp!( unsafe {
318
+ esp_idf_svc:: sys:: esp_vfs_eventfd_register(
319
+ & esp_idf_svc:: sys:: esp_vfs_eventfd_config_t {
320
+ max_fds: 5 ,
321
+ ..Default :: default ( )
322
+ } ,
323
+ )
324
+ } ) ?;
325
+ }
326
+
315
327
#[ cfg( not( esp_idf_version = "4.3" ) ) ]
316
328
test_tcp_bind_async ( ) ?;
317
329
@@ -730,18 +742,6 @@ fn test_tcp_bind_async() -> anyhow::Result<()> {
730
742
731
743
info ! ( "About to bind a simple echo service to port 8081 using async (with async-io)!" ) ;
732
744
733
- #[ allow( clippy:: needless_update) ]
734
- {
735
- esp_idf_svc:: sys:: esp!( unsafe {
736
- esp_idf_svc:: sys:: esp_vfs_eventfd_register(
737
- & esp_idf_svc:: sys:: esp_vfs_eventfd_config_t {
738
- max_fds: 5 ,
739
- ..Default :: default ( )
740
- } ,
741
- )
742
- } ) ?;
743
- }
744
-
745
745
thread:: Builder :: new ( ) . stack_size ( 20000 ) . spawn ( move || {
746
746
let executor = LocalExecutor :: new ( ) ;
747
747
@@ -755,6 +755,78 @@ fn test_tcp_bind_async() -> anyhow::Result<()> {
755
755
756
756
fn test_https_client ( ) -> anyhow:: Result < ( ) > {
757
757
async fn test ( ) -> anyhow:: Result < ( ) > {
758
+ // Implement `esp_idf_svc::tls::PollableSocket` for async-io sockets
759
+ ////////////////////////////////////////////////////////////////////
760
+
761
+ pub struct EspTlsSocket ( Option < async_io:: Async < TcpStream > > ) ;
762
+
763
+ impl EspTlsSocket {
764
+ pub const fn new ( socket : async_io:: Async < TcpStream > ) -> Self {
765
+ Self ( Some ( socket) )
766
+ }
767
+
768
+ pub fn handle ( & self ) -> i32 {
769
+ self . 0 . as_ref ( ) . unwrap ( ) . as_raw_fd ( )
770
+ }
771
+
772
+ pub fn poll_readable (
773
+ & self ,
774
+ ctx : & mut core:: task:: Context ,
775
+ ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
776
+ self . 0
777
+ . as_ref ( )
778
+ . unwrap ( )
779
+ . poll_readable ( ctx)
780
+ . map_err ( |_| EspError :: from_infallible :: < { esp_idf_svc:: sys:: ESP_FAIL } > ( ) )
781
+ }
782
+
783
+ pub fn poll_writeable (
784
+ & self ,
785
+ ctx : & mut core:: task:: Context ,
786
+ ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
787
+ self . 0
788
+ . as_ref ( )
789
+ . unwrap ( )
790
+ . poll_writable ( ctx)
791
+ . map_err ( |_| EspError :: from_infallible :: < { esp_idf_svc:: sys:: ESP_FAIL } > ( ) )
792
+ }
793
+
794
+ fn release ( & mut self ) -> Result < ( ) , esp_idf_svc:: sys:: EspError > {
795
+ let socket = self . 0 . take ( ) . unwrap ( ) ;
796
+ socket. into_inner ( ) . unwrap ( ) . into_raw_fd ( ) ;
797
+
798
+ Ok ( ( ) )
799
+ }
800
+ }
801
+
802
+ impl esp_idf_svc:: tls:: Socket for EspTlsSocket {
803
+ fn handle ( & self ) -> i32 {
804
+ EspTlsSocket :: handle ( self )
805
+ }
806
+
807
+ fn release ( & mut self ) -> Result < ( ) , esp_idf_svc:: sys:: EspError > {
808
+ EspTlsSocket :: release ( self )
809
+ }
810
+ }
811
+
812
+ impl esp_idf_svc:: tls:: PollableSocket for EspTlsSocket {
813
+ fn poll_readable (
814
+ & self ,
815
+ ctx : & mut core:: task:: Context ,
816
+ ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
817
+ EspTlsSocket :: poll_readable ( self , ctx)
818
+ }
819
+
820
+ fn poll_writable (
821
+ & self ,
822
+ ctx : & mut core:: task:: Context ,
823
+ ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
824
+ EspTlsSocket :: poll_writeable ( self , ctx)
825
+ }
826
+ }
827
+
828
+ ////////////////////////////////////////////////////////////////////
829
+
758
830
let addr = "google.com:443" . to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ;
759
831
let socket = Async :: < TcpStream > :: connect ( addr) . await ?;
760
832
@@ -780,7 +852,7 @@ fn test_https_client() -> anyhow::Result<()> {
780
852
}
781
853
782
854
let th = thread:: Builder :: new ( )
783
- . stack_size ( 15000 )
855
+ . stack_size ( 20000 )
784
856
. spawn ( move || async_io:: block_on ( test ( ) ) ) ?;
785
857
786
858
th. join ( ) . unwrap ( )
@@ -1473,70 +1545,3 @@ fn waveshare_epd_hello_world(
1473
1545
1474
1546
Ok ( ( ) )
1475
1547
}
1476
-
1477
- pub struct EspTlsSocket ( Option < async_io:: Async < TcpStream > > ) ;
1478
-
1479
- impl EspTlsSocket {
1480
- pub const fn new ( socket : async_io:: Async < TcpStream > ) -> Self {
1481
- Self ( Some ( socket) )
1482
- }
1483
-
1484
- pub fn handle ( & self ) -> i32 {
1485
- self . 0 . as_ref ( ) . unwrap ( ) . as_raw_fd ( )
1486
- }
1487
-
1488
- pub fn poll_readable (
1489
- & self ,
1490
- ctx : & mut core:: task:: Context ,
1491
- ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
1492
- self . 0
1493
- . as_ref ( )
1494
- . unwrap ( )
1495
- . poll_readable ( ctx)
1496
- . map_err ( |_| EspError :: from_infallible :: < { esp_idf_svc:: sys:: ESP_FAIL } > ( ) )
1497
- }
1498
-
1499
- pub fn poll_writeable (
1500
- & self ,
1501
- ctx : & mut core:: task:: Context ,
1502
- ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
1503
- self . 0
1504
- . as_ref ( )
1505
- . unwrap ( )
1506
- . poll_writable ( ctx)
1507
- . map_err ( |_| EspError :: from_infallible :: < { esp_idf_svc:: sys:: ESP_FAIL } > ( ) )
1508
- }
1509
-
1510
- fn release ( & mut self ) -> Result < ( ) , esp_idf_svc:: sys:: EspError > {
1511
- let socket = self . 0 . take ( ) . unwrap ( ) ;
1512
- socket. into_inner ( ) . unwrap ( ) . into_raw_fd ( ) ;
1513
-
1514
- Ok ( ( ) )
1515
- }
1516
- }
1517
-
1518
- impl esp_idf_svc:: tls:: Socket for EspTlsSocket {
1519
- fn handle ( & self ) -> i32 {
1520
- EspTlsSocket :: handle ( self )
1521
- }
1522
-
1523
- fn release ( & mut self ) -> Result < ( ) , esp_idf_svc:: sys:: EspError > {
1524
- EspTlsSocket :: release ( self )
1525
- }
1526
- }
1527
-
1528
- impl esp_idf_svc:: tls:: PollableSocket for EspTlsSocket {
1529
- fn poll_readable (
1530
- & self ,
1531
- ctx : & mut core:: task:: Context ,
1532
- ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
1533
- EspTlsSocket :: poll_readable ( self , ctx)
1534
- }
1535
-
1536
- fn poll_writable (
1537
- & self ,
1538
- ctx : & mut core:: task:: Context ,
1539
- ) -> core:: task:: Poll < Result < ( ) , esp_idf_svc:: sys:: EspError > > {
1540
- EspTlsSocket :: poll_writeable ( self , ctx)
1541
- }
1542
- }
0 commit comments