Ver Fonte

Add constructor for Connection.

Andrew Walbran há 1 ano atrás
pai
commit
e6cf84e2c8
1 ficheiros alterados com 15 adições e 8 exclusões
  1. 15 8
      src/device/socket/multiconnectionmanager.rs

+ 15 - 8
src/device/socket/multiconnectionmanager.rs

@@ -51,6 +51,17 @@ struct Connection {
     buffer: RingBuffer,
 }
 
+impl Connection {
+    fn new(peer: VsockAddr, local_port: u32) -> Self {
+        let mut info = ConnectionInfo::new(peer, local_port);
+        info.buf_alloc = PER_CONNECTION_BUFFER_CAPACITY.try_into().unwrap();
+        Self {
+            info,
+            buffer: RingBuffer::new(PER_CONNECTION_BUFFER_CAPACITY),
+        }
+    }
+}
+
 impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
     /// Construct a new connection manager wrapping the given low-level VirtIO socket driver.
     pub fn new(driver: VirtIOSocket<H, T>) -> Self {
@@ -77,15 +88,11 @@ impl<H: Hal, T: Transport> VsockConnectionManager<H, T> {
             return Err(SocketError::ConnectionExists.into());
         }
 
-        let mut new_connection_info = ConnectionInfo::new(destination, src_port);
-        new_connection_info.buf_alloc = PER_CONNECTION_BUFFER_CAPACITY.try_into().unwrap();
+        let new_connection = Connection::new(destination, src_port);
 
-        self.driver.connect(&new_connection_info)?;
-        debug!("Connection requested: {:?}", new_connection_info);
-        self.connections.push(Connection {
-            info: new_connection_info,
-            buffer: RingBuffer::new(PER_CONNECTION_BUFFER_CAPACITY),
-        });
+        self.driver.connect(&new_connection.info)?;
+        debug!("Connection requested: {:?}", new_connection.info);
+        self.connections.push(new_connection);
         Ok(())
     }