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

A complete endpoint of a TCP connection. More...

#include <tcp_connection.hh>

Public Member Functions

 TCPConnection (const TCPConfig &cfg)
 Construct a new connection from a configuration. More...
 
"Input" interface for the writer
void connect ()
 Initiate a connection by sending a SYN segment. More...
 
size_t write (const std::string &data)
 Write data to the outbound byte stream, and send it over TCP if possible. More...
 
size_t remaining_outbound_capacity () const
 
void end_input_stream ()
 Shut down the outbound byte stream (still allows reading incoming data) More...
 
"Output" interface for the reader
ByteStreaminbound_stream ()
 The inbound byte stream received from the peer. More...
 
Accessors used for testing
size_t bytes_in_flight () const
 number of bytes sent and not yet acknowledged, counting SYN/FIN each as one byte More...
 
size_t unassembled_bytes () const
 number of bytes not yet reassembled More...
 
size_t time_since_last_segment_received () const
 Number of milliseconds since the last segment was received. More...
 
TCPState state () const
 
Methods for the owner or operating system to call
void segment_received (const TCPSegment &seg)
 Called when a new segment has been received from the network. More...
 
void tick (const size_t ms_since_last_tick)
 Called periodically when time elapses. More...
 
std::queue< TCPSegment > & segments_out ()
 TCPSegments that the TCPConnection has enqueued for transmission. More...
 
bool active () const
 Is the connection still alive in any way? More...
 
construction and destruction

moving is allowed; copying is disallowed; default construction not possible

 ~TCPConnection ()
 destructor sends a RST if the connection is still open More...
 
 TCPConnection ()=delete
 
 TCPConnection (TCPConnection &&other)=default
 
TCPConnectionoperator= (TCPConnection &&other)=default
 
 TCPConnection (const TCPConnection &other)=delete
 
TCPConnectionoperator= (const TCPConnection &other)=delete
 

Private Attributes

TCPConfig _cfg
 
bool _linger_after_streams_finish {true}
 
TCPReceiver _receiver {_cfg.recv_capacity}
 
std::queue< TCPSegment_segments_out {}
 outbound queue of segments that the TCPConnection wants sent More...
 
TCPSender _sender {_cfg.send_capacity, _cfg.rt_timeout, _cfg.fixed_isn}
 

Detailed Description

A complete endpoint of a TCP connection.

Definition at line 10 of file tcp_connection.hh.

Constructor & Destructor Documentation

◆ TCPConnection() [1/4]

TCPConnection::TCPConnection ( const TCPConfig cfg)
inlineexplicit

Construct a new connection from a configuration.

Definition at line 84 of file tcp_connection.hh.

◆ ~TCPConnection()

TCPConnection::~TCPConnection ( )

destructor sends a RST if the connection is still open

Definition at line 39 of file tcp_connection.cc.

◆ TCPConnection() [2/4]

TCPConnection::TCPConnection ( )
delete

◆ TCPConnection() [3/4]

TCPConnection::TCPConnection ( TCPConnection &&  other)
default

◆ TCPConnection() [4/4]

TCPConnection::TCPConnection ( const TCPConnection other)
delete

Member Function Documentation

◆ active()

bool TCPConnection::active ( ) const

Is the connection still alive in any way?

Returns
true if either stream is still running or if the TCPConnection is lingering after both streams have finished (e.g. to ACK retransmissions from the peer)

Definition at line 25 of file tcp_connection.cc.

◆ bytes_in_flight()

size_t TCPConnection::bytes_in_flight ( ) const

number of bytes sent and not yet acknowledged, counting SYN/FIN each as one byte

Definition at line 17 of file tcp_connection.cc.

◆ connect()

void TCPConnection::connect ( )

Initiate a connection by sending a SYN segment.

Definition at line 37 of file tcp_connection.cc.

◆ end_input_stream()

void TCPConnection::end_input_stream ( )

Shut down the outbound byte stream (still allows reading incoming data)

Definition at line 35 of file tcp_connection.cc.

◆ inbound_stream()

ByteStream& TCPConnection::inbound_stream ( )
inline

The inbound byte stream received from the peer.

Definition at line 46 of file tcp_connection.hh.

◆ operator=() [1/2]

TCPConnection& TCPConnection::operator= ( const TCPConnection other)
delete

◆ operator=() [2/2]

TCPConnection& TCPConnection::operator= ( TCPConnection &&  other)
default

◆ remaining_outbound_capacity()

size_t TCPConnection::remaining_outbound_capacity ( ) const
Returns
the number of bytes that can be written right now.

Definition at line 15 of file tcp_connection.cc.

◆ segment_received()

void TCPConnection::segment_received ( const TCPSegment seg)

Called when a new segment has been received from the network.

Definition at line 23 of file tcp_connection.cc.

◆ segments_out()

std::queue<TCPSegment>& TCPConnection::segments_out ( )
inline

TCPSegments that the TCPConnection has enqueued for transmission.

Note
The owner or operating system will dequeue these and put each one into the payload of a lower-layer datagram (usually Internet datagrams (IP), but could also be user datagrams (UDP) or any other kind).

Definition at line 75 of file tcp_connection.hh.

◆ state()

TCPState TCPConnection::state ( ) const
inline

Definition at line 59 of file tcp_connection.hh.

◆ tick()

void TCPConnection::tick ( const size_t  ms_since_last_tick)

Called periodically when time elapses.

Parameters
[in]ms_since_last_ticknumber of milliseconds since the last call to this method

Definition at line 33 of file tcp_connection.cc.

◆ time_since_last_segment_received()

size_t TCPConnection::time_since_last_segment_received ( ) const

Number of milliseconds since the last segment was received.

summarize the state of the sender, receiver, and the connection

Definition at line 21 of file tcp_connection.cc.

◆ unassembled_bytes()

size_t TCPConnection::unassembled_bytes ( ) const

number of bytes not yet reassembled

Definition at line 19 of file tcp_connection.cc.

◆ write()

size_t TCPConnection::write ( const std::string data)

Write data to the outbound byte stream, and send it over TCP if possible.

Returns
the number of bytes from data that were actually written.

Definition at line 27 of file tcp_connection.cc.

Member Data Documentation

◆ _cfg

TCPConfig TCPConnection::_cfg
private

Definition at line 12 of file tcp_connection.hh.

◆ _linger_after_streams_finish

bool TCPConnection::_linger_after_streams_finish {true}
private

Should the TCPConnection stay active (and keep ACKing) for 10 * _cfg.rt_timeout milliseconds after both streams have ended, in case the remote TCPConnection doesn't know we've received its whole stream?

Definition at line 22 of file tcp_connection.hh.

◆ _receiver

TCPReceiver TCPConnection::_receiver {_cfg.recv_capacity}
private

Definition at line 13 of file tcp_connection.hh.

◆ _segments_out

std::queue<TCPSegment> TCPConnection::_segments_out {}
private

outbound queue of segments that the TCPConnection wants sent

Definition at line 17 of file tcp_connection.hh.

◆ _sender

TCPSender TCPConnection::_sender {_cfg.send_capacity, _cfg.rt_timeout, _cfg.fixed_isn}
private

Definition at line 14 of file tcp_connection.hh.


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