Selaa lähdekoodia

Use multiconnectionmanager in example.

Andrew Walbran 1 vuosi sitten
vanhempi
commit
8d936c6139
2 muutettua tiedostoa jossa 18 lisäystä ja 13 poistoa
  1. 16 13
      examples/aarch64/src/main.rs
  2. 2 0
      src/device/socket/mod.rs

+ 16 - 13
examples/aarch64/src/main.rs

@@ -30,7 +30,7 @@ use virtio_drivers::{
         blk::VirtIOBlk,
         console::VirtIOConsole,
         gpu::VirtIOGpu,
-        socket::{SingleConnectionManager, VirtIOSocket, VsockAddr, VsockEventType},
+        socket::{VirtIOSocket, VsockAddr, VsockConnectionManager, VsockEventType},
     },
     transport::{
         mmio::{MmioTransport, VirtIOHeader},
@@ -204,30 +204,33 @@ fn virtio_console<T: Transport>(transport: T) {
 }
 
 fn virtio_socket<T: Transport>(transport: T) -> virtio_drivers::Result<()> {
-    let mut socket = SingleConnectionManager::new(
+    let mut socket = VsockConnectionManager::new(
         VirtIOSocket::<HalImpl, T>::new(transport).expect("Failed to create socket driver"),
     );
     let host_cid = 2;
     let port = 1221;
-    info!("Connecting to host on port {port}...");
-    socket.connect(
-        VsockAddr {
-            cid: host_cid,
-            port,
-        },
+    let host_address = VsockAddr {
+        cid: host_cid,
         port,
-    )?;
-    socket.wait_for_connect()?;
+    };
+    info!("Connecting to host on port {port}...");
+    socket.connect(host_address, port)?;
+    let event = socket.wait_for_event()?;
+    assert_eq!(event.source, host_address);
+    assert_eq!(event.destination.port, port);
+    assert_eq!(event.event_type, VsockEventType::Connected);
     info!("Connected to the host");
 
     const EXCHANGE_NUM: usize = 2;
     let messages = ["0-Ack. Hello from guest.", "1-Ack. Received again."];
     for k in 0..EXCHANGE_NUM {
         let mut buffer = [0u8; 24];
-        let socket_event = socket.wait_for_recv(&mut buffer)?;
+        let socket_event = socket.wait_for_event()?;
         let VsockEventType::Received {length, ..} = socket_event.event_type else {
             panic!("Received unexpected socket event {:?}", socket_event);
         };
+        let read_length = socket.recv(host_address, port, &mut buffer)?;
+        assert_eq!(length, read_length);
         info!(
             "Received message: {:?}({:?}), len: {:?}",
             buffer,
@@ -236,10 +239,10 @@ fn virtio_socket<T: Transport>(transport: T) -> virtio_drivers::Result<()> {
         );
 
         let message = messages[k % messages.len()];
-        socket.send(message.as_bytes())?;
+        socket.send(host_address, port, message.as_bytes())?;
         info!("Sent message: {:?}", message);
     }
-    socket.shutdown()?;
+    socket.shutdown(host_address, port)?;
     info!("Shutdown the connection");
     Ok(())
 }

+ 2 - 0
src/device/socket/mod.rs

@@ -10,6 +10,8 @@ mod singleconnectionmanager;
 mod vsock;
 
 pub use error::SocketError;
+#[cfg(feature = "alloc")]
+pub use multiconnectionmanager::VsockConnectionManager;
 pub use protocol::VsockAddr;
 #[cfg(feature = "alloc")]
 pub use singleconnectionmanager::SingleConnectionManager;