Skip to main content

Socket

Struct Socket 

Source
pub struct Socket { /* private fields */ }
Expand description

An owned, non-blocking socket. Closed on drop.

Implementations§

Source§

impl Socket

Source

pub fn new(family: i32, ty: i32) -> Result<Socket, Error>

Creates a raw non-blocking socket.

Source

pub fn tcp_listen( addr: SocketAddr, opts: &ListenOptions, ) -> Result<Socket, Error>

A non-blocking TCP listener bound to addr.

Source

pub fn udp_bind(addr: SocketAddr, opts: &UdpOptions) -> Result<Socket, Error>

A non-blocking UDP socket bound to addr.

Source

pub fn tcp_connect(addr: SocketAddr) -> Result<Socket, Error>

Connects a TCP socket to addr, waiting for the handshake, and returns it non-blocking with TCP_NODELAY set. Meant for clients and tests.

Examples found in repository?
examples/loadgen.rs (line 108)
76fn main() {
77    let mut addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
78    let (mut conns, mut threads, mut secs, mut pipeline) = (64usize, 4usize, 10u64, 1usize);
79    let mut cpus: Option<Vec<usize>> = None;
80    let v: Vec<String> = std::env::args().skip(1).collect();
81    let mut i = 0;
82    while i < v.len() {
83        let val = v.get(i + 1).cloned().unwrap_or_default();
84        match v[i].as_str() {
85            "--addr" => addr = val.parse().expect("--addr"),
86            "--conns" => conns = val.parse().expect("--conns"),
87            "--threads" => threads = val.parse().expect("--threads"),
88            "--secs" => secs = val.parse().expect("--secs"),
89            "--pipeline" => pipeline = val.parse().expect("--pipeline"),
90            "--cpus" => cpus = Some(val.split(',').map(|s| s.trim().parse().expect("--cpus")).collect()),
91            other => panic!("unknown argument {other}"),
92        }
93        i += 2;
94    }
95    let rt = Runtime::builder()
96        .configure(|c| {
97            c.threads = threads;
98            c.thread_name = "loadgen".into();
99            if let Some(cpus) = cpus {
100                c.pin_threads = true;
101                c.cpus = Some(cpus);
102            }
103        })
104        .start()
105        .expect("start");
106    let (htx, hrx) = mpsc::channel::<ConnHandle>();
107    for k in 0..conns {
108        let sock = Socket::tcp_connect(addr).expect("connect");
109        let htx = htx.clone();
110        rt.loops()[k % threads].run(move |lcx| {
111            let h = lcx.adopt(sock, Box::new(Client { pipeline })).expect("adopt");
112            htx.send(h).unwrap();
113        });
114    }
115    drop(htx);
116    let handles: Vec<ConnHandle> = hrx.iter().take(conns).collect();
117    for h in &handles {
118        h.send(Box::new(()));
119    }
120    let count = |rt: &simoom_rt::RuntimeHandle| -> (u64, u64) {
121        let (tx, rx) = mpsc::channel();
122        for l in rt.loops() {
123            let tx = tx.clone();
124            l.run(move |_| tx.send((DONE.with(|c| c.get()), ERRORS.with(|c| c.get()))).unwrap());
125        }
126        drop(tx);
127        rx.iter().fold((0, 0), |a, b| (a.0 + b.0, a.1 + b.1))
128    };
129    std::thread::sleep(Duration::from_secs(1));
130    let (r0, _) = count(&rt);
131    let s0 = rt.stats().total().driver;
132    let t0 = Instant::now();
133    std::thread::sleep(Duration::from_secs(secs));
134    let (r1, errors) = count(&rt);
135    let el = t0.elapsed().as_secs_f64();
136    let s1 = rt.stats().total().driver;
137    let rps = (r1 - r0) as f64 / el;
138    let lat_us = (conns * pipeline) as f64 / rps * 1e6;
139    println!(
140        "{conns} conns x {pipeline} pipelined, {threads} threads: {rps:.0} req/s, mean latency {lat_us:.1} us (Little), closed connections {errors}, client syscalls/req {:.2}",
141        (s1.syscalls() - s0.syscalls()) as f64 / (r1 - r0).max(1) as f64
142    );
143    rt.stop(Duration::ZERO);
144    rt.join();
145}
Source

pub fn accept(&self) -> Result<(Socket, Option<SocketAddr>), Error>

Accepts one pending connection without blocking. The new socket is non-blocking.

Source

pub fn recv(&self, buf: &mut [u8]) -> Result<usize, Error>

Receives into buf without blocking. Ok(0) means the peer closed its side.

Source

pub fn recv_into(&self, buf: &mut Vec<u8>) -> Result<usize, Error>

Receives into the spare capacity of buf and extends its length by the bytes read.

Source

pub fn send(&self, buf: &[u8]) -> Result<usize, Error>

Sends from buf without blocking (never raises SIGPIPE).

Source

pub fn send_vectored(&self, bufs: &[&[u8]]) -> Result<usize, Error>

Sends several buffers with one call (sendmsg / WSASend) without blocking. At most 64 buffers are sent per call.

Source

pub fn shutdown(&self, how: Shutdown) -> Result<(), Error>

Shuts down one or both directions.

Source

pub fn set_nodelay(&self, on: bool) -> Result<(), Error>

Sets or clears TCP_NODELAY.

Source

pub fn nodelay(&self) -> Result<bool, Error>

Whether TCP_NODELAY is set.

Source

pub fn set_nonblocking(&self, on: bool) -> Result<(), Error>

Switches non-blocking mode.

Source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

The local address.

Source

pub fn peer_addr(&self) -> Result<SocketAddr, Error>

The peer address.

Source

pub fn take_error(&self) -> Result<Option<Error>, Error>

Reads and clears the pending socket error (SO_ERROR).

Source

pub fn setsockopt_int( &self, level: i32, name: i32, value: i32, ) -> Result<(), Error>

Sets an integer socket option.

Source

pub fn getsockopt_int(&self, level: i32, name: i32) -> Result<i32, Error>

Reads an integer socket option.

Source

pub fn raw(&self) -> i32

The raw handle (still owned by this Socket).

Source

pub unsafe fn from_raw(raw: i32) -> Socket

Takes ownership of a raw handle.

§Safety

raw must be an open socket that nothing else owns or will close.

Source

pub fn into_raw(self) -> i32

Gives up ownership of the raw handle.

Source

pub fn into_tcp_stream(self) -> TcpStream ⓘ

Converts into a std TCP stream (it stays non-blocking).

Source

pub fn into_tcp_listener(self) -> TcpListener

Converts into a std TCP listener (it stays non-blocking).

Source

pub fn into_udp_socket(self) -> UdpSocket

Converts into a std UDP socket (it stays non-blocking).

Source

pub fn try_clone(&self) -> Result<Socket, Error>

An independent handle to the same socket (dup / WSADuplicateSocket).

Source§

impl Socket

Source

pub fn recv_msgs( &self, bufs: &mut [Vec<u8>], meta: &mut [RecvMeta], ) -> Result<usize, Error>

Receives up to bufs.len() (at most BATCH) datagrams without blocking. Each buffer is cleared and filled up to its capacity; meta[i] describes bufs[i]. Returns how many were received, or a WouldBlock error when none was waiting.

Source

pub fn send_msgs(&self, msgs: &[Transmit<'_>]) -> Result<usize, Error>

Sends up to BATCH transmits without blocking. Returns how many were handed to the kernel; a WouldBlock error means none was (the socket buffer is full).

Trait Implementations§

Source§

impl Debug for Socket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<TcpListener> for Socket

Source§

fn from(s: TcpListener) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Source§

impl From<TcpStream> for Socket

Source§

fn from(s: TcpStream) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Source§

impl From<UdpSocket> for Socket

Source§

fn from(s: UdpSocket) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Auto Trait Implementations§

§

impl Forget for Socket

§

impl Freeze for Socket

§

impl Move for Socket

§

impl RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin for Socket

§

impl UnsafeUnpin for Socket

§

impl UnwindSafe for Socket

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.