net::socket
TCP and UDP sockets. Every operation that can wait is a future; see net for the ownership handshake all of them share.
public module net::socket::tcp;
public module net::socket::udp;
| File | What it holds | Import |
|---|---|---|
tcp | TcpListener, TcpStream, and TcpConnect / TcpAccept / TcpRead / TcpWrite | import std::net::socket::tcp; |
udp | UdpSocket and UdpSend / UdpSendTo / UdpRecv / UdpRecvFrom | import std::net::socket::udp; |
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
type struct TcpListener {
fd: i32;
}type struct TcpStream {
fd: i32;
}type struct TcpConnect {
fd: i32;
addr: [u8;
28];
addrlen: u32;
err: IoError;
rt: Reactor*;
}Connect to a remote address without blocking. The socket is opened and the handshake started when the future is created; each poll asks the OS whether it finished, parking on writability in between. The socket is closed if the future is dropped before it completes.
std::net::socket::tcptype struct TcpAccept {
listener: TcpListener;
rt: Reactor*;
}Accept the next incoming connection. An accepted socket does not inherit the listener's non-blocking mode, so it is set here — it is about to be used with these same futures. Owns the listener for the duration and hands it back in the TcpAccepted, so a server loop starts the next accept from that.
type struct TcpRead {
stream: TcpStream;
buf: Array<u8>;
rt: Reactor*;
}Read as soon as the socket has anything to give. Completes with the byte count — 0 means the peer closed its end — or the error that ended it.
std::net::socket::tcptype struct TcpWrite {
stream: TcpStream;
buf: Array<u8>;
rt: Reactor*;
}Write as soon as the socket has room. Completes with the number of bytes accepted, which may be fewer than asked for — a caller with more to send starts another write for the remainder, which is why the buffer comes back untouched. Owns the socket and the buffer exactly as TcpRead does, and for the same reason.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct UdpSocket {
fd: i32;
}type struct UdpSend {
socket: UdpSocket;
buf: Array<u8>;
rt: Reactor*;
}Send one datagram on a connected socket. Fails NotConnected if no default peer was pinned. Owns the socket and the buffer exactly as UdpSendTo does.
type struct UdpSendTo {
socket: UdpSocket;
buf: Array<u8>;
dest: SocketAddr;
rt: Reactor*;
}Send one datagram to dest. Completes with the number of bytes the kernel accepted; a partial datagram is never sent, so that is the whole buffer on success. The buffer comes back untouched.
type struct UdpRecv {
socket: UdpSocket;
buf: Array<u8>;
rt: Reactor*;
}Receive on a connected socket, dropping the peer address - it can only be the connected peer. Truncates an oversized datagram, as UdpRecvFrom does.
type struct UdpRecvFrom {
socket: UdpSocket;
buf: Array<u8>;
rt: Reactor*;
}Receive one datagram, completing with the byte count and the peer that sent it. A datagram longer than the buffer is truncated (the tail is discarded - POSIX UDP semantics).
std::net::socket::udp