userver: engine::io::Socket Class Reference
Loading...
Searching...
No Matches
engine::io::Socket Class Referencefinal

#include <userver/engine/io/socket.hpp>

Detailed Description

Socket representation.

It is not thread-safe to concurrently read from socket. It is not thread-safe to concurrently write to socket. However it is safe to concurrently read and write into socket:

// Sending and receiving data from self on the same socket
engine::io::Socket& socket = listener.socket;
auto read_task = engine::AsyncNoTracing([&socket, &deadline] {
for (char expected_data = 0; expected_data <= 100; ++expected_data) {
char c = 0;
const auto recvfrom = socket.RecvSomeFrom(&c, 1, deadline);
EXPECT_EQ(1, recvfrom.bytes_received);
EXPECT_EQ(expected_data, c);
}
});
const auto& addr = socket.Getsockname();
for (char send_data = 0; send_data <= 100; ++send_data) {
const auto bytes_sent = socket.SendAllTo(addr, &send_data, 1, deadline);
EXPECT_EQ(bytes_sent, 1);
}
read_task.Get();
Examples
samples/tcp_full_duplex_service/main.cpp, and samples/tcp_service/main.cpp.

Definition at line 41 of file socket.hpp.

+ Inheritance diagram for engine::io::Socket:

Classes

struct  RecvFromResult
 

Public Member Functions

 Socket ()=default
 Constructs an invalid socket.
 
 Socket (AddrDomain, SocketType)
 Constructs a socket for the address domain of specified type.
 
 Socket (int fd, AddrDomain domain=AddrDomain::kUnspecified)
 Adopts an existing socket for specified address domain.
 
 operator bool () const
 Whether the socket is valid.
 
bool IsValid () const override
 Whether the socket is valid.
 
void Connect (const Sockaddr &, Deadline)
 Connects the socket to a specified endpoint.
 
void Bind (const Sockaddr &)
 Binds the socket to the specified endpoint.
 
void Listen (int backlog=SOMAXCONN)
 Starts listening for connections on a specified socket (must be bound).
 
bool WaitReadable (Deadline) override
 
bool WaitWriteable (Deadline) override
 
size_t RecvSome (void *buf, size_t len, Deadline deadline)
 Receives at least one byte from the socket.
 
size_t RecvAll (void *buf, size_t len, Deadline deadline)
 Receives exactly len bytes from the socket.
 
std::optional< size_t > RecvNoblock (void *buf, size_t len)
 Receives up to len bytes from the stream.
 
size_t SendAll (std::initializer_list< IoData > list, Deadline deadline)
 Sends a buffer vector to the socket.
 
size_t SendAll (std::span< const IoData > list, Deadline deadline)
 
size_t WriteAll (std::span< const IoData > list, Deadline deadline) override
 Sends IoData array using vector I/O (e.g. writev).
 
size_t SendAll (const IoData *list, std::size_t list_size, Deadline deadline)
 Sends exactly list_size IoData to the socket.
 
size_t SendAll (const struct iovec *list, std::size_t list_size, Deadline deadline)
 Sends exactly list_size iovec to the socket.
 
size_t SendAll (const void *buf, size_t len, Deadline deadline)
 Sends exactly len bytes to the socket.
 
Socket Accept (Deadline)
 Accepts a connection from a listening socket.
 
RecvFromResult RecvSomeFrom (void *buf, size_t len, Deadline deadline)
 Receives at least one byte from the socket, returning source address.
 
size_t SendAllTo (const Sockaddr &dest_addr, const void *buf, size_t len, Deadline deadline)
 Sends exactly len bytes to the specified address via the socket.
 
int Fd () const
 File descriptor corresponding to this socket.
 
const SockaddrGetpeername ()
 Address of a remote peer.
 
const SockaddrGetsockname ()
 Local socket address.
 
int Release () &&noexcept
 Releases file descriptor and invalidates the socket.
 
void Close ()
 Closes and invalidates the socket.
 
int GetOption (int layer, int optname) const
 Retrieves a socket option.
 
void SetOption (int layer, int optname, int optval)
 Sets a socket option.
 
void SetOption (int layer, int optname, const void *optval, socklen_t optlen)
 Sets a socket option with non-trivial optval.
 
size_t ReadSome (void *buf, size_t len, Deadline deadline) override
 Receives at least one byte from the socket.
 
size_t ReadAll (void *buf, size_t len, Deadline deadline) override
 Receives exactly len bytes from the socket.
 
std::optional< size_t > ReadNoblock (void *buf, size_t len) override
 Receives up to len bytes from the stream.
 
size_t WriteAll (const void *buf, size_t len, Deadline deadline) override
 Writes exactly len bytes to the socket.
 
ReadableBaseGetReadableBase ()
 
WritableBaseGetWritableBase ()
 
AwaitableToken GetAwaitableToken ()
 Satisfies engine::Awaitable, for use with engine::WaitAnyContext and friends.
 
AwaitableToken GetAwaitableToken ()
 Satisfies engine::Awaitable, for use with engine::WaitAnyContext and friends.
 
virtual size_t WriteAll (std::initializer_list< IoData > list, Deadline deadline)
 Sends IoData initializer list using vector I/O (e.g. writev).
 

Protected Member Functions

void SetReadableAwaitableToken (AwaitableToken token)
 
void SetWritableAwaitableToken (AwaitableToken token)
 

Constructor & Destructor Documentation

◆ Socket()

engine::io::Socket::Socket ( int fd,
AddrDomain domain = AddrDomain::kUnspecified )
explicit

Adopts an existing socket for specified address domain.

Note
File descriptor will be silently forced to nonblocking mode.

Member Function Documentation

◆ Accept()

Socket engine::io::Socket::Accept ( Deadline )
nodiscard

Accepts a connection from a listening socket.

See also
engine::io::Listen

◆ Bind()

void engine::io::Socket::Bind ( const Sockaddr & )

Binds the socket to the specified endpoint.

Note
Sockaddr domain must match the socket's domain.

◆ Close()

void engine::io::Socket::Close ( )

Closes and invalidates the socket.

Warning
You should not call Close with pending I/O. This may work okay sometimes but it's loosely predictable.

◆ Connect()

void engine::io::Socket::Connect ( const Sockaddr & ,
Deadline  )

Connects the socket to a specified endpoint.

Note
Sockaddr domain must match the socket's domain.

◆ GetAwaitableToken() [1/2]

AwaitableToken engine::io::ReadAwaiter::GetAwaitableToken ( )
inlineinherited

Satisfies engine::Awaitable, for use with engine::WaitAnyContext and friends.

Definition at line 39 of file common.hpp.

◆ GetAwaitableToken() [2/2]

AwaitableToken engine::io::WriteAwaiter::GetAwaitableToken ( )
inlineinherited

Satisfies engine::Awaitable, for use with engine::WaitAnyContext and friends.

Definition at line 91 of file common.hpp.

◆ GetReadableBase()

ReadableBase & engine::io::RwBase::GetReadableBase ( )
inlineinherited

Definition at line 146 of file common.hpp.

◆ GetWritableBase()

WritableBase & engine::io::RwBase::GetWritableBase ( )
inlineinherited

Definition at line 148 of file common.hpp.

◆ IsValid()

bool engine::io::Socket::IsValid ( ) const
overridevirtual

Whether the socket is valid.

Implements engine::io::ReadableBase.

◆ operator bool()

engine::io::Socket::operator bool ( ) const
inlineexplicit

Whether the socket is valid.

Definition at line 59 of file socket.hpp.

◆ ReadAll()

size_t engine::io::Socket::ReadAll ( void * buf,
size_t len,
Deadline deadline )
inlinenodiscardoverridevirtual

Receives exactly len bytes from the socket.

Note
Can return less than len if socket is closed by peer.

Implements engine::io::ReadableBase.

Definition at line 175 of file socket.hpp.

◆ ReadNoblock()

std::optional< size_t > engine::io::Socket::ReadNoblock ( void * buf,
size_t len )
inlinenodiscardoverridevirtual

Receives up to len bytes from the stream.

Returns
  • nullopt on data absence
  • optional{0} if socket is closed by peer.
  • optional{data_bytes_available} otherwise, 1 <= data_bytes_available <= len

Reimplemented from engine::io::ReadableBase.

Definition at line 185 of file socket.hpp.

◆ ReadSome()

size_t engine::io::Socket::ReadSome ( void * buf,
size_t len,
Deadline deadline )
inlinenodiscardoverridevirtual

Receives at least one byte from the socket.

Returns
0 if connection is closed on one side and no data could be received any more, received bytes count otherwise.

Implements engine::io::ReadableBase.

Examples
samples/tcp_full_duplex_service/main.cpp.

Definition at line 169 of file socket.hpp.

◆ RecvAll()

size_t engine::io::Socket::RecvAll ( void * buf,
size_t len,
Deadline deadline )
nodiscard

Receives exactly len bytes from the socket.

Note
Can return less than len if socket is closed by peer.

◆ RecvNoblock()

std::optional< size_t > engine::io::Socket::RecvNoblock ( void * buf,
size_t len )
nodiscard

Receives up to len bytes from the stream.

Returns
  • nullopt on data absence
  • optional{0} if socket is closed by peer.
  • optional{data_bytes_available} otherwise, 1 <= data_bytes_available <= len

◆ RecvSome()

size_t engine::io::Socket::RecvSome ( void * buf,
size_t len,
Deadline deadline )
nodiscard

Receives at least one byte from the socket.

Returns
0 if connection is closed on one side and no data could be received any more, received bytes count otherwise.

◆ RecvSomeFrom()

RecvFromResult engine::io::Socket::RecvSomeFrom ( void * buf,
size_t len,
Deadline deadline )
nodiscard

Receives at least one byte from the socket, returning source address.

Returns
0 in bytes_sent if connection is closed on one side and no data could be received any more, received bytes count otherwise + source address.

◆ SendAll() [1/4]

size_t engine::io::Socket::SendAll ( const IoData * list,
std::size_t list_size,
Deadline deadline )
nodiscard

Sends exactly list_size IoData to the socket.

Note
Can return less than len if socket is closed by peer.

◆ SendAll() [2/4]

size_t engine::io::Socket::SendAll ( const struct iovec * list,
std::size_t list_size,
Deadline deadline )
nodiscard

Sends exactly list_size iovec to the socket.

Note
Can return less than len if socket is closed by peer.

◆ SendAll() [3/4]

size_t engine::io::Socket::SendAll ( const void * buf,
size_t len,
Deadline deadline )
nodiscard

Sends exactly len bytes to the socket.

Note
Can return less than len if socket is closed by peer.

◆ SendAll() [4/4]

size_t engine::io::Socket::SendAll ( std::initializer_list< IoData > list,
Deadline deadline )
nodiscard

Sends a buffer vector to the socket.

Note
Can return less than len if socket is closed by peer.
const auto bytes_sent = sockets.second.SendAll({{"data", 4}, {"chunk 1", 7}, {"chunk 2", 7}}, deadline);
Examples
samples/tcp_full_duplex_service/main.cpp.

◆ SendAllTo()

size_t engine::io::Socket::SendAllTo ( const Sockaddr & dest_addr,
const void * buf,
size_t len,
Deadline deadline )
nodiscard

Sends exactly len bytes to the specified address via the socket.

Note
Can return less than len in bytes_sent if socket is closed by peer.
Sockaddr domain must match the socket's domain.
Not for SocketType::kStream connections, see man sendto.

◆ SetReadableAwaitableToken()

void engine::io::ReadAwaiter::SetReadableAwaitableToken ( AwaitableToken token)
inlineprotectedinherited

Definition at line 44 of file common.hpp.

◆ SetWritableAwaitableToken()

void engine::io::WriteAwaiter::SetWritableAwaitableToken ( AwaitableToken token)
inlineprotectedinherited

Definition at line 96 of file common.hpp.

◆ WaitReadable()

bool engine::io::Socket::WaitReadable ( Deadline )
nodiscardoverridevirtual

Suspends current task until the socket has data available.

Returns
false on timeout or on task cancellations; true otherwise.

Implements engine::io::ReadAwaiter.

◆ WaitWriteable()

bool engine::io::Socket::WaitWriteable ( Deadline )
nodiscardoverridevirtual

Suspends current task until the socket can accept more data.

Returns
false on timeout or on task cancellations; true otherwise.

Implements engine::io::WriteAwaiter.

◆ WriteAll() [1/3]

size_t engine::io::Socket::WriteAll ( const void * buf,
size_t len,
Deadline deadline )
inlinenodiscardoverridevirtual

Writes exactly len bytes to the socket.

Note
Can return less than len if socket is closed by peer.

Implements engine::io::WritableBase.

Definition at line 189 of file socket.hpp.

◆ WriteAll() [2/3]

size_t engine::io::Socket::WriteAll ( std::span< const IoData > list,
Deadline deadline )
inlinenodiscardoverridevirtual

Sends IoData array using vector I/O (e.g. writev).

Note
Can return less than total bytes if stream is closed by peer.

Reimplemented from engine::io::WritableBase.

Definition at line 107 of file socket.hpp.

◆ WriteAll() [3/3]

virtual size_t engine::io::WritableBase::WriteAll ( std::initializer_list< IoData > list,
Deadline deadline )
inlinenodiscardvirtualinherited

Sends IoData initializer list using vector I/O (e.g. writev).

Note
Can return less than total bytes if stream is closed by peer.

Reimplemented in engine::io::TlsWrapper.

Definition at line 133 of file common.hpp.


The documentation for this class was generated from the following file: