123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #ifndef RTC_BASE_VIRTUAL_SOCKET_SERVER_H_
- #define RTC_BASE_VIRTUAL_SOCKET_SERVER_H_
- #include <deque>
- #include <map>
- #include <vector>
- #include "rtc_base/checks.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/deprecated/recursive_critical_section.h"
- #include "rtc_base/event.h"
- #include "rtc_base/fake_clock.h"
- #include "rtc_base/message_handler.h"
- #include "rtc_base/socket_server.h"
- namespace rtc {
- class Packet;
- class VirtualSocket;
- class SocketAddressPair;
- class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
- public:
- VirtualSocketServer();
-
-
-
- explicit VirtualSocketServer(ThreadProcessingFakeClock* fake_clock);
- ~VirtualSocketServer() override;
-
-
- IPAddress GetDefaultRoute(int family);
- void SetDefaultRoute(const IPAddress& from_addr);
-
-
- uint32_t bandwidth() const { return bandwidth_; }
- void set_bandwidth(uint32_t bandwidth) { bandwidth_ = bandwidth; }
-
-
- uint32_t network_capacity() const { return network_capacity_; }
- void set_network_capacity(uint32_t capacity) { network_capacity_ = capacity; }
-
- uint32_t send_buffer_capacity() const { return send_buffer_capacity_; }
- void set_send_buffer_capacity(uint32_t capacity) {
- send_buffer_capacity_ = capacity;
- }
-
- uint32_t recv_buffer_capacity() const { return recv_buffer_capacity_; }
- void set_recv_buffer_capacity(uint32_t capacity) {
- recv_buffer_capacity_ = capacity;
- }
-
-
-
- uint32_t delay_mean() const { return delay_mean_; }
- uint32_t delay_stddev() const { return delay_stddev_; }
- uint32_t delay_samples() const { return delay_samples_; }
- void set_delay_mean(uint32_t delay_mean) { delay_mean_ = delay_mean; }
- void set_delay_stddev(uint32_t delay_stddev) { delay_stddev_ = delay_stddev; }
- void set_delay_samples(uint32_t delay_samples) {
- delay_samples_ = delay_samples;
- }
-
-
- void UpdateDelayDistribution();
-
-
- double drop_probability() { return drop_prob_; }
- void set_drop_probability(double drop_prob) {
- RTC_DCHECK_GE(drop_prob, 0.0);
- RTC_DCHECK_LE(drop_prob, 1.0);
- drop_prob_ = drop_prob;
- }
-
-
-
-
-
-
-
-
- void SetSendingBlocked(bool blocked);
-
- Socket* CreateSocket(int family, int type) override;
- AsyncSocket* CreateAsyncSocket(int family, int type) override;
-
- void SetMessageQueue(Thread* queue) override;
- bool Wait(int cms, bool process_io) override;
- void WakeUp() override;
- void SetDelayOnAddress(const rtc::SocketAddress& address, int delay_ms) {
- delay_by_ip_[address.ipaddr()] = delay_ms;
- }
-
-
-
-
-
-
-
- void SetAlternativeLocalAddress(const rtc::IPAddress& address,
- const rtc::IPAddress& alternative);
- typedef std::pair<double, double> Point;
- typedef std::vector<Point> Function;
- static Function* CreateDistribution(uint32_t mean,
- uint32_t stddev,
- uint32_t samples);
-
-
-
- bool ProcessMessagesUntilIdle();
-
- void SetNextPortForTesting(uint16_t port);
-
-
- bool CloseTcpConnections(const SocketAddress& addr_local,
- const SocketAddress& addr_remote);
-
-
- uint32_t sent_packets() const { return sent_packets_; }
-
- sigslot::signal1<VirtualSocket*> SignalSocketCreated;
- protected:
-
- IPAddress GetNextIP(int family);
- uint16_t GetNextPort();
- VirtualSocket* CreateSocketInternal(int family, int type);
-
- int Bind(VirtualSocket* socket, SocketAddress* addr);
-
- int Bind(VirtualSocket* socket, const SocketAddress& addr);
-
- VirtualSocket* LookupBinding(const SocketAddress& addr);
- int Unbind(const SocketAddress& addr, VirtualSocket* socket);
-
- void AddConnection(const SocketAddress& client,
- const SocketAddress& server,
- VirtualSocket* socket);
-
- VirtualSocket* LookupConnection(const SocketAddress& client,
- const SocketAddress& server);
- void RemoveConnection(const SocketAddress& client,
- const SocketAddress& server);
-
- int Connect(VirtualSocket* socket,
- const SocketAddress& remote_addr,
- bool use_delay);
-
- bool Disconnect(VirtualSocket* socket);
-
- int SendUdp(VirtualSocket* socket,
- const char* data,
- size_t data_size,
- const SocketAddress& remote_addr);
-
- void SendTcp(VirtualSocket* socket);
-
- void AddPacketToNetwork(VirtualSocket* socket,
- VirtualSocket* recipient,
- int64_t cur_time,
- const char* data,
- size_t data_size,
- size_t header_size,
- bool ordered);
-
- void PurgeNetworkPackets(VirtualSocket* socket, int64_t cur_time);
-
- uint32_t SendDelay(uint32_t size);
-
-
-
- uint32_t GetTransitDelay(Socket* socket);
-
-
- static Function* Accumulate(Function* f);
- static Function* Invert(Function* f);
- static Function* Resample(Function* f,
- double x1,
- double x2,
- uint32_t samples);
- static double Evaluate(Function* f, double x);
-
-
-
- void OnMessageQueueDestroyed() { msg_queue_ = nullptr; }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote);
- private:
- friend class VirtualSocket;
-
- sigslot::signal0<> SignalReadyToSend;
- typedef std::map<SocketAddress, VirtualSocket*> AddressMap;
- typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap;
-
-
- ThreadProcessingFakeClock* fake_clock_ = nullptr;
-
- Event wakeup_;
- Thread* msg_queue_;
- bool stop_on_idle_;
- in_addr next_ipv4_;
- in6_addr next_ipv6_;
- uint16_t next_port_;
- AddressMap* bindings_;
- ConnectionMap* connections_;
- IPAddress default_route_v4_;
- IPAddress default_route_v6_;
- uint32_t bandwidth_;
- uint32_t network_capacity_;
- uint32_t send_buffer_capacity_;
- uint32_t recv_buffer_capacity_;
- uint32_t delay_mean_;
- uint32_t delay_stddev_;
- uint32_t delay_samples_;
-
- uint32_t sent_packets_ = 0;
- std::map<rtc::IPAddress, int> delay_by_ip_;
- std::map<rtc::IPAddress, rtc::IPAddress> alternative_address_mapping_;
- std::unique_ptr<Function> delay_dist_;
- RecursiveCriticalSection delay_crit_;
- double drop_prob_;
- bool sending_blocked_ = false;
- RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer);
- };
- class VirtualSocket : public AsyncSocket,
- public MessageHandlerAutoCleanup,
- public sigslot::has_slots<> {
- public:
- VirtualSocket(VirtualSocketServer* server, int family, int type, bool async);
- ~VirtualSocket() override;
- SocketAddress GetLocalAddress() const override;
- SocketAddress GetRemoteAddress() const override;
- int Bind(const SocketAddress& addr) override;
- int Connect(const SocketAddress& addr) override;
- int Close() override;
- int Send(const void* pv, size_t cb) override;
- int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
- int Recv(void* pv, size_t cb, int64_t* timestamp) override;
- int RecvFrom(void* pv,
- size_t cb,
- SocketAddress* paddr,
- int64_t* timestamp) override;
- int Listen(int backlog) override;
- VirtualSocket* Accept(SocketAddress* paddr) override;
- int GetError() const override;
- void SetError(int error) override;
- ConnState GetState() const override;
- int GetOption(Option opt, int* value) override;
- int SetOption(Option opt, int value) override;
- void OnMessage(Message* pmsg) override;
- bool was_any() { return was_any_; }
- void set_was_any(bool was_any) { was_any_ = was_any; }
-
- sigslot::signal2<VirtualSocket*, const SocketAddress&> SignalAddressReady;
- private:
- struct NetworkEntry {
- size_t size;
- int64_t done_time;
- };
- typedef std::deque<SocketAddress> ListenQueue;
- typedef std::deque<NetworkEntry> NetworkQueue;
- typedef std::vector<char> SendBuffer;
- typedef std::list<Packet*> RecvBuffer;
- typedef std::map<Option, int> OptionsMap;
- int InitiateConnect(const SocketAddress& addr, bool use_delay);
- void CompleteConnect(const SocketAddress& addr, bool notify);
- int SendUdp(const void* pv, size_t cb, const SocketAddress& addr);
- int SendTcp(const void* pv, size_t cb);
-
- void SetLocalAddress(const SocketAddress& addr);
- void OnSocketServerReadyToSend();
- VirtualSocketServer* server_;
- int type_;
- bool async_;
- ConnState state_;
- int error_;
- SocketAddress local_addr_;
- SocketAddress remote_addr_;
-
- ListenQueue* listen_queue_;
-
- SendBuffer send_buffer_;
-
-
- bool ready_to_send_ = true;
-
- RecursiveCriticalSection crit_;
-
- NetworkQueue network_;
- size_t network_size_;
-
-
- int64_t last_delivery_time_ = 0;
-
- RecvBuffer recv_buffer_;
-
- size_t recv_buffer_size_;
-
- bool bound_;
-
-
-
-
- bool was_any_;
-
- OptionsMap options_map_;
- friend class VirtualSocketServer;
- };
- }
- #endif
|