You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
9.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_VIRTUALSOCKETSERVER_H_
  28. #define TALK_BASE_VIRTUALSOCKETSERVER_H_
  29. #include <cassert>
  30. #include <deque>
  31. #include <map>
  32. #include "talk/base/messagequeue.h"
  33. #include "talk/base/socketserver.h"
  34. namespace talk_base {
  35. class VirtualSocket;
  36. class SocketAddressPair;
  37. // Simulates a network in the same manner as a loopback interface. The
  38. // interface can create as many addresses as you want. All of the sockets
  39. // created by this network will be able to communicate with one another, unless
  40. // they are bound to addresses from incompatible families.
  41. class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
  42. public:
  43. // TODO: Add "owned" parameter.
  44. // If "owned" is set, the supplied socketserver will be deleted later.
  45. explicit VirtualSocketServer(SocketServer* ss);
  46. virtual ~VirtualSocketServer();
  47. SocketServer* socketserver() { return server_; }
  48. // Limits the network bandwidth (maximum bytes per second). Zero means that
  49. // all sends occur instantly. Defaults to 0.
  50. uint32 bandwidth() const { return bandwidth_; }
  51. void set_bandwidth(uint32 bandwidth) { bandwidth_ = bandwidth; }
  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.
  54. uint32 network_capacity() const { return network_capacity_; }
  55. void set_network_capacity(uint32 capacity) {
  56. network_capacity_ = capacity;
  57. }
  58. // The amount of data which can be buffered by tcp on the sender's side
  59. uint32 send_buffer_capacity() const { return send_buffer_capacity_; }
  60. void set_send_buffer_capacity(uint32 capacity) {
  61. send_buffer_capacity_ = capacity;
  62. }
  63. // The amount of data which can be buffered by tcp on the receiver's side
  64. uint32 recv_buffer_capacity() const { return recv_buffer_capacity_; }
  65. void set_recv_buffer_capacity(uint32 capacity) {
  66. recv_buffer_capacity_ = capacity;
  67. }
  68. // Controls the (transit) delay for packets sent in the network. This does
  69. // not inclue the time required to sit in the send queue. Both of these
  70. // values are measured in milliseconds. Defaults to no delay.
  71. uint32 delay_mean() const { return delay_mean_; }
  72. uint32 delay_stddev() const { return delay_stddev_; }
  73. uint32 delay_samples() const { return delay_samples_; }
  74. void set_delay_mean(uint32 delay_mean) { delay_mean_ = delay_mean; }
  75. void set_delay_stddev(uint32 delay_stddev) {
  76. delay_stddev_ = delay_stddev;
  77. }
  78. void set_delay_samples(uint32 delay_samples) {
  79. delay_samples_ = delay_samples;
  80. }
  81. // If the (transit) delay parameters are modified, this method should be
  82. // called to recompute the new distribution.
  83. void UpdateDelayDistribution();
  84. // Controls the (uniform) probability that any sent packet is dropped. This
  85. // is separate from calculations to drop based on queue size.
  86. double drop_probability() { return drop_prob_; }
  87. void set_drop_probability(double drop_prob) {
  88. assert((0 <= drop_prob) && (drop_prob <= 1));
  89. drop_prob_ = drop_prob;
  90. }
  91. // SocketFactory:
  92. virtual Socket* CreateSocket(int type);
  93. virtual Socket* CreateSocket(int family, int type);
  94. virtual AsyncSocket* CreateAsyncSocket(int type);
  95. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  96. // SocketServer:
  97. virtual void SetMessageQueue(MessageQueue* queue);
  98. virtual bool Wait(int cms, bool process_io);
  99. virtual void WakeUp();
  100. typedef std::pair<double, double> Point;
  101. typedef std::vector<Point> Function;
  102. static Function* CreateDistribution(uint32 mean, uint32 stddev,
  103. uint32 samples);
  104. // Similar to Thread::ProcessMessages, but it only processes messages until
  105. // there are no immediate messages or pending network traffic. Returns false
  106. // if Thread::Stop() was called.
  107. bool ProcessMessagesUntilIdle();
  108. protected:
  109. // Returns a new IP not used before in this network.
  110. IPAddress GetNextIP(int family);
  111. uint16 GetNextPort();
  112. VirtualSocket* CreateSocketInternal(int family, int type);
  113. // Binds the given socket to addr, assigning and IP and Port if necessary
  114. int Bind(VirtualSocket* socket, SocketAddress* addr);
  115. // Binds the given socket to the given (fully-defined) address.
  116. int Bind(VirtualSocket* socket, const SocketAddress& addr);
  117. // Find the socket bound to the given address
  118. VirtualSocket* LookupBinding(const SocketAddress& addr);
  119. int Unbind(const SocketAddress& addr, VirtualSocket* socket);
  120. // Adds a mapping between this socket pair and the socket.
  121. void AddConnection(const SocketAddress& client,
  122. const SocketAddress& server,
  123. VirtualSocket* socket);
  124. // Find the socket pair corresponding to this server address.
  125. VirtualSocket* LookupConnection(const SocketAddress& client,
  126. const SocketAddress& server);
  127. void RemoveConnection(const SocketAddress& client,
  128. const SocketAddress& server);
  129. // Connects the given socket to the socket at the given address
  130. int Connect(VirtualSocket* socket, const SocketAddress& remote_addr,
  131. bool use_delay);
  132. // Sends a disconnect message to the socket at the given address
  133. bool Disconnect(VirtualSocket* socket);
  134. // Sends the given packet to the socket at the given address (if one exists).
  135. int SendUdp(VirtualSocket* socket, const char* data, size_t data_size,
  136. const SocketAddress& remote_addr);
  137. // Moves as much data as possible from the sender's buffer to the network
  138. void SendTcp(VirtualSocket* socket);
  139. // Places a packet on the network.
  140. void AddPacketToNetwork(VirtualSocket* socket, VirtualSocket* recipient,
  141. uint32 cur_time, const char* data, size_t data_size,
  142. size_t header_size, bool ordered);
  143. // Removes stale packets from the network
  144. void PurgeNetworkPackets(VirtualSocket* socket, uint32 cur_time);
  145. // Computes the number of milliseconds required to send a packet of this size.
  146. uint32 SendDelay(uint32 size);
  147. // Returns a random transit delay chosen from the appropriate distribution.
  148. uint32 GetRandomTransitDelay();
  149. // Basic operations on functions. Those that return a function also take
  150. // ownership of the function given (and hence, may modify or delete it).
  151. static Function* Accumulate(Function* f);
  152. static Function* Invert(Function* f);
  153. static Function* Resample(Function* f, double x1, double x2, uint32 samples);
  154. static double Evaluate(Function* f, double x);
  155. // NULL out our message queue if it goes away. Necessary in the case where
  156. // our lifetime is greater than that of the thread we are using, since we
  157. // try to send Close messages for all connected sockets when we shutdown.
  158. void OnMessageQueueDestroyed() { msg_queue_ = NULL; }
  159. // Determine if two sockets should be able to communicate.
  160. // We don't (currently) specify an address family for sockets; instead,
  161. // the currently bound address is used to infer the address family.
  162. // Any socket that is not explicitly bound to an IPv4 address is assumed to be
  163. // dual-stack capable.
  164. // This function tests if two addresses can communicate, as well as the
  165. // sockets to which they may be bound (the addresses may or may not yet be
  166. // bound to the sockets).
  167. // First the addresses are tested (after normalization):
  168. // If both have the same family, then communication is OK.
  169. // If only one is IPv4 then false, unless the other is bound to ::.
  170. // This applies even if the IPv4 address is 0.0.0.0.
  171. // The socket arguments are optional; the sockets are checked to see if they
  172. // were explicitly bound to IPv6-any ('::'), and if so communication is
  173. // permitted.
  174. // NB: This scheme doesn't permit non-dualstack IPv6 sockets.
  175. static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote);
  176. private:
  177. friend class VirtualSocket;
  178. typedef std::map<SocketAddress, VirtualSocket*> AddressMap;
  179. typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap;
  180. SocketServer* server_;
  181. bool server_owned_;
  182. MessageQueue* msg_queue_;
  183. bool stop_on_idle_;
  184. uint32 network_delay_;
  185. in_addr next_ipv4_;
  186. in6_addr next_ipv6_;
  187. uint16 next_port_;
  188. AddressMap* bindings_;
  189. ConnectionMap* connections_;
  190. uint32 bandwidth_;
  191. uint32 network_capacity_;
  192. uint32 send_buffer_capacity_;
  193. uint32 recv_buffer_capacity_;
  194. uint32 delay_mean_;
  195. uint32 delay_stddev_;
  196. uint32 delay_samples_;
  197. Function* delay_dist_;
  198. CriticalSection delay_crit_;
  199. double drop_prob_;
  200. DISALLOW_EVIL_CONSTRUCTORS(VirtualSocketServer);
  201. };
  202. } // namespace talk_base
  203. #endif // TALK_BASE_VIRTUALSOCKETSERVER_H_