Update locking to be interior to PeerPcb
This commit is contained in:
parent
261f24d9ef
commit
3e5a01ffbe
6 changed files with 57 additions and 77 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use std::{
|
||||
io::{Error, Read},
|
||||
mem::MaybeUninit,
|
||||
os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
|
||||
};
|
||||
|
||||
|
|
@ -38,14 +39,19 @@ impl IntoRawFd for TunInterface {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn assume_init(buf: &[MaybeUninit<u8>]) -> &[u8] {
|
||||
&*(buf as *const [MaybeUninit<u8>] as *const [u8])
|
||||
}
|
||||
|
||||
impl TunInterface {
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn recv(&mut self, buf: &mut [u8]) -> usize {
|
||||
// there might be a more efficient way to implement this
|
||||
let tmp_buf = &mut [0u8; 1500];
|
||||
let len = self.socket.read(tmp_buf)?;
|
||||
buf[..len - 4].copy_from_slice(&tmp_buf[4..len]);
|
||||
pub fn recv(&self, buf: &mut [u8]) -> usize {
|
||||
// Use IoVec to read directly into target buffer
|
||||
let mut tmp_buf = [MaybeUninit::uninit(); 1500];
|
||||
let len = self.socket.recv(&mut tmp_buf)?;
|
||||
let result_buf = unsafe { assume_init(&tmp_buf[4..len]) };
|
||||
buf[..len - 4].copy_from_slice(&result_buf);
|
||||
len - 4
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue