Sponge
CS144's user-space TCP library
fd_adapter.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_FD_ADAPTER_HH
2 #define SPONGE_LIBSPONGE_FD_ADAPTER_HH
3 
4 #include "file_descriptor.hh"
5 #include "lossy_fd_adapter.hh"
6 #include "socket.hh"
7 #include "tcp_config.hh"
8 #include "tcp_header.hh"
9 #include "tcp_segment.hh"
10 
11 #include <optional>
12 #include <utility>
13 
17  private:
19  bool _listen = false;
20 
21  protected:
23 
24  public:
27  void set_listening(const bool l) { _listen = l; }
28 
31  bool listening() const { return _listen; }
32 
35  const FdAdapterConfig &config() const { return _cfg; }
36 
40 
42  void tick(const size_t) {}
43 };
44 
47  private:
49 
50  public:
52  explicit TCPOverUDPSocketAdapter(UDPSocket &&sock) : _sock(std::move(sock)) {}
53 
55  std::optional<TCPSegment> read();
56 
58  void write(TCPSegment &seg);
59 
61  operator UDPSocket &() { return _sock; }
62 
64  operator const UDPSocket &() const { return _sock; }
65 };
66 
69 
70 #endif // SPONGE_LIBSPONGE_FD_ADAPTER_HH
socket.hh
FdAdapterBase::_listen
bool _listen
Is the connected TCP FSM in listen state?
Definition: fd_adapter.hh:19
FdAdapterBase::config_mut
FdAdapterConfig & config_mut()
Get the current configuration (mutable)
Definition: fd_adapter.hh:39
tcp_segment.hh
file_descriptor.hh
TCPOverUDPSocketAdapter::TCPOverUDPSocketAdapter
TCPOverUDPSocketAdapter(UDPSocket &&sock)
Construct from a UDPSocket sliced into a FileDescriptor.
Definition: fd_adapter.hh:52
FdAdapterBase::listening
bool listening() const
Get the listening flag.
Definition: fd_adapter.hh:31
tcp_header.hh
TCPOverUDPSocketAdapter::write
void write(TCPSegment &seg)
Writes a TCP segment into a UDP payload.
Definition: fd_adapter.cc:50
TCPOverUDPSocketAdapter::read
std::optional< TCPSegment > read()
Attempts to read and return a TCP segment related to the current connection from a UDP payload.
Definition: fd_adapter.cc:21
FdAdapterConfig
Config for classes derived from FdAdapter.
Definition: tcp_config.hh:26
tcp_config.hh
lossy_fd_adapter.hh
LossyFdAdapter
An adapter class that adds random dropping behavior to an FD adapter.
Definition: lossy_fd_adapter.hh:15
FdAdapterBase
Basic functionality for file descriptor adaptors.
Definition: fd_adapter.hh:16
UDPSocket
A wrapper around UDP sockets.
Definition: socket.hh:51
FdAdapterBase::tick
void tick(const size_t)
Called periodically when time elapses.
Definition: fd_adapter.hh:42
std
FdAdapterBase::config
const FdAdapterConfig & config() const
Get the current configuration.
Definition: fd_adapter.hh:35
FdAdapterBase::set_listening
void set_listening(const bool l)
Set the listening flag.
Definition: fd_adapter.hh:27
FdAdapterBase::_cfg
FdAdapterConfig _cfg
Configuration values.
Definition: fd_adapter.hh:18
TCPOverUDPSocketAdapter
A FD adaptor that reads and writes TCP segments in UDP payloads.
Definition: fd_adapter.hh:46
TCPOverUDPSocketAdapter::_sock
UDPSocket _sock
Definition: fd_adapter.hh:48
TCPSegment
TCP segment
Definition: tcp_segment.hh:10
FdAdapterBase::config_mutable
FdAdapterConfig & config_mutable()
Definition: fd_adapter.hh:22