cheatah
Class

socket::Conn

An owning TCP connection — a socket fd whose destructor closes it.

The RAII counterpart to the fd-based calls above, and the C++/cheatah analog of a Python socket used in a with block. A Conn owns exactly one fd; when it is destroyed (scope exit, including a return/break/exception out of a with body) or explicitly close()d, the fd is released — so a connection opened with with socket.open(host, port) as c { … } cannot leak. Move-only: copying would give two owners of one fd and double-close it, so the copy operations are deleted and a moved-from Conn is left closed.

Functions

fn Conn · 4 overloads
Conn()=defaultsource#
Conn(long long fd)source#
Conn(const Conn &)=deletesource#
Conn(Conn &&other) noexceptsource#

Construct a closed connection (owns no fd).

Complexity

O(1).

Allocation

none.

fn operator= · 2 overloads
Conn & operator=(const Conn &)=deletesource#
Conn & operator=(Conn &&other) noexceptsource#

Move-assign, closing this fd first, then taking over other's (which becomes closed).

Parameters
other

the connection to move from.

Returns

reference to this connection.

Complexity

O(1).

Allocation

none.

fn ~Conn() source#

Close the fd if still open.

Complexity

O(1) syscall.

Allocation

none.

fn bool is_open() const source#

Is a connection open?

Returns

true iff this owns an open fd.

Complexity

O(1).

Allocation

none.

fn long long fd() const source#

The raw fd, for the low-level calls or to hand to tls.open(conn.fd(), …).

Returns

the owned fd, or -1 when closed.

Complexity

O(1).

Allocation

none.

fn long long send(const std::string &data) source#

Send some of data (one send(); see the free send()).

Parameters
data

bytes to send.

Returns

bytes actually sent, or -1 on error.

Complexity

O(n).

Allocation

none.

fn long long sendall(const std::string &data) source#

Send data in full, looping until every byte is written (see the free sendall()).

Parameters
data

bytes to send.

Returns

0 on success, -1 on error.

Complexity

O(n).

Allocation

none.

fn std::string recv(long long bufsize) source#

Receive up to bufsize bytes (see the free recv()).

Parameters
bufsize

maximum bytes to read.

Returns

the bytes read (binary-safe), or "" on EOF/error.

Complexity

O(bufsize).

Allocation

allocates the returned string (plus the free recv()'s reused per-thread scratch buffer on growth).

Concurrency

blocks until data, EOF, or the set_timeout() deadline.

fn long long set_timeout(long long timeout_ms) source#

Bound both blocking directions by timeout_ms (see the free set_timeout()).

Parameters
timeout_ms

per-operation bound in milliseconds (<= 0 clears it).

Returns

0 on success, -1 on error.

Complexity

O(1).

Allocation

none.

fn long long local_port() const source#

The local TCP port this fd is bound to (see the free local_port()).

Returns

the port, or -1 on error.

Complexity

O(1) syscall.

Allocation

none.

fn long long shutdown() source#

Half-close both directions WITHOUT releasing the fd (see the free shutdown()) — wakes a blocked reader for a clean shutdown; still call close() (or let the destructor) afterward.

Returns

0 on success, -1 on error.

Complexity

O(1) syscall.

Allocation

none.

fn long long close() source#

Close the fd now (idempotent — the destructor will not close it again).

Returns

0 on success, -1 if already closed.

Complexity

O(1) syscall.

Allocation

none.

Constants & variables

var long long fd_ source#