#include "rtc_base/virtual_socket_server.h"
#include <errno.h>
#include <math.h>
#include <map>
#include <memory>
#include <vector>
#include "absl/algorithm/container.h"
#include "api/sequence_checker.h"
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/event.h"
#include "rtc_base/fake_clock.h"
#include "rtc_base/logging.h"
#include "rtc_base/physical_socket_server.h"
#include "rtc_base/socket_address_pair.h"
#include "rtc_base/thread.h"
#include "rtc_base/time_utils.h"
namespace rtc {
MutexLock;
TaskQueueBase;
TimeDelta;
#if defined(WEBRTC_WIN)
const in_addr kInitialNextIPv4 = {{{0x01, 0, 0, 0}}};
#else
const in_addr kInitialNextIPv4 = …;
#endif
const in6_addr kInitialNextIPv6 = …;
const uint16_t kFirstEphemeralPort = …;
const uint16_t kLastEphemeralPort = …;
const uint16_t kEphemeralPortCount = …;
const uint32_t kDefaultNetworkCapacity = …;
const uint32_t kDefaultTcpBufferSize = …;
const uint32_t UDP_HEADER_SIZE = …;
const uint32_t TCP_HEADER_SIZE = …;
const uint32_t TCP_MSS = …;
const int NUM_SAMPLES = …;
class Packet { … };
VirtualSocket::VirtualSocket(VirtualSocketServer* server, int family, int type)
: … { … }
VirtualSocket::~VirtualSocket() { … }
SocketAddress VirtualSocket::GetLocalAddress() const { … }
SocketAddress VirtualSocket::GetRemoteAddress() const { … }
void VirtualSocket::SetLocalAddress(const SocketAddress& addr) { … }
int VirtualSocket::Bind(const SocketAddress& addr) { … }
int VirtualSocket::Connect(const SocketAddress& addr) { … }
VirtualSocket::SafetyBlock::SafetyBlock(VirtualSocket* socket)
: … { … }
VirtualSocket::SafetyBlock::~SafetyBlock() { … }
void VirtualSocket::SafetyBlock::SetNotAlive() { … }
void VirtualSocket::SafetyBlock::PostSignalReadEvent() { … }
void VirtualSocket::SafetyBlock::MaybeSignalReadEvent() { … }
int VirtualSocket::Close() { … }
int VirtualSocket::Send(const void* pv, size_t cb) { … }
int VirtualSocket::SendTo(const void* pv,
size_t cb,
const SocketAddress& addr) { … }
int VirtualSocket::Recv(void* pv, size_t cb, int64_t* timestamp) { … }
int VirtualSocket::RecvFrom(void* pv,
size_t cb,
SocketAddress* paddr,
int64_t* timestamp) { … }
int VirtualSocket::SafetyBlock::RecvFrom(void* buffer,
size_t size,
SocketAddress& addr) { … }
int VirtualSocket::Listen(int backlog) { … }
void VirtualSocket::SafetyBlock::Listen() { … }
VirtualSocket* VirtualSocket::Accept(SocketAddress* paddr) { … }
VirtualSocket::SafetyBlock::AcceptResult VirtualSocket::SafetyBlock::Accept() { … }
int VirtualSocket::GetError() const { … }
void VirtualSocket::SetError(int error) { … }
Socket::ConnState VirtualSocket::GetState() const { … }
int VirtualSocket::GetOption(Option opt, int* value) { … }
int VirtualSocket::SetOption(Option opt, int value) { … }
void VirtualSocket::PostPacket(TimeDelta delay,
std::unique_ptr<Packet> packet) { … }
bool VirtualSocket::SafetyBlock::AddPacket(std::unique_ptr<Packet> packet) { … }
void VirtualSocket::PostConnect(TimeDelta delay,
const SocketAddress& remote_addr) { … }
void VirtualSocket::SafetyBlock::PostConnect(TimeDelta delay,
const SocketAddress& remote_addr) { … }
VirtualSocket::SafetyBlock::Signal VirtualSocket::SafetyBlock::Connect(
VirtualSocket::SafetyBlock::PostedConnects::iterator remote_addr_it) { … }
bool VirtualSocket::SafetyBlock::IsAlive() { … }
void VirtualSocket::PostDisconnect(TimeDelta delay) { … }
int VirtualSocket::InitiateConnect(const SocketAddress& addr, bool use_delay) { … }
void VirtualSocket::CompleteConnect(const SocketAddress& addr) { … }
int VirtualSocket::SendUdp(const void* pv,
size_t cb,
const SocketAddress& addr) { … }
int VirtualSocket::SendTcp(const void* pv, size_t cb) { … }
void VirtualSocket::OnSocketServerReadyToSend() { … }
void VirtualSocket::SetToBlocked() { … }
void VirtualSocket::UpdateRecv(size_t data_size) { … }
void VirtualSocket::UpdateSend(size_t data_size) { … }
void VirtualSocket::MaybeSignalWriteEvent(size_t capacity) { … }
uint32_t VirtualSocket::AddPacket(int64_t cur_time, size_t packet_size) { … }
int64_t VirtualSocket::UpdateOrderedDelivery(int64_t ts) { … }
size_t VirtualSocket::PurgeNetworkPackets(int64_t cur_time) { … }
VirtualSocketServer::VirtualSocketServer() : … { … }
VirtualSocketServer::VirtualSocketServer(ThreadProcessingFakeClock* fake_clock)
: … { … }
VirtualSocketServer::~VirtualSocketServer() { … }
IPAddress VirtualSocketServer::GetNextIP(int family) { … }
uint16_t VirtualSocketServer::GetNextPort() { … }
void VirtualSocketServer::SetSendingBlocked(bool blocked) { … }
VirtualSocket* VirtualSocketServer::CreateSocket(int family, int type) { … }
void VirtualSocketServer::SetMessageQueue(Thread* msg_queue) { … }
bool VirtualSocketServer::Wait(webrtc::TimeDelta max_wait_duration,
bool process_io) { … }
void VirtualSocketServer::WakeUp() { … }
void VirtualSocketServer::SetAlternativeLocalAddress(
const rtc::IPAddress& address,
const rtc::IPAddress& alternative) { … }
bool VirtualSocketServer::ProcessMessagesUntilIdle() { … }
void VirtualSocketServer::SetNextPortForTesting(uint16_t port) { … }
bool VirtualSocketServer::CloseTcpConnections(
const SocketAddress& addr_local,
const SocketAddress& addr_remote) { … }
int VirtualSocketServer::Bind(VirtualSocket* socket,
const SocketAddress& addr) { … }
SocketAddress VirtualSocketServer::AssignBindAddress(
const SocketAddress& app_addr) { … }
VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { … }
int VirtualSocketServer::Unbind(const SocketAddress& addr,
VirtualSocket* socket) { … }
void VirtualSocketServer::AddConnection(const SocketAddress& local,
const SocketAddress& remote,
VirtualSocket* remote_socket) { … }
VirtualSocket* VirtualSocketServer::LookupConnection(
const SocketAddress& local,
const SocketAddress& remote) { … }
void VirtualSocketServer::RemoveConnection(const SocketAddress& local,
const SocketAddress& remote) { … }
static double Random() { … }
int VirtualSocketServer::Connect(VirtualSocket* socket,
const SocketAddress& remote_addr,
bool use_delay) { … }
bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { … }
bool VirtualSocketServer::Disconnect(const SocketAddress& addr) { … }
bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr,
const SocketAddress& remote_addr) { … }
int VirtualSocketServer::SendUdp(VirtualSocket* socket,
const char* data,
size_t data_size,
const SocketAddress& remote_addr) { … }
void VirtualSocketServer::SendTcp(VirtualSocket* socket) { … }
void VirtualSocketServer::SendTcp(const SocketAddress& addr) { … }
void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender,
VirtualSocket* recipient,
int64_t cur_time,
const char* data,
size_t data_size,
size_t header_size,
bool ordered) { … }
uint32_t VirtualSocketServer::SendDelay(uint32_t size) { … }
#if 0
void PrintFunction(std::vector<std::pair<double, double> >* f) {
return;
double sum = 0;
for (uint32_t i = 0; i < f->size(); ++i) {
std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl;
sum += (*f)[i].second;
}
if (!f->empty()) {
const double mean = sum / f->size();
double sum_sq_dev = 0;
for (uint32_t i = 0; i < f->size(); ++i) {
double dev = (*f)[i].second - mean;
sum_sq_dev += dev * dev;
}
std::cout << "Mean = " << mean << " StdDev = "
<< sqrt(sum_sq_dev / f->size()) << std::endl;
}
}
#endif
void VirtualSocketServer::UpdateDelayDistribution() { … }
static double PI = …;
static double Normal(double x, double mean, double stddev) { … }
#if 0
static double Pareto(double x, double min, double k) {
if (x < min)
return 0;
else
return k * std::pow(min, k) / std::pow(x, k+1);
}
#endif
std::unique_ptr<VirtualSocketServer::Function>
VirtualSocketServer::CreateDistribution(uint32_t mean,
uint32_t stddev,
uint32_t samples) { … }
uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { … }
struct FunctionDomainCmp { … };
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate(
std::unique_ptr<Function> f) { … }
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert(
std::unique_ptr<Function> f) { … }
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample(
std::unique_ptr<Function> f,
double x1,
double x2,
uint32_t samples) { … }
double VirtualSocketServer::Evaluate(const Function* f, double x) { … }
bool VirtualSocketServer::CanInteractWith(VirtualSocket* local,
VirtualSocket* remote) { … }
IPAddress VirtualSocketServer::GetDefaultSourceAddress(int family) { … }
void VirtualSocketServer::SetDefaultSourceAddress(const IPAddress& from_addr) { … }
void VirtualSocketServer::set_bandwidth(uint32_t bandwidth) { … }
void VirtualSocketServer::set_network_capacity(uint32_t capacity) { … }
uint32_t VirtualSocketServer::send_buffer_capacity() const { … }
void VirtualSocketServer::set_send_buffer_capacity(uint32_t capacity) { … }
uint32_t VirtualSocketServer::recv_buffer_capacity() const { … }
void VirtualSocketServer::set_recv_buffer_capacity(uint32_t capacity) { … }
void VirtualSocketServer::set_delay_mean(uint32_t delay_mean) { … }
void VirtualSocketServer::set_delay_stddev(uint32_t delay_stddev) { … }
void VirtualSocketServer::set_delay_samples(uint32_t delay_samples) { … }
void VirtualSocketServer::set_drop_probability(double drop_prob) { … }
void VirtualSocketServer::set_max_udp_payload(size_t payload_size) { … }
uint32_t VirtualSocketServer::sent_packets() const { … }
}