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

A wrapper around TCP sockets. More...

#include <socket.hh>

Inheritance diagram for TCPSocket:
Inheritance graph
[legend]

Public Member Functions

 TCPSocket ()
 Default: construct an unbound, unconnected TCP socket. More...
 
TCPSocket accept ()
 Accept a new incoming connection. More...
 
void listen (const int backlog=16)
 Mark a socket as listening for incoming connections. 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...
 

Private Member Functions

 TCPSocket (FileDescriptor &&fd)
 Construct from FileDescriptor (used by accept()) More...
 

Additional Inherited Members

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

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

Example:

const uint16_t portnum = ((std::random_device()()) % 50000) + 1025;
// create a TCP socket, bind it to a local address, and listen
sock1.bind(Address("127.0.0.1", portnum));
sock1.listen(1);
// create another socket and connect to the first one
sock2.connect(Address("127.0.0.1", portnum));
// accept the connection
auto sock3 = sock1.accept();
sock3.write("hi there");
auto recvd = sock2.read();
sock2.write("hi yourself");
auto recvd2 = sock3.read();
sock1.close(); // don't need to accept any more connections
sock2.close(); // you can call close(2) on a socket
sock3.shutdown(SHUT_RDWR); // you can also shutdown(2) a socket
if (recvd != "hi there" || recvd2 != "hi yourself") {
throw std::runtime_error("wrong data received");
}

Definition at line 88 of file socket.hh.

Constructor & Destructor Documentation

◆ TCPSocket() [1/2]

TCPSocket::TCPSocket ( FileDescriptor &&  fd)
inlineexplicitprivate

Construct from FileDescriptor (used by accept())

Parameters
[in]fdis the FileDescriptor from which to construct

Definition at line 92 of file socket.hh.

◆ TCPSocket() [2/2]

TCPSocket::TCPSocket ( )
inline

Default: construct an unbound, unconnected TCP socket.

Definition at line 96 of file socket.hh.

Member Function Documentation

◆ accept()

TCPSocket TCPSocket::accept ( )

Accept a new incoming connection.

Returns
a new TCPSocket connected to the peer.
Note
This function blocks until a new connection is available

Definition at line 151 of file socket.cc.

◆ listen()

void TCPSocket::listen ( const int  backlog = 16)

Mark a socket as listening for incoming connections.

Parameters
[in]backlogis the number of waiting connections to queue (see listen(2))

Definition at line 146 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
TCPSocket
A wrapper around TCP sockets.
Definition: socket.hh:88
recvd2
auto recvd2
Definition: socket_example_1.cc:16
FileDescriptor::write
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
Definition: file_descriptor.hh:65
sock3
auto sock3
Definition: socket_example_2.cc:13
FileDescriptor::close
void close()
Close the underlying file descriptor.
Definition: file_descriptor.hh:74
FileDescriptor::read
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
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
recvd
auto recvd
Definition: socket_example_1.cc:12