|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
17 static void show_usage(
const char *argv0,
const char *msg) {
18 cout <<
"Usage: " << argv0 <<
" [options] <host> <port>\n\n"
20 <<
" Option Default\n"
23 <<
" -l Server (listen) mode. (client mode)\n"
24 <<
" In server mode, <host>:<port> is the address to bind.\n\n"
31 <<
" -Lu <loss> Set uplink loss to <rate> (float in 0..1) (no loss)\n"
32 <<
" -Ld <loss> Set downlink loss to <rate> (float in 0..1) (no loss)\n\n"
34 <<
" -h Show this message and quit.\n\n";
42 static void check_argc(
int argc,
char **argv,
int curr,
const char *err) {
43 if (curr + 3 >= argc) {
56 while (argc - curr > 2) {
57 if (
strncmp(
"-l", argv[curr], 3) == 0) {
61 }
else if (
strncmp(
"-w", argv[curr], 3) == 0) {
62 check_argc(argc, argv, curr,
"ERROR: -w requires one argument.");
63 c_fsm.recv_capacity =
strtol(argv[curr + 1],
nullptr, 0);
66 }
else if (
strncmp(
"-t", argv[curr], 3) == 0) {
67 check_argc(argc, argv, curr,
"ERROR: -t requires one argument.");
68 c_fsm.rt_timeout =
strtol(argv[curr + 1],
nullptr, 0);
71 }
else if (
strncmp(
"-Lu", argv[curr], 3) == 0) {
72 check_argc(argc, argv, curr,
"ERROR: -Lu requires one argument.");
73 float lossrate =
strtof(argv[curr + 1],
nullptr);
74 using LossRateUpT = decltype(c_filt.loss_rate_up);
79 }
else if (
strncmp(
"-Ld", argv[curr], 3) == 0) {
80 check_argc(argc, argv, curr,
"ERROR: -Lu requires one argument.");
81 float lossrate =
strtof(argv[curr + 1],
nullptr);
82 using LossRateDnT = decltype(c_filt.loss_rate_dn);
87 }
else if (
strncmp(
"-h", argv[curr], 3) == 0) {
98 c_filt.source = {
"0", argv[argc - 1]};
100 c_filt.destination = {argv[argc - 2], argv[argc - 1]};
106 int main(
int argc,
char **argv) {
109 show_usage(argv[0],
"ERROR: required arguments are missing.");
119 udp_sock.
bind(c_filt.source);
125 tcp_socket.
connect(c_fsm, c_filt);
static constexpr size_t MAX_PAYLOAD_SIZE
Max TCP payload that fits in either IPv4 or UDP datagram.
static tuple< TCPConfig, FdAdapterConfig, bool > get_config(int argc, char **argv)
Config for classes derived from FdAdapter.
static void show_usage(const char *argv0, const char *msg)
void bind(const Address &address)
Bind a socket to a specified address with bind(2), usually for listen/accept.
void connect(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Connect using the specified configurations; blocks until connect succeeds or fails.
Config for TCP sender and receiver.
constexpr uint16_t DPORT_DFLT
void listen_and_accept(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Listen and accept using the specified configurations; blocks until accept succeeds or fails.
void bidirectional_stream_copy(Socket &socket)
Copy socket input/output to stdin/stdout until finished.
A wrapper around UDP sockets.
int main(int argc, char **argv)
static void check_argc(int argc, char **argv, int curr, const char *err)
static constexpr uint16_t TIMEOUT_DFLT
Default re-transmit timeout is 1 second.
A FD adaptor that reads and writes TCP segments in UDP payloads.
LossyFdAdapter< TCPOverUDPSocketAdapter > LossyTCPOverUDPSocketAdapter
Typedef for TCPOverUDPSocketAdapter.
Multithreaded wrapper around TCPConnection that approximates the Unix sockets API.