henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |
| 12 | #define RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <deque> |
| 15 | #include <map> |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 16 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/event.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/fake_clock.h" |
Sebastian Jansson | 4db28b5 | 2020-01-08 14:07:15 +0100 | [diff] [blame] | 22 | #include "rtc_base/message_handler.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/socket_server.h" |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 24 | #include "rtc_base/synchronization/mutex.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | |
| 26 | namespace rtc { |
| 27 | |
| 28 | class Packet; |
| 29 | class VirtualSocket; |
| 30 | class SocketAddressPair; |
| 31 | |
| 32 | // Simulates a network in the same manner as a loopback interface. The |
| 33 | // interface can create as many addresses as you want. All of the sockets |
| 34 | // created by this network will be able to communicate with one another, unless |
| 35 | // they are bound to addresses from incompatible families. |
Niels Möller | 9bd2457 | 2021-04-19 12:18:27 +0200 | [diff] [blame] | 36 | class VirtualSocketServer : public SocketServer { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 37 | public: |
| 38 | VirtualSocketServer(); |
| 39 | // This constructor needs to be used if the test uses a fake clock and |
| 40 | // ProcessMessagesUntilIdle, since ProcessMessagesUntilIdle needs a way of |
| 41 | // advancing time. |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 42 | explicit VirtualSocketServer(ThreadProcessingFakeClock* fake_clock); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | ~VirtualSocketServer() override; |
| 44 | |
| 45 | // The default route indicates which local address to use when a socket is |
| 46 | // bound to the 'any' address, e.g. 0.0.0.0. |
| 47 | IPAddress GetDefaultRoute(int family); |
| 48 | void SetDefaultRoute(const IPAddress& from_addr); |
| 49 | |
| 50 | // Limits the network bandwidth (maximum bytes per second). Zero means that |
| 51 | // all sends occur instantly. Defaults to 0. |
| 52 | uint32_t bandwidth() const { return bandwidth_; } |
| 53 | void set_bandwidth(uint32_t bandwidth) { bandwidth_ = bandwidth; } |
| 54 | |
| 55 | // Limits the amount of data which can be in flight on the network without |
| 56 | // packet loss (on a per sender basis). Defaults to 64 KB. |
| 57 | uint32_t network_capacity() const { return network_capacity_; } |
| 58 | void set_network_capacity(uint32_t capacity) { network_capacity_ = capacity; } |
| 59 | |
| 60 | // The amount of data which can be buffered by tcp on the sender's side |
| 61 | uint32_t send_buffer_capacity() const { return send_buffer_capacity_; } |
| 62 | void set_send_buffer_capacity(uint32_t capacity) { |
| 63 | send_buffer_capacity_ = capacity; |
| 64 | } |
| 65 | |
| 66 | // The amount of data which can be buffered by tcp on the receiver's side |
| 67 | uint32_t recv_buffer_capacity() const { return recv_buffer_capacity_; } |
| 68 | void set_recv_buffer_capacity(uint32_t capacity) { |
| 69 | recv_buffer_capacity_ = capacity; |
| 70 | } |
| 71 | |
| 72 | // Controls the (transit) delay for packets sent in the network. This does |
| 73 | // not inclue the time required to sit in the send queue. Both of these |
| 74 | // values are measured in milliseconds. Defaults to no delay. |
| 75 | uint32_t delay_mean() const { return delay_mean_; } |
| 76 | uint32_t delay_stddev() const { return delay_stddev_; } |
| 77 | uint32_t delay_samples() const { return delay_samples_; } |
| 78 | void set_delay_mean(uint32_t delay_mean) { delay_mean_ = delay_mean; } |
| 79 | void set_delay_stddev(uint32_t delay_stddev) { delay_stddev_ = delay_stddev; } |
| 80 | void set_delay_samples(uint32_t delay_samples) { |
| 81 | delay_samples_ = delay_samples; |
| 82 | } |
| 83 | |
| 84 | // If the (transit) delay parameters are modified, this method should be |
| 85 | // called to recompute the new distribution. |
| 86 | void UpdateDelayDistribution(); |
| 87 | |
| 88 | // Controls the (uniform) probability that any sent packet is dropped. This |
| 89 | // is separate from calculations to drop based on queue size. |
| 90 | double drop_probability() { return drop_prob_; } |
| 91 | void set_drop_probability(double drop_prob) { |
| 92 | RTC_DCHECK_GE(drop_prob, 0.0); |
| 93 | RTC_DCHECK_LE(drop_prob, 1.0); |
| 94 | drop_prob_ = drop_prob; |
| 95 | } |
| 96 | |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 97 | // Controls the maximum UDP payload for the networks simulated |
| 98 | // by this server. Any UDP payload sent that is larger than this will |
| 99 | // be dropped. |
| 100 | size_t max_udp_payload() { return max_udp_payload_; } |
| 101 | void set_max_udp_payload(size_t payload_size) { |
| 102 | max_udp_payload_ = payload_size; |
| 103 | } |
| 104 | |
| 105 | size_t largest_seen_udp_payload() { return largest_seen_udp_payload_; } |
| 106 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 107 | // If `blocked` is true, subsequent attempts to send will result in -1 being |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | // returned, with the socket error set to EWOULDBLOCK. |
| 109 | // |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 110 | // If this method is later called with `blocked` set to false, any sockets |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | // that previously failed to send with EWOULDBLOCK will emit SignalWriteEvent. |
| 112 | // |
| 113 | // This can be used to simulate the send buffer on a network interface being |
| 114 | // full, and test functionality related to EWOULDBLOCK/SignalWriteEvent. |
| 115 | void SetSendingBlocked(bool blocked); |
| 116 | |
| 117 | // SocketFactory: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 118 | Socket* CreateSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 120 | |
| 121 | // SocketServer: |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 122 | void SetMessageQueue(Thread* queue) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 123 | bool Wait(int cms, bool process_io) override; |
| 124 | void WakeUp() override; |
| 125 | |
| 126 | void SetDelayOnAddress(const rtc::SocketAddress& address, int delay_ms) { |
| 127 | delay_by_ip_[address.ipaddr()] = delay_ms; |
| 128 | } |
| 129 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 130 | // Used by TurnPortTest and TcpPortTest (for example), to mimic a case where |
| 131 | // a proxy returns the local host address instead of the original one the |
| 132 | // port was bound against. Please see WebRTC issue 3927 for more detail. |
| 133 | // |
| 134 | // If SetAlternativeLocalAddress(A, B) is called, then when something |
| 135 | // attempts to bind a socket to address A, it will get a socket bound to |
| 136 | // address B instead. |
| 137 | void SetAlternativeLocalAddress(const rtc::IPAddress& address, |
| 138 | const rtc::IPAddress& alternative); |
| 139 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 140 | typedef std::pair<double, double> Point; |
| 141 | typedef std::vector<Point> Function; |
| 142 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 143 | static std::unique_ptr<Function> CreateDistribution(uint32_t mean, |
| 144 | uint32_t stddev, |
| 145 | uint32_t samples); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 146 | |
| 147 | // Similar to Thread::ProcessMessages, but it only processes messages until |
| 148 | // there are no immediate messages or pending network traffic. Returns false |
| 149 | // if Thread::Stop() was called. |
| 150 | bool ProcessMessagesUntilIdle(); |
| 151 | |
| 152 | // Sets the next port number to use for testing. |
| 153 | void SetNextPortForTesting(uint16_t port); |
| 154 | |
| 155 | // Close a pair of Tcp connections by addresses. Both connections will have |
| 156 | // its own OnClose invoked. |
| 157 | bool CloseTcpConnections(const SocketAddress& addr_local, |
| 158 | const SocketAddress& addr_remote); |
| 159 | |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 160 | // Number of packets that clients have attempted to send through this virtual |
| 161 | // socket server. Intended to be used for test assertions. |
| 162 | uint32_t sent_packets() const { return sent_packets_; } |
| 163 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 164 | // Binds the given socket to addr, assigning and IP and Port if necessary |
| 165 | int Bind(VirtualSocket* socket, SocketAddress* addr); |
| 166 | |
| 167 | // Binds the given socket to the given (fully-defined) address. |
| 168 | int Bind(VirtualSocket* socket, const SocketAddress& addr); |
| 169 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 170 | int Unbind(const SocketAddress& addr, VirtualSocket* socket); |
| 171 | |
| 172 | // Adds a mapping between this socket pair and the socket. |
| 173 | void AddConnection(const SocketAddress& client, |
| 174 | const SocketAddress& server, |
| 175 | VirtualSocket* socket); |
| 176 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 177 | // Connects the given socket to the socket at the given address |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 178 | int Connect(VirtualSocket* socket, |
| 179 | const SocketAddress& remote_addr, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 180 | bool use_delay); |
| 181 | |
| 182 | // Sends a disconnect message to the socket at the given address |
| 183 | bool Disconnect(VirtualSocket* socket); |
| 184 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 185 | // Lookup address, and disconnect corresponding socket. |
| 186 | bool Disconnect(const SocketAddress& addr); |
| 187 | |
| 188 | // Lookup connection, close corresponding socket. |
| 189 | bool Disconnect(const SocketAddress& local_addr, |
| 190 | const SocketAddress& remote_addr); |
| 191 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 192 | // Sends the given packet to the socket at the given address (if one exists). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 193 | int SendUdp(VirtualSocket* socket, |
| 194 | const char* data, |
| 195 | size_t data_size, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 196 | const SocketAddress& remote_addr); |
| 197 | |
| 198 | // Moves as much data as possible from the sender's buffer to the network |
| 199 | void SendTcp(VirtualSocket* socket); |
| 200 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 201 | // Like above, but lookup sender by address. |
| 202 | void SendTcp(const SocketAddress& addr); |
| 203 | |
| 204 | // Computes the number of milliseconds required to send a packet of this size. |
| 205 | uint32_t SendDelay(uint32_t size); |
| 206 | |
| 207 | // Cancel attempts to connect to a socket that is being closed. |
| 208 | void CancelConnects(VirtualSocket* socket); |
| 209 | |
| 210 | // Clear incoming messages for a socket that is being closed. |
| 211 | void Clear(VirtualSocket* socket); |
| 212 | |
| 213 | void ProcessOneMessage(); |
| 214 | |
| 215 | void PostSignalReadEvent(VirtualSocket* socket); |
| 216 | |
| 217 | // Sending was previously blocked, but now isn't. |
| 218 | sigslot::signal0<> SignalReadyToSend; |
| 219 | |
| 220 | protected: |
| 221 | // Returns a new IP not used before in this network. |
| 222 | IPAddress GetNextIP(int family); |
| 223 | |
| 224 | // Find the socket bound to the given address |
| 225 | VirtualSocket* LookupBinding(const SocketAddress& addr); |
| 226 | |
| 227 | private: |
| 228 | uint16_t GetNextPort(); |
| 229 | |
| 230 | VirtualSocket* CreateSocketInternal(int family, int type); |
| 231 | |
| 232 | // Find the socket pair corresponding to this server address. |
| 233 | VirtualSocket* LookupConnection(const SocketAddress& client, |
| 234 | const SocketAddress& server); |
| 235 | |
| 236 | void RemoveConnection(const SocketAddress& client, |
| 237 | const SocketAddress& server); |
| 238 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 239 | // Places a packet on the network. |
| 240 | void AddPacketToNetwork(VirtualSocket* socket, |
| 241 | VirtualSocket* recipient, |
| 242 | int64_t cur_time, |
| 243 | const char* data, |
| 244 | size_t data_size, |
| 245 | size_t header_size, |
| 246 | bool ordered); |
| 247 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 248 | // If the delay has been set for the address of the socket, returns the set |
| 249 | // delay. Otherwise, returns a random transit delay chosen from the |
| 250 | // appropriate distribution. |
| 251 | uint32_t GetTransitDelay(Socket* socket); |
| 252 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 253 | // Basic operations on functions. |
| 254 | static std::unique_ptr<Function> Accumulate(std::unique_ptr<Function> f); |
| 255 | static std::unique_ptr<Function> Invert(std::unique_ptr<Function> f); |
| 256 | static std::unique_ptr<Function> Resample(std::unique_ptr<Function> f, |
| 257 | double x1, |
| 258 | double x2, |
| 259 | uint32_t samples); |
| 260 | static double Evaluate(const Function* f, double x); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 261 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 262 | // Determine if two sockets should be able to communicate. |
| 263 | // We don't (currently) specify an address family for sockets; instead, |
| 264 | // the currently bound address is used to infer the address family. |
| 265 | // Any socket that is not explicitly bound to an IPv4 address is assumed to be |
| 266 | // dual-stack capable. |
| 267 | // This function tests if two addresses can communicate, as well as the |
| 268 | // sockets to which they may be bound (the addresses may or may not yet be |
| 269 | // bound to the sockets). |
| 270 | // First the addresses are tested (after normalization): |
| 271 | // If both have the same family, then communication is OK. |
| 272 | // If only one is IPv4 then false, unless the other is bound to ::. |
| 273 | // This applies even if the IPv4 address is 0.0.0.0. |
| 274 | // The socket arguments are optional; the sockets are checked to see if they |
| 275 | // were explicitly bound to IPv6-any ('::'), and if so communication is |
| 276 | // permitted. |
| 277 | // NB: This scheme doesn't permit non-dualstack IPv6 sockets. |
| 278 | static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); |
| 279 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 280 | typedef std::map<SocketAddress, VirtualSocket*> AddressMap; |
| 281 | typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; |
| 282 | |
| 283 | // May be null if the test doesn't use a fake clock, or it does but doesn't |
| 284 | // use ProcessMessagesUntilIdle. |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 285 | ThreadProcessingFakeClock* fake_clock_ = nullptr; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 286 | |
| 287 | // Used to implement Wait/WakeUp. |
| 288 | Event wakeup_; |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 289 | Thread* msg_queue_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 290 | bool stop_on_idle_; |
| 291 | in_addr next_ipv4_; |
| 292 | in6_addr next_ipv6_; |
| 293 | uint16_t next_port_; |
| 294 | AddressMap* bindings_; |
| 295 | ConnectionMap* connections_; |
| 296 | |
| 297 | IPAddress default_route_v4_; |
| 298 | IPAddress default_route_v6_; |
| 299 | |
| 300 | uint32_t bandwidth_; |
| 301 | uint32_t network_capacity_; |
| 302 | uint32_t send_buffer_capacity_; |
| 303 | uint32_t recv_buffer_capacity_; |
| 304 | uint32_t delay_mean_; |
| 305 | uint32_t delay_stddev_; |
| 306 | uint32_t delay_samples_; |
| 307 | |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 308 | // Used for testing. |
| 309 | uint32_t sent_packets_ = 0; |
| 310 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 311 | std::map<rtc::IPAddress, int> delay_by_ip_; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 312 | std::map<rtc::IPAddress, rtc::IPAddress> alternative_address_mapping_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 313 | std::unique_ptr<Function> delay_dist_; |
| 314 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 315 | double drop_prob_; |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 316 | // The largest UDP payload permitted on this virtual socket server. |
| 317 | // The default is the max size of IPv4 fragmented UDP packet payload: |
| 318 | // 65535 bytes - 8 bytes UDP header - 20 bytes IP header. |
| 319 | size_t max_udp_payload_ = 65507; |
| 320 | // The largest UDP payload seen so far. |
| 321 | size_t largest_seen_udp_payload_ = 0; |
| 322 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 323 | bool sending_blocked_ = false; |
| 324 | RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer); |
| 325 | }; |
| 326 | |
| 327 | // Implements the socket interface using the virtual network. Packets are |
| 328 | // passed as messages using the message queue of the socket server. |
| 329 | class VirtualSocket : public AsyncSocket, |
Tomas Gunnarsson | d966347 | 2020-11-21 16:20:23 +0100 | [diff] [blame] | 330 | public MessageHandler, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 331 | public sigslot::has_slots<> { |
| 332 | public: |
| 333 | VirtualSocket(VirtualSocketServer* server, int family, int type, bool async); |
| 334 | ~VirtualSocket() override; |
| 335 | |
| 336 | SocketAddress GetLocalAddress() const override; |
| 337 | SocketAddress GetRemoteAddress() const override; |
| 338 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 339 | int Bind(const SocketAddress& addr) override; |
| 340 | int Connect(const SocketAddress& addr) override; |
| 341 | int Close() override; |
| 342 | int Send(const void* pv, size_t cb) override; |
| 343 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 344 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 345 | int RecvFrom(void* pv, |
| 346 | size_t cb, |
| 347 | SocketAddress* paddr, |
| 348 | int64_t* timestamp) override; |
| 349 | int Listen(int backlog) override; |
| 350 | VirtualSocket* Accept(SocketAddress* paddr) override; |
| 351 | |
| 352 | int GetError() const override; |
| 353 | void SetError(int error) override; |
| 354 | ConnState GetState() const override; |
| 355 | int GetOption(Option opt, int* value) override; |
| 356 | int SetOption(Option opt, int value) override; |
| 357 | void OnMessage(Message* pmsg) override; |
| 358 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 359 | size_t recv_buffer_size() const { return recv_buffer_size_; } |
| 360 | size_t send_buffer_size() const { return send_buffer_.size(); } |
| 361 | const char* send_buffer_data() const { return send_buffer_.data(); } |
| 362 | |
| 363 | // Used by server sockets to set the local address without binding. |
| 364 | void SetLocalAddress(const SocketAddress& addr); |
| 365 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 366 | bool was_any() { return was_any_; } |
| 367 | void set_was_any(bool was_any) { was_any_ = was_any; } |
| 368 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 369 | void SetToBlocked(); |
| 370 | |
| 371 | void UpdateRecv(size_t data_size); |
| 372 | void UpdateSend(size_t data_size); |
| 373 | |
| 374 | void MaybeSignalWriteEvent(size_t capacity); |
| 375 | |
| 376 | // Adds a packet to be sent. Returns delay, based on network_size_. |
| 377 | uint32_t AddPacket(int64_t cur_time, size_t packet_size); |
| 378 | |
| 379 | int64_t UpdateOrderedDelivery(int64_t ts); |
| 380 | |
| 381 | // Removes stale packets from the network. Returns current size. |
| 382 | size_t PurgeNetworkPackets(int64_t cur_time); |
| 383 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 384 | private: |
| 385 | struct NetworkEntry { |
| 386 | size_t size; |
| 387 | int64_t done_time; |
| 388 | }; |
| 389 | |
| 390 | typedef std::deque<SocketAddress> ListenQueue; |
| 391 | typedef std::deque<NetworkEntry> NetworkQueue; |
| 392 | typedef std::vector<char> SendBuffer; |
| 393 | typedef std::list<Packet*> RecvBuffer; |
| 394 | typedef std::map<Option, int> OptionsMap; |
| 395 | |
| 396 | int InitiateConnect(const SocketAddress& addr, bool use_delay); |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 397 | void CompleteConnect(const SocketAddress& addr); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 398 | int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); |
| 399 | int SendTcp(const void* pv, size_t cb); |
| 400 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 401 | void OnSocketServerReadyToSend(); |
| 402 | |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 403 | VirtualSocketServer* const server_; |
| 404 | const int type_; |
| 405 | const bool async_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 406 | ConnState state_; |
| 407 | int error_; |
| 408 | SocketAddress local_addr_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 409 | SocketAddress remote_addr_; |
| 410 | |
| 411 | // Pending sockets which can be Accepted |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 412 | std::unique_ptr<ListenQueue> listen_queue_ RTC_GUARDED_BY(mutex_) |
| 413 | RTC_PT_GUARDED_BY(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 414 | |
| 415 | // Data which tcp has buffered for sending |
| 416 | SendBuffer send_buffer_; |
| 417 | // Set to false if the last attempt to send resulted in EWOULDBLOCK. |
| 418 | // Set back to true when the socket can send again. |
| 419 | bool ready_to_send_ = true; |
| 420 | |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 421 | // Mutex to protect the recv_buffer and listen_queue_ |
| 422 | webrtc::Mutex mutex_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 423 | |
| 424 | // Network model that enforces bandwidth and capacity constraints |
| 425 | NetworkQueue network_; |
| 426 | size_t network_size_; |
| 427 | // The scheduled delivery time of the last packet sent on this socket. |
| 428 | // It is used to ensure ordered delivery of packets sent on this socket. |
| 429 | int64_t last_delivery_time_ = 0; |
| 430 | |
| 431 | // Data which has been received from the network |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 432 | RecvBuffer recv_buffer_ RTC_GUARDED_BY(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 433 | // The amount of data which is in flight or in recv_buffer_ |
| 434 | size_t recv_buffer_size_; |
| 435 | |
| 436 | // Is this socket bound? |
| 437 | bool bound_; |
| 438 | |
| 439 | // When we bind a socket to Any, VSS's Bind gives it another address. For |
| 440 | // dual-stack sockets, we want to distinguish between sockets that were |
| 441 | // explicitly given a particular address and sockets that had one picked |
| 442 | // for them by VSS. |
| 443 | bool was_any_; |
| 444 | |
| 445 | // Store the options that are set |
| 446 | OptionsMap options_map_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 450 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 451 | #endif // RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |