Sponge
CS144's user-space TCP library
Classes | Public Member Functions | Protected Member Functions | List of all members
UDPSocket Class Reference

A wrapper around UDP sockets. More...

#include <socket.hh>

Inheritance diagram for UDPSocket:
Inheritance graph
[legend]

Classes

struct  received_datagram
 Returned by UDPSocket::recv; carries received data and information about the sender. More...
 

Public Member Functions

 UDPSocket ()
 Default: construct an unbound, unconnected UDP socket. More...
 
received_datagram recv (const size_t mtu=65536)
 Receive a datagram and the Address of its sender. More...
 
void recv (received_datagram &datagram, const size_t mtu=65536)
 Receive a datagram and the Address of its sender (caller can allocate storage) More...
 
void send (const BufferViewList &payload)
 Send datagram to the socket's connected address (must call connect() first) More...
 
void sendto (const Address &destination, const BufferViewList &payload)
 Send a datagram to specified Address. More...
 
- Public Member Functions inherited from Socket
void bind (const Address &address)
 Bind a socket to a specified address with bind(2), usually for listen/accept. More...
 
void connect (const Address &address)
 Connect a socket to a specified peer address with connect(2). More...
 
Address local_address () const
 Get local address of socket with getsockname(2). More...
 
Address peer_address () const
 Get peer address of socket with getpeername(2). More...
 
void set_reuseaddr ()
 Allow local address to be reused sooner via SO_REUSEADDR. More...
 
void shutdown (const int how)
 Shut down a socket via shutdown(2). More...
 
- Public Member Functions inherited from FileDescriptor
 FileDescriptor (const int fd)
 Construct from a file descriptor number returned by the kernel. More...
 
 ~FileDescriptor ()=default
 Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero. More...
 
void close ()
 Close the underlying file descriptor. More...
 
FileDescriptor duplicate () const
 Copy a FileDescriptor explicitly, increasing the FDWrapper refcount. More...
 
std::string read (const size_t limit=std::numeric_limits< size_t >::max())
 Read up to limit bytes. More...
 
void read (std::string &str, const size_t limit=std::numeric_limits< size_t >::max())
 Read up to limit bytes into str (caller can allocate storage) More...
 
void set_blocking (const bool blocking_state)
 Set blocking(true) or non-blocking(false) More...
 
size_t write (BufferViewList buffer, const bool write_all=true)
 Write a buffer (or list of buffers), possibly blocking until all is written. More...
 
size_t write (const char *str, const bool write_all=true)
 Write a string, possibly blocking until all is written. More...
 
size_t write (const std::string &str, const bool write_all=true)
 Write a string, possibly blocking until all is written. More...
 
int fd_num () const
 underlying descriptor number More...
 
bool eof () const
 EOF flag state. More...
 
bool closed () const
 closed flag state More...
 
unsigned int read_count () const
 number of reads More...
 
unsigned int write_count () const
 number of writes More...
 
 FileDescriptor (const FileDescriptor &other)=delete
 copy construction is forbidden More...
 
FileDescriptoroperator= (const FileDescriptor &other)=delete
 copy assignment is forbidden More...
 
 FileDescriptor (FileDescriptor &&other)=default
 move construction is allowed More...
 
FileDescriptoroperator= (FileDescriptor &&other)=default
 move assignment is allowed More...
 

Protected Member Functions

 UDPSocket (FileDescriptor &&fd)
 Construct from FileDescriptor (used by TCPOverUDPSocketAdapter) More...
 
- Protected Member Functions inherited from Socket
 Socket (const int domain, const int type)
 Construct via socket(2). More...
 
 Socket (FileDescriptor &&fd, const int domain, const int type)
 Construct from a file descriptor. More...
 
template<typename option_type >
void setsockopt (const int level, const int option, const option_type &option_value)
 Wrapper around setsockopt(2). More...
 

Detailed Description

A wrapper around UDP sockets.

Functions in this class are essentially wrappers over their POSIX eponyms.

Example:

const uint16_t portnum = ((std::random_device()()) % 50000) + 1025;
// create a UDP socket and bind it to a local address
sock1.bind(Address("127.0.0.1", portnum));
// create another UDP socket and send a datagram to the first socket without connecting
sock2.sendto(Address("127.0.0.1", portnum), "hi there");
// receive sent datagram, connect the socket to the peer's address, and send a response
auto recvd = sock1.recv();
sock1.connect(recvd.source_address);
sock1.send("hi yourself");
auto recvd2 = sock2.recv();
if (recvd.payload != "hi there" || recvd2.payload != "hi yourself") {
throw std::runtime_error("wrong data received");
}

Definition at line 51 of file socket.hh.


Class Documentation

◆ UDPSocket::received_datagram

struct UDPSocket::received_datagram

Returned by UDPSocket::recv; carries received data and information about the sender.

Definition at line 62 of file socket.hh.

Class Members
string payload UDP datagram payload.
Address source_address Address from which this datagram was received.

Constructor & Destructor Documentation

◆ UDPSocket() [1/2]

UDPSocket::UDPSocket ( FileDescriptor &&  fd)
inlineexplicitprotected

Construct from FileDescriptor (used by TCPOverUDPSocketAdapter)

Parameters
[in]fdis the FileDescriptor from which to construct

Definition at line 55 of file socket.hh.

◆ UDPSocket() [2/2]

UDPSocket::UDPSocket ( )
inline

Default: construct an unbound, unconnected UDP socket.

Definition at line 59 of file socket.hh.

Member Function Documentation

◆ recv() [1/2]

received_datagram UDPSocket::recv ( const size_t  mtu = 65536)

Receive a datagram and the Address of its sender.

◆ recv() [2/2]

void UDPSocket::recv ( received_datagram datagram,
const size_t  mtu = 65536 
)

Receive a datagram and the Address of its sender (caller can allocate storage)

◆ send()

void UDPSocket::send ( const BufferViewList payload)

Send datagram to the socket's connected address (must call connect() first)

Definition at line 139 of file socket.cc.

◆ sendto()

void UDPSocket::sendto ( const Address destination,
const BufferViewList payload 
)

Send a datagram to specified Address.

Definition at line 134 of file socket.cc.


The documentation for this class was generated from the following files:
sock1
UDPSocket sock1
Definition: socket_example_1.cc:4
portnum
const uint16_t portnum
Definition: socket_example_1.cc:1
recvd2
auto recvd2
Definition: socket_example_1.cc:16
UDPSocket::sendto
void sendto(const Address &destination, const BufferViewList &payload)
Send a datagram to specified Address.
Definition: socket.cc:134
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
std::random_device
sock2
UDPSocket sock2
Definition: socket_example_1.cc:8
Socket::bind
void bind(const Address &address)
Bind a socket to a specified address with bind(2), usually for listen/accept.
Definition: socket.cc:61
std::runtime_error
Socket::connect
void connect(const Address &address)
Connect a socket to a specified peer address with connect(2).
Definition: socket.cc:65
UDPSocket::recv
received_datagram recv(const size_t mtu=65536)
Receive a datagram and the Address of its sender.
UDPSocket
A wrapper around UDP sockets.
Definition: socket.hh:51
UDPSocket::send
void send(const BufferViewList &payload)
Send datagram to the socket's connected address (must call connect() first)
Definition: socket.cc:139
recvd
auto recvd
Definition: socket_example_1.cc:12