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

Base class for network sockets (TCP, UDP, etc.) More...

#include <socket.hh>

Inheritance diagram for Socket:
Inheritance graph
[legend]

Public Member Functions

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

 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...
 

Private Member Functions

Address get_address (const std::string &name_of_function, const std::function< int(int, sockaddr *, socklen_t *)> &function) const
 Get the local or peer address the socket is connected to. More...
 

Detailed Description

Base class for network sockets (TCP, UDP, etc.)

Socket is generally used via a subclass. See TCPSocket and UDPSocket for usage examples.

Definition at line 14 of file socket.hh.

Constructor & Destructor Documentation

◆ Socket() [1/2]

Socket::Socket ( const int  domain,
const int  type 
)
protected

Construct via socket(2).

Parameters
[in]domainis as described in socket(7), probably AF_INET or AF_UNIX
[in]typeis as described in socket(7)

Definition at line 14 of file socket.cc.

◆ Socket() [2/2]

Socket::Socket ( FileDescriptor &&  fd,
const int  domain,
const int  type 
)
protected

Construct from a file descriptor.

Parameters
[in]fdis the FileDescriptor from which to construct
[in]domainis fd's domain; throws std::runtime_error if wrong value is supplied
[in]typeis fd's type; throws std::runtime_error if wrong value is supplied

Definition at line 20 of file socket.cc.

Member Function Documentation

◆ bind()

void Socket::bind ( const Address address)

Bind a socket to a specified address with bind(2), usually for listen/accept.

Parameters
[in]addressis a local Address to bind

Definition at line 61 of file socket.cc.

◆ connect()

void Socket::connect ( const Address address)

Connect a socket to a specified peer address with connect(2).

Parameters
[in]addressis the peer's Address

Definition at line 65 of file socket.cc.

◆ get_address()

Address Socket::get_address ( const std::string name_of_function,
const std::function< int(int, sockaddr *, socklen_t *)> &  function 
) const
private

Get the local or peer address the socket is connected to.

Parameters
[in]name_of_functionis the function to call (string passed to SystemCall())
[in]functionis a pointer to the function
Returns
the requested Address

Definition at line 43 of file socket.cc.

◆ local_address()

Address Socket::local_address ( ) const

Get local address of socket with getsockname(2).

Returns
the local Address of the socket

Definition at line 54 of file socket.cc.

◆ peer_address()

Address Socket::peer_address ( ) const

Get peer address of socket with getpeername(2).

Returns
the socket's peer's Address

Definition at line 57 of file socket.cc.

◆ set_reuseaddr()

void Socket::set_reuseaddr ( )

Allow local address to be reused sooner via SO_REUSEADDR.

Note
Using SO_REUSEADDR may reduce the robustness of your application

Definition at line 168 of file socket.cc.

◆ setsockopt()

template<typename option_type >
void Socket::setsockopt ( const int  level,
const int  option,
const option_type &  option_value 
)
protected

Wrapper around setsockopt(2).

Parameters
[in]levelThe protocol level at which the argument resides
[in]optionA single option to set
[in]option_valueThe value to set

See setsockopt(2) for details.

Definition at line 162 of file socket.cc.

◆ shutdown()

void Socket::shutdown ( const int  how)

Shut down a socket via shutdown(2).

Parameters
[in]howcan be SHUT_RD, SHUT_WR, or SHUT_RDWR; see shutdown(2)

Definition at line 69 of file socket.cc.


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