Sponge
CS144's user-space TCP library
|
#include "util.hh"
#include <array>
#include <cctype>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <sys/socket.h>
Go to the source code of this file.
Functions | |
mt19937 | get_random_generator () |
Seed a fast random generator. More... | |
void | hexdump (const char *data, const size_t len, const size_t indent) |
void | hexdump (const uint8_t *data, const size_t len, const size_t indent) |
int | SystemCall (const char *attempt, const int return_value, const int errno_mask) |
Error-checking wrapper for most syscalls. More... | |
int | SystemCall (const string &attempt, const int return_value, const int errno_mask) |
Version of SystemCall that takes a C++ std::string. More... | |
uint64_t | timestamp_ms () |
Get the time in milliseconds since the program began. More... | |
mt19937 get_random_generator | ( | ) |
Seed a fast random generator.
A properly seeded mt19937 generator takes a lot of entropy!
This code borrows from the following:
int SystemCall | ( | const char * | attempt, |
const int | return_value, | ||
const int | errno_mask | ||
) |
Error-checking wrapper for most syscalls.
[in] | attempt | is the name of the syscall to try (for error reporting) |
[in] | return_value | is the return value of the syscall |
[in] | errno_mask | is any errno value that is acceptable, e.g., EAGAIN when reading a non-blocking fd |
This function works for any syscall that returns less than 0 on error and sets errno:
For example, to wrap a call to open(2), you might say:
If you don't have permission to open the file, SystemCall will throw a std::runtime_error. If you don't want to throw in that case, you can pass EACCESS
in errno_mask
:
int SystemCall | ( | const string & | attempt, |
const int | return_value, | ||
const int | errno_mask | ||
) |
Version of SystemCall that takes a C++ std::string.
[in] | attempt | is the name of the syscall to try (for error reporting) |
[in] | return_value | is the return value of the syscall |
[in] | errno_mask | is any errno value that is acceptable, e.g., EAGAIN when reading a non-blocking fd |
see the other SystemCall() documentation for more details