pub struct LocalSocket {Show 16 fields
socket_type: SocketType,
self_weak: RwLock<Weak<LocalSocket>>,
protocol: SocketProtocol,
state: RwLock<SocketState>,
local_addr: RwLock<Option<String>>,
peer_addr: RwLock<Option<String>>,
read_buffer: RwLock<Arc<SocketBuffer>>,
peer_read_buffer: RwLock<Option<Arc<SocketBuffer>>>,
peer_socket: RwLock<Option<Weak<LocalSocket>>>,
backlog: RwLock<Vec<Arc<LocalSocket>>>,
max_backlog: RwLock<usize>,
accept_waker: Waker,
read_waker: Waker,
handle_waker: Waker,
handle_queue: RwLock<VecDeque<KernelObject>>,
nonblocking: RwLock<bool>,
}Expand description
Local Socket Implementation
This socket type provides local (Unix-like) domain socket functionality. It uses VecDeque buffers internally for data transfer and integrates with the NetworkManager for socket registry.
Fields§
§socket_type: SocketTypeSocket type (Stream, Datagram, etc.)
self_weak: RwLock<Weak<LocalSocket>>Weak self reference (initialized when wrapped in Arc)
This is used to establish peer relationships in methods that only
have &self (e.g., connect()), where we still need an Arc<Self>.
protocol: SocketProtocolSocket protocol
state: RwLock<SocketState>Current socket state
local_addr: RwLock<Option<String>>Local address (if bound)
peer_addr: RwLock<Option<String>>Peer address (if connected)
read_buffer: RwLock<Arc<SocketBuffer>>Read buffer: data received from peer (shared with peer for writing)
peer_read_buffer: RwLock<Option<Arc<SocketBuffer>>>Write buffer reference: shared with peer socket for writing When we write, we push to peer’s read_buffer
peer_socket: RwLock<Option<Weak<LocalSocket>>>Peer socket reference (for waking read waiters)
backlog: RwLock<Vec<Arc<LocalSocket>>>Backlog queue for listening sockets Contains pending connections waiting to be accepted
max_backlog: RwLock<usize>Maximum backlog size (set by listen())
accept_waker: WakerWaker for blocking accept() operations
read_waker: WakerWaker for blocking read() operations
handle_waker: WakerWaker for blocking recv_handle() operations
handle_queue: RwLock<VecDeque<KernelObject>>Queue of handles (KernelObjects) received from peer This allows passing file descriptors / kernel objects between tasks
nonblocking: RwLock<bool>Nonblocking I/O flag
Implementations§
Source§impl LocalSocket
impl LocalSocket
pub(crate) fn init_self_weak(this: &Arc<Self>)
Sourcepub fn from_socket_object(socket: &Arc<dyn SocketObject>) -> Option<&Self>
pub fn from_socket_object(socket: &Arc<dyn SocketObject>) -> Option<&Self>
Safely downcast a SocketObject to LocalSocket using Any trait
Returns None if the socket is not a LocalSocket. This is completely safe and does not use any unsafe code.
Sourcepub fn new(socket_type: SocketType, protocol: SocketProtocol) -> Self
pub fn new(socket_type: SocketType, protocol: SocketProtocol) -> Self
Sourcepub fn send_handle(&self, object: KernelObject) -> Result<(), IpcError>
pub fn send_handle(&self, object: KernelObject) -> Result<(), IpcError>
Send a KernelObject handle through this socket
This is LocalSocket-only (SCM_RIGHTS equivalent) and uses dup() semantics.
Sourcepub fn send_handle_and_data(
&self,
object: KernelObject,
data: &[u8],
) -> Result<(), IpcError>
pub fn send_handle_and_data( &self, object: KernelObject, data: &[u8], ) -> Result<(), IpcError>
Send a handle and data together atomically for Wayland protocol
This method ensures that both the handle and data are available before waking the peer, preventing race conditions where recvmsg might get the handle but not the data (or vice versa).
This is needed for Wayland protocol messages with file descriptors, where the client expects both the FD and message data in a single recvmsg call.
Sourcepub fn recv_handle_and_data(
&self,
max_data_len: usize,
) -> Result<(KernelObject, Vec<u8>), IpcError>
pub fn recv_handle_and_data( &self, max_data_len: usize, ) -> Result<(KernelObject, Vec<u8>), IpcError>
Receive a handle and data together atomically for Wayland protocol
Returns both a handle and data in a single atomic operation. This is the counterpart to send_handle_and_data().
§Arguments
max_data_len- Maximum amount of data to read
§Returns
(KernelObject, Vec<u8>)- Handle and data on successIpcError- Error if no handle/data available or other error
Sourcepub fn recv_handle(&self) -> Result<KernelObject, IpcError>
pub fn recv_handle(&self) -> Result<KernelObject, IpcError>
Receive a KernelObject handle from this socket (non-blocking)
Sourcepub fn accept_blocking(
&self,
task_id: usize,
trapframe: &mut Trapframe,
) -> Result<Arc<dyn SocketObject>, SocketError>
pub fn accept_blocking( &self, task_id: usize, trapframe: &mut Trapframe, ) -> Result<Arc<dyn SocketObject>, SocketError>
Accept a connection with blocking behavior
This method blocks the calling task until a connection is available in the backlog. It uses the waker mechanism to properly suspend and wake the task.
§Arguments
task_id- ID of the task calling accepttrapframe- Trapframe for scheduler context switching
§Returns
Arc to the accepted socket connection
Sourcepub fn create_connected_pair(
local_addr: String,
peer_addr: String,
) -> (Arc<Self>, Arc<Self>)
pub fn create_connected_pair( local_addr: String, peer_addr: String, ) -> (Arc<Self>, Arc<Self>)
Sourcepub fn recv_handle_blocking(
&self,
task_id: usize,
trapframe: &mut Trapframe,
) -> Result<KernelObject, IpcError>
pub fn recv_handle_blocking( &self, task_id: usize, trapframe: &mut Trapframe, ) -> Result<KernelObject, IpcError>
Blocking handle receive operation
This method blocks the calling task until a handle is available in the handle queue, or the peer is closed.