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 | |
| 11 | #ifndef WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |
| 12 | #define WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |
| 13 | |
| 14 | #include <assert.h> |
| 15 | |
| 16 | #include <deque> |
| 17 | #include <map> |
| 18 | |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 19 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | #include "webrtc/base/messagequeue.h" |
| 21 | #include "webrtc/base/socketserver.h" |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 25 | class Packet; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | class VirtualSocket; |
| 27 | class SocketAddressPair; |
| 28 | |
| 29 | // Simulates a network in the same manner as a loopback interface. The |
| 30 | // interface can create as many addresses as you want. All of the sockets |
| 31 | // created by this network will be able to communicate with one another, unless |
| 32 | // they are bound to addresses from incompatible families. |
| 33 | class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> { |
| 34 | public: |
| 35 | // TODO: Add "owned" parameter. |
| 36 | // If "owned" is set, the supplied socketserver will be deleted later. |
| 37 | explicit VirtualSocketServer(SocketServer* ss); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 38 | ~VirtualSocketServer() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | |
| 40 | SocketServer* socketserver() { return server_; } |
| 41 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 42 | // The default route indicates which local address to use when a socket is |
| 43 | // bound to the 'any' address, e.g. 0.0.0.0. |
| 44 | IPAddress GetDefaultRoute(int family); |
| 45 | void SetDefaultRoute(const IPAddress& from_addr); |
| 46 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | // Limits the network bandwidth (maximum bytes per second). Zero means that |
| 48 | // all sends occur instantly. Defaults to 0. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | uint32_t bandwidth() const { return bandwidth_; } |
| 50 | void set_bandwidth(uint32_t bandwidth) { bandwidth_ = bandwidth; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | |
| 52 | // Limits the amount of data which can be in flight on the network without |
| 53 | // packet loss (on a per sender basis). Defaults to 64 KB. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 54 | uint32_t network_capacity() const { return network_capacity_; } |
| 55 | void set_network_capacity(uint32_t capacity) { network_capacity_ = capacity; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | |
| 57 | // The amount of data which can be buffered by tcp on the sender's side |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 58 | uint32_t send_buffer_capacity() const { return send_buffer_capacity_; } |
| 59 | void set_send_buffer_capacity(uint32_t capacity) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | send_buffer_capacity_ = capacity; |
| 61 | } |
| 62 | |
| 63 | // The amount of data which can be buffered by tcp on the receiver's side |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 64 | uint32_t recv_buffer_capacity() const { return recv_buffer_capacity_; } |
| 65 | void set_recv_buffer_capacity(uint32_t capacity) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | recv_buffer_capacity_ = capacity; |
| 67 | } |
| 68 | |
| 69 | // Controls the (transit) delay for packets sent in the network. This does |
| 70 | // not inclue the time required to sit in the send queue. Both of these |
| 71 | // values are measured in milliseconds. Defaults to no delay. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 72 | uint32_t delay_mean() const { return delay_mean_; } |
| 73 | uint32_t delay_stddev() const { return delay_stddev_; } |
| 74 | uint32_t delay_samples() const { return delay_samples_; } |
| 75 | void set_delay_mean(uint32_t delay_mean) { delay_mean_ = delay_mean; } |
| 76 | void set_delay_stddev(uint32_t delay_stddev) { delay_stddev_ = delay_stddev; } |
| 77 | void set_delay_samples(uint32_t delay_samples) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | delay_samples_ = delay_samples; |
| 79 | } |
| 80 | |
| 81 | // If the (transit) delay parameters are modified, this method should be |
| 82 | // called to recompute the new distribution. |
| 83 | void UpdateDelayDistribution(); |
| 84 | |
| 85 | // Controls the (uniform) probability that any sent packet is dropped. This |
| 86 | // is separate from calculations to drop based on queue size. |
| 87 | double drop_probability() { return drop_prob_; } |
| 88 | void set_drop_probability(double drop_prob) { |
| 89 | assert((0 <= drop_prob) && (drop_prob <= 1)); |
| 90 | drop_prob_ = drop_prob; |
| 91 | } |
| 92 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 93 | // If |blocked| is true, subsequent attempts to send will result in -1 being |
| 94 | // returned, with the socket error set to EWOULDBLOCK. |
| 95 | // |
| 96 | // If this method is later called with |blocked| set to false, any sockets |
| 97 | // that previously failed to send with EWOULDBLOCK will emit SignalWriteEvent. |
| 98 | // |
| 99 | // This can be used to simulate the send buffer on a network interface being |
| 100 | // full, and test functionality related to EWOULDBLOCK/SignalWriteEvent. |
| 101 | void SetSendingBlocked(bool blocked); |
| 102 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | // SocketFactory: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 104 | Socket* CreateSocket(int type) override; |
| 105 | Socket* CreateSocket(int family, int type) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 107 | AsyncSocket* CreateAsyncSocket(int type) override; |
| 108 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | |
| 110 | // SocketServer: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 111 | void SetMessageQueue(MessageQueue* queue) override; |
| 112 | bool Wait(int cms, bool process_io) override; |
| 113 | void WakeUp() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | |
| 115 | typedef std::pair<double, double> Point; |
| 116 | typedef std::vector<Point> Function; |
| 117 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 118 | static Function* CreateDistribution(uint32_t mean, |
| 119 | uint32_t stddev, |
| 120 | uint32_t samples); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | |
| 122 | // Similar to Thread::ProcessMessages, but it only processes messages until |
| 123 | // there are no immediate messages or pending network traffic. Returns false |
| 124 | // if Thread::Stop() was called. |
| 125 | bool ProcessMessagesUntilIdle(); |
| 126 | |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 127 | // Sets the next port number to use for testing. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 128 | void SetNextPortForTesting(uint16_t port); |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 129 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 130 | // Close a pair of Tcp connections by addresses. Both connections will have |
| 131 | // its own OnClose invoked. |
| 132 | bool CloseTcpConnections(const SocketAddress& addr_local, |
| 133 | const SocketAddress& addr_remote); |
| 134 | |
tommi | 5ce1a2a | 2016-05-14 03:19:31 -0700 | [diff] [blame] | 135 | // For testing purpose only. Fired when a client socket is created. |
| 136 | sigslot::signal1<VirtualSocket*> SignalSocketCreated; |
| 137 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 138 | protected: |
| 139 | // Returns a new IP not used before in this network. |
| 140 | IPAddress GetNextIP(int family); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 141 | uint16_t GetNextPort(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | |
| 143 | VirtualSocket* CreateSocketInternal(int family, int type); |
| 144 | |
| 145 | // Binds the given socket to addr, assigning and IP and Port if necessary |
| 146 | int Bind(VirtualSocket* socket, SocketAddress* addr); |
| 147 | |
| 148 | // Binds the given socket to the given (fully-defined) address. |
| 149 | int Bind(VirtualSocket* socket, const SocketAddress& addr); |
| 150 | |
| 151 | // Find the socket bound to the given address |
| 152 | VirtualSocket* LookupBinding(const SocketAddress& addr); |
| 153 | |
| 154 | int Unbind(const SocketAddress& addr, VirtualSocket* socket); |
| 155 | |
| 156 | // Adds a mapping between this socket pair and the socket. |
| 157 | void AddConnection(const SocketAddress& client, |
| 158 | const SocketAddress& server, |
| 159 | VirtualSocket* socket); |
| 160 | |
| 161 | // Find the socket pair corresponding to this server address. |
| 162 | VirtualSocket* LookupConnection(const SocketAddress& client, |
| 163 | const SocketAddress& server); |
| 164 | |
| 165 | void RemoveConnection(const SocketAddress& client, |
| 166 | const SocketAddress& server); |
| 167 | |
| 168 | // Connects the given socket to the socket at the given address |
| 169 | int Connect(VirtualSocket* socket, const SocketAddress& remote_addr, |
| 170 | bool use_delay); |
| 171 | |
| 172 | // Sends a disconnect message to the socket at the given address |
| 173 | bool Disconnect(VirtualSocket* socket); |
| 174 | |
| 175 | // Sends the given packet to the socket at the given address (if one exists). |
| 176 | int SendUdp(VirtualSocket* socket, const char* data, size_t data_size, |
| 177 | const SocketAddress& remote_addr); |
| 178 | |
| 179 | // Moves as much data as possible from the sender's buffer to the network |
| 180 | void SendTcp(VirtualSocket* socket); |
| 181 | |
| 182 | // Places a packet on the network. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 183 | void AddPacketToNetwork(VirtualSocket* socket, |
| 184 | VirtualSocket* recipient, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 185 | int64_t cur_time, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 186 | const char* data, |
| 187 | size_t data_size, |
| 188 | size_t header_size, |
| 189 | bool ordered); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | |
| 191 | // Removes stale packets from the network |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 192 | void PurgeNetworkPackets(VirtualSocket* socket, int64_t cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | |
| 194 | // Computes the number of milliseconds required to send a packet of this size. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 195 | uint32_t SendDelay(uint32_t size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 196 | |
| 197 | // Returns a random transit delay chosen from the appropriate distribution. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 198 | uint32_t GetRandomTransitDelay(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 199 | |
| 200 | // Basic operations on functions. Those that return a function also take |
| 201 | // ownership of the function given (and hence, may modify or delete it). |
| 202 | static Function* Accumulate(Function* f); |
| 203 | static Function* Invert(Function* f); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 204 | static Function* Resample(Function* f, |
| 205 | double x1, |
| 206 | double x2, |
| 207 | uint32_t samples); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | static double Evaluate(Function* f, double x); |
| 209 | |
| 210 | // NULL out our message queue if it goes away. Necessary in the case where |
| 211 | // our lifetime is greater than that of the thread we are using, since we |
| 212 | // try to send Close messages for all connected sockets when we shutdown. |
| 213 | void OnMessageQueueDestroyed() { msg_queue_ = NULL; } |
| 214 | |
| 215 | // Determine if two sockets should be able to communicate. |
| 216 | // We don't (currently) specify an address family for sockets; instead, |
| 217 | // the currently bound address is used to infer the address family. |
| 218 | // Any socket that is not explicitly bound to an IPv4 address is assumed to be |
| 219 | // dual-stack capable. |
| 220 | // This function tests if two addresses can communicate, as well as the |
| 221 | // sockets to which they may be bound (the addresses may or may not yet be |
| 222 | // bound to the sockets). |
| 223 | // First the addresses are tested (after normalization): |
| 224 | // If both have the same family, then communication is OK. |
| 225 | // If only one is IPv4 then false, unless the other is bound to ::. |
| 226 | // This applies even if the IPv4 address is 0.0.0.0. |
| 227 | // The socket arguments are optional; the sockets are checked to see if they |
| 228 | // were explicitly bound to IPv6-any ('::'), and if so communication is |
| 229 | // permitted. |
| 230 | // NB: This scheme doesn't permit non-dualstack IPv6 sockets. |
| 231 | static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); |
| 232 | |
| 233 | private: |
| 234 | friend class VirtualSocket; |
| 235 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 236 | // Sending was previously blocked, but now isn't. |
| 237 | sigslot::signal0<> SignalReadyToSend; |
| 238 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | typedef std::map<SocketAddress, VirtualSocket*> AddressMap; |
| 240 | typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; |
| 241 | |
| 242 | SocketServer* server_; |
| 243 | bool server_owned_; |
| 244 | MessageQueue* msg_queue_; |
| 245 | bool stop_on_idle_; |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 246 | int64_t network_delay_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | in_addr next_ipv4_; |
| 248 | in6_addr next_ipv6_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 249 | uint16_t next_port_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | AddressMap* bindings_; |
| 251 | ConnectionMap* connections_; |
| 252 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 253 | IPAddress default_route_v4_; |
| 254 | IPAddress default_route_v6_; |
| 255 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 256 | uint32_t bandwidth_; |
| 257 | uint32_t network_capacity_; |
| 258 | uint32_t send_buffer_capacity_; |
| 259 | uint32_t recv_buffer_capacity_; |
| 260 | uint32_t delay_mean_; |
| 261 | uint32_t delay_stddev_; |
| 262 | uint32_t delay_samples_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | Function* delay_dist_; |
| 264 | CriticalSection delay_crit_; |
| 265 | |
| 266 | double drop_prob_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 267 | bool sending_blocked_ = false; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 268 | RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 271 | // Implements the socket interface using the virtual network. Packets are |
| 272 | // passed as messages using the message queue of the socket server. |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 273 | class VirtualSocket : public AsyncSocket, |
| 274 | public MessageHandler, |
| 275 | public sigslot::has_slots<> { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 276 | public: |
| 277 | VirtualSocket(VirtualSocketServer* server, int family, int type, bool async); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 278 | ~VirtualSocket() override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 279 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 280 | SocketAddress GetLocalAddress() const override; |
| 281 | SocketAddress GetRemoteAddress() const override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 282 | |
guoweis@webrtc.org | 4fba293 | 2014-12-18 04:45:05 +0000 | [diff] [blame] | 283 | // Used by TurnPortTest to mimic a case where proxy returns local host address |
| 284 | // instead of the original one TurnPort was bound against. Please see WebRTC |
| 285 | // issue 3927 for more detail. |
| 286 | void SetAlternativeLocalAddress(const SocketAddress& addr); |
| 287 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 288 | int Bind(const SocketAddress& addr) override; |
| 289 | int Connect(const SocketAddress& addr) override; |
| 290 | int Close() override; |
| 291 | int Send(const void* pv, size_t cb) override; |
| 292 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 293 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 294 | int RecvFrom(void* pv, |
| 295 | size_t cb, |
| 296 | SocketAddress* paddr, |
| 297 | int64_t* timestamp) override; |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 298 | int Listen(int backlog) override; |
| 299 | VirtualSocket* Accept(SocketAddress* paddr) override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 300 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 301 | int GetError() const override; |
| 302 | void SetError(int error) override; |
| 303 | ConnState GetState() const override; |
| 304 | int GetOption(Option opt, int* value) override; |
| 305 | int SetOption(Option opt, int value) override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 306 | int EstimateMTU(uint16_t* mtu) override; |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 307 | void OnMessage(Message* pmsg) override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 308 | |
| 309 | bool was_any() { return was_any_; } |
| 310 | void set_was_any(bool was_any) { was_any_ = was_any; } |
| 311 | |
| 312 | // For testing purpose only. Fired when client socket is bound to an address. |
| 313 | sigslot::signal2<VirtualSocket*, const SocketAddress&> SignalAddressReady; |
| 314 | |
| 315 | private: |
| 316 | struct NetworkEntry { |
| 317 | size_t size; |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 318 | int64_t done_time; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | typedef std::deque<SocketAddress> ListenQueue; |
| 322 | typedef std::deque<NetworkEntry> NetworkQueue; |
| 323 | typedef std::vector<char> SendBuffer; |
| 324 | typedef std::list<Packet*> RecvBuffer; |
| 325 | typedef std::map<Option, int> OptionsMap; |
| 326 | |
| 327 | int InitiateConnect(const SocketAddress& addr, bool use_delay); |
| 328 | void CompleteConnect(const SocketAddress& addr, bool notify); |
| 329 | int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); |
| 330 | int SendTcp(const void* pv, size_t cb); |
| 331 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 332 | // Used by server sockets to set the local address without binding. |
| 333 | void SetLocalAddress(const SocketAddress& addr); |
| 334 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 335 | void OnSocketServerReadyToSend(); |
| 336 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 337 | VirtualSocketServer* server_; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 338 | int type_; |
| 339 | bool async_; |
| 340 | ConnState state_; |
| 341 | int error_; |
| 342 | SocketAddress local_addr_; |
| 343 | SocketAddress alternative_local_addr_; |
| 344 | SocketAddress remote_addr_; |
| 345 | |
| 346 | // Pending sockets which can be Accepted |
| 347 | ListenQueue* listen_queue_; |
| 348 | |
| 349 | // Data which tcp has buffered for sending |
| 350 | SendBuffer send_buffer_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame^] | 351 | // Set to false if the last attempt to send resulted in EWOULDBLOCK. |
| 352 | // Set back to true when the socket can send again. |
| 353 | bool ready_to_send_ = true; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 354 | |
| 355 | // Critical section to protect the recv_buffer and queue_ |
| 356 | CriticalSection crit_; |
| 357 | |
| 358 | // Network model that enforces bandwidth and capacity constraints |
| 359 | NetworkQueue network_; |
| 360 | size_t network_size_; |
| 361 | |
| 362 | // Data which has been received from the network |
| 363 | RecvBuffer recv_buffer_; |
| 364 | // The amount of data which is in flight or in recv_buffer_ |
| 365 | size_t recv_buffer_size_; |
| 366 | |
| 367 | // Is this socket bound? |
| 368 | bool bound_; |
| 369 | |
| 370 | // When we bind a socket to Any, VSS's Bind gives it another address. For |
| 371 | // dual-stack sockets, we want to distinguish between sockets that were |
| 372 | // explicitly given a particular address and sockets that had one picked |
| 373 | // for them by VSS. |
| 374 | bool was_any_; |
| 375 | |
| 376 | // Store the options that are set |
| 377 | OptionsMap options_map_; |
| 378 | |
| 379 | friend class VirtualSocketServer; |
| 380 | }; |
| 381 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 382 | } // namespace rtc |
| 383 | |
| 384 | #endif // WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |