mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2024-11-25 05:03:05 +00:00
vmm_vhost: inline Endpoint::recv_data
Convenience methods like this aren't worth the mental overhead IMO. Change-Id: I2c08cf64b781328f515b88373170c4506058814c Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5066129 Commit-Queue: Frederick Mayle <fmayle@google.com> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
parent
98e465673b
commit
50cbe0cade
4 changed files with 11 additions and 37 deletions
13
third_party/vmm_vhost/src/connection.rs
vendored
13
third_party/vmm_vhost/src/connection.rs
vendored
|
@ -240,19 +240,6 @@ impl<R: Req> Endpoint<R> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads `len` bytes at most.
|
||||
///
|
||||
/// # Return:
|
||||
/// * - (number of bytes received, buf) on success
|
||||
pub fn recv_data(&self, len: usize) -> Result<Vec<u8>> {
|
||||
let mut buf = vec![0u8; len];
|
||||
let (data_len, _) = self
|
||||
.0
|
||||
.recv_into_bufs(&mut [IoSliceMut::new(&mut buf)], false /* allow_fd */)?;
|
||||
buf.truncate(data_len);
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// Reads all bytes into the given scatter/gather vectors with optional attached files. Will
|
||||
/// loop until all data has been transfered and errors if EOF is reached before then.
|
||||
///
|
||||
|
|
|
@ -386,8 +386,8 @@ mod tests {
|
|||
.unwrap();
|
||||
assert_eq!(len, 4);
|
||||
|
||||
let buf4 = slave.recv_data(2).unwrap();
|
||||
assert_eq!(buf4.len(), 2);
|
||||
let mut buf4 = vec![0u8; 2];
|
||||
slave.recv_into_bufs_all(&mut [&mut buf4[..]]).unwrap();
|
||||
assert_eq!(&buf1[..2], &buf4[..]);
|
||||
let (bytes, buf2, files) = slave.recv_into_buf(0x2).unwrap();
|
||||
assert_eq!(bytes, 2);
|
||||
|
@ -451,8 +451,9 @@ mod tests {
|
|||
.unwrap();
|
||||
assert_eq!(len, 4);
|
||||
|
||||
let v = slave.recv_data(5).unwrap();
|
||||
assert_eq!(v.len(), 5);
|
||||
let mut v = vec![0u8; 5];
|
||||
slave.recv_into_bufs_all(&mut [&mut v[..]]).unwrap();
|
||||
assert_eq!(&v[..], &[1, 2, 3, 4, 1]);
|
||||
|
||||
let (bytes, _, files) = slave.recv_into_buf(0x4).unwrap();
|
||||
assert_eq!(bytes, 3);
|
||||
|
|
12
third_party/vmm_vhost/src/master_req_handler.rs
vendored
12
third_party/vmm_vhost/src/master_req_handler.rs
vendored
|
@ -303,16 +303,8 @@ impl<S: VhostUserMasterReqHandler> MasterReqHandler<S> {
|
|||
// . validate message body and optional payload
|
||||
let (hdr, files) = self.sub_sock.recv_header()?;
|
||||
self.check_attached_files(&hdr, &files)?;
|
||||
let buf = match hdr.get_size() {
|
||||
0 => vec![0u8; 0],
|
||||
len => {
|
||||
let rbuf = self.sub_sock.recv_data(len as usize)?;
|
||||
if rbuf.len() != len as usize {
|
||||
return Err(Error::InvalidMessage);
|
||||
}
|
||||
rbuf
|
||||
}
|
||||
};
|
||||
let mut buf = vec![0u8; hdr.get_size().try_into().unwrap()];
|
||||
self.sub_sock.recv_into_bufs_all(&mut [&mut buf[..]])?;
|
||||
let size = buf.len();
|
||||
|
||||
let res = match hdr.get_code() {
|
||||
|
|
14
third_party/vmm_vhost/src/slave_req_handler.rs
vendored
14
third_party/vmm_vhost/src/slave_req_handler.rs
vendored
|
@ -667,16 +667,10 @@ impl<S: VhostUserSlaveReqHandler> SlaveReqHandler<S> {
|
|||
hdr: VhostUserMsgHeader<MasterReq>,
|
||||
files: Option<Vec<File>>,
|
||||
) -> Result<()> {
|
||||
let buf = match hdr.get_size() {
|
||||
0 => vec![0u8; 0],
|
||||
len => {
|
||||
let rbuf = self.slave_req_helper.endpoint.recv_data(len as usize)?;
|
||||
if rbuf.len() != len as usize {
|
||||
return Err(Error::InvalidMessage);
|
||||
}
|
||||
rbuf
|
||||
}
|
||||
};
|
||||
let mut buf = vec![0u8; hdr.get_size().try_into().unwrap()];
|
||||
self.slave_req_helper
|
||||
.endpoint
|
||||
.recv_into_bufs_all(&mut [&mut buf[..]])?;
|
||||
let size = buf.len();
|
||||
|
||||
match hdr.get_code() {
|
||||
|
|
Loading…
Reference in a new issue