Skip to main content

Socket

Struct Socket 

Source
pub struct Socket<'n, 'a: 'n> { /* private fields */ }
Expand description

A UDP socket, held as a capability.

Minted by net-stack and validated by the kernel on every use, exactly as a file capability is (7.10). Holding one is the authority to send through it and nothing else: it names one source port, it cannot be widened, and it stops working the moment net-stack revokes it or dies.

§Why it borrows the Net handle

The same reason File borrows its Fs: both speak to one service over one endpoint and must share ONE correlation-tag counter. Two counters can mint the same tag for two exchanges in flight, and the result is not a loud rejection but a stale reply silently accepted as the current answer. The borrow makes that unspellable.

Implementations§

Source§

impl<'n, 'a: 'n> Socket<'n, 'a>

Source

pub fn send_to( &mut self, ip: Ipv4, port: u16, data: &[u8], buf: &mut [u8], ) -> Result<usize, Error>

Send a datagram and return what came back, in buf.

Returns how many bytes of response landed in buf. Zero means nothing answered - which is an ordinary outcome for UDP, not an error, and is why this is Ok(0) rather than an Err. Nothing is retried on your behalf.

Blocks up to SOCKET_SECS, because the round trip happens inside net-stack - it sends, retries, and waits for the response before replying to us at all.

Authority: this capability’s WRITE right, checked by the KERNEL before net-stack is reached.

§The ambiguity, stated

net-stack answers “nothing came back” - and also “not sent at all”, when it has no gateway yet - with a single zero byte, which is byte-identical to a genuine one-byte response of 0x00. This reports both as Ok(0). The protocol makes them the same bytes, so no reading of it can tell them apart; recording that is better than choosing one and being quietly wrong for the other. (services/shell reports the sentinel as one byte of DATA, which is how sock says “received 1 bytes back” about a query nobody answered.)

§Errors
Source

pub fn take_held(&mut self) -> Option<Message>

Take one message that arrived during an operation and was NOT the reply.

Drain this in a loop after every operation if your task serves clients - they are real requests, held rather than dropped, and only you can answer them. A program that serves nobody always gets None.

Trait Implementations§

Source§

impl<'n, 'a: 'n> Drop for Socket<'n, 'a>

Source§

fn drop(&mut self)

Drops our handle to the capability.

There is no close operation for a UDP socket - net-stack exposes send and nothing else on this capability - so this releases the local cap-table slot and cannot tell the service anything. The socket is reclaimed when net-stack revokes it or restarts. Recorded rather than hidden: a reader comparing this with File, which does close, would otherwise assume an omission.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'n, 'a> Freeze for Socket<'n, 'a>

§

impl<'n, 'a> !RefUnwindSafe for Socket<'n, 'a>

§

impl<'n, 'a> !Send for Socket<'n, 'a>

§

impl<'n, 'a> !Sync for Socket<'n, 'a>

§

impl<'n, 'a> Unpin for Socket<'n, 'a>

§

impl<'n, 'a> UnsafeUnpin for Socket<'n, 'a>

§

impl<'n, 'a> !UnwindSafe for Socket<'n, 'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.