147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_VIRTUALSOCKETSERVER_H_
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_VIRTUALSOCKETSERVER_H_
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <assert.h>
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <deque>
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <map>
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/messagequeue.h"
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/socketserver.h"
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass VirtualSocket;
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass SocketAddressPair;
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Simulates a network in the same manner as a loopback interface.  The
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// interface can create as many addresses as you want.  All of the sockets
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// created by this network will be able to communicate with one another, unless
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// they are bound to addresses from incompatible families.
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // TODO: Add "owned" parameter.
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // If "owned" is set, the supplied socketserver will be deleted later.
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  explicit VirtualSocketServer(SocketServer* ss);
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual ~VirtualSocketServer();
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketServer* socketserver() { return server_; }
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Limits the network bandwidth (maximum bytes per second).  Zero means that
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // all sends occur instantly.  Defaults to 0.
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 bandwidth() const { return bandwidth_; }
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_bandwidth(uint32 bandwidth) { bandwidth_ = bandwidth; }
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Limits the amount of data which can be in flight on the network without
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // packet loss (on a per sender basis).  Defaults to 64 KB.
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 network_capacity() const { return network_capacity_; }
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_network_capacity(uint32 capacity) {
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    network_capacity_ = capacity;
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // The amount of data which can be buffered by tcp on the sender's side
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 send_buffer_capacity() const { return send_buffer_capacity_; }
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_send_buffer_capacity(uint32 capacity) {
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    send_buffer_capacity_ = capacity;
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // The amount of data which can be buffered by tcp on the receiver's side
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 recv_buffer_capacity() const { return recv_buffer_capacity_; }
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_recv_buffer_capacity(uint32 capacity) {
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    recv_buffer_capacity_ = capacity;
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Controls the (transit) delay for packets sent in the network.  This does
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // not inclue the time required to sit in the send queue.  Both of these
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // values are measured in milliseconds.  Defaults to no delay.
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_mean() const { return delay_mean_; }
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_stddev() const { return delay_stddev_; }
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_samples() const { return delay_samples_; }
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_delay_mean(uint32 delay_mean) { delay_mean_ = delay_mean; }
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_delay_stddev(uint32 delay_stddev) {
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    delay_stddev_ = delay_stddev;
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_delay_samples(uint32 delay_samples) {
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    delay_samples_ = delay_samples;
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // If the (transit) delay parameters are modified, this method should be
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // called to recompute the new distribution.
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void UpdateDelayDistribution();
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Controls the (uniform) probability that any sent packet is dropped.  This
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // is separate from calculations to drop based on queue size.
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  double drop_probability() { return drop_prob_; }
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void set_drop_probability(double drop_prob) {
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    assert((0 <= drop_prob) && (drop_prob <= 1));
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    drop_prob_ = drop_prob;
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // SocketFactory:
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int type);
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int family, int type);
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int type);
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // SocketServer:
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual void SetMessageQueue(MessageQueue* queue);
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool Wait(int cms, bool process_io);
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual void WakeUp();
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::pair<double, double> Point;
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::vector<Point> Function;
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static Function* CreateDistribution(uint32 mean, uint32 stddev,
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                      uint32 samples);
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Similar to Thread::ProcessMessages, but it only processes messages until
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // there are no immediate messages or pending network traffic.  Returns false
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // if Thread::Stop() was called.
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool ProcessMessagesUntilIdle();
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1130cdce7c07a77d724f5d7f5f85918b1a1fb9a7e62jiayl@webrtc.org  // Sets the next port number to use for testing.
1140cdce7c07a77d724f5d7f5f85918b1a1fb9a7e62jiayl@webrtc.org  void SetNextPortForTesting(uint16 port);
1150cdce7c07a77d724f5d7f5f85918b1a1fb9a7e62jiayl@webrtc.org
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org protected:
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Returns a new IP not used before in this network.
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  IPAddress GetNextIP(int family);
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint16 GetNextPort();
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  VirtualSocket* CreateSocketInternal(int family, int type);
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Binds the given socket to addr, assigning and IP and Port if necessary
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int Bind(VirtualSocket* socket, SocketAddress* addr);
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Binds the given socket to the given (fully-defined) address.
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int Bind(VirtualSocket* socket, const SocketAddress& addr);
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Find the socket bound to the given address
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  VirtualSocket* LookupBinding(const SocketAddress& addr);
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int Unbind(const SocketAddress& addr, VirtualSocket* socket);
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Adds a mapping between this socket pair and the socket.
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void AddConnection(const SocketAddress& client,
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                     const SocketAddress& server,
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                     VirtualSocket* socket);
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Find the socket pair corresponding to this server address.
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  VirtualSocket* LookupConnection(const SocketAddress& client,
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                  const SocketAddress& server);
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void RemoveConnection(const SocketAddress& client,
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                        const SocketAddress& server);
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Connects the given socket to the socket at the given address
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int Connect(VirtualSocket* socket, const SocketAddress& remote_addr,
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org              bool use_delay);
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Sends a disconnect message to the socket at the given address
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool Disconnect(VirtualSocket* socket);
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Sends the given packet to the socket at the given address (if one exists).
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int SendUdp(VirtualSocket* socket, const char* data, size_t data_size,
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org              const SocketAddress& remote_addr);
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Moves as much data as possible from the sender's buffer to the network
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void SendTcp(VirtualSocket* socket);
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Places a packet on the network.
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void AddPacketToNetwork(VirtualSocket* socket, VirtualSocket* recipient,
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                          uint32 cur_time, const char* data, size_t data_size,
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                          size_t header_size, bool ordered);
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Removes stale packets from the network
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void PurgeNetworkPackets(VirtualSocket* socket, uint32 cur_time);
16747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Computes the number of milliseconds required to send a packet of this size.
16947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 SendDelay(uint32 size);
17047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
17147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Returns a random transit delay chosen from the appropriate distribution.
17247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 GetRandomTransitDelay();
17347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
17447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Basic operations on functions.  Those that return a function also take
17547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // ownership of the function given (and hence, may modify or delete it).
17647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static Function* Accumulate(Function* f);
17747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static Function* Invert(Function* f);
17847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static Function* Resample(Function* f, double x1, double x2, uint32 samples);
17947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static double Evaluate(Function* f, double x);
18047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
18147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // NULL out our message queue if it goes away. Necessary in the case where
18247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // our lifetime is greater than that of the thread we are using, since we
18347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // try to send Close messages for all connected sockets when we shutdown.
18447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void OnMessageQueueDestroyed() { msg_queue_ = NULL; }
18547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
18647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Determine if two sockets should be able to communicate.
18747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // We don't (currently) specify an address family for sockets; instead,
18847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // the currently bound address is used to infer the address family.
18947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Any socket that is not explicitly bound to an IPv4 address is assumed to be
19047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // dual-stack capable.
19147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // This function tests if two addresses can communicate, as well as the
19247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // sockets to which they may be bound (the addresses may or may not yet be
19347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // bound to the sockets).
19447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // First the addresses are tested (after normalization):
19547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // If both have the same family, then communication is OK.
19647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // If only one is IPv4 then false, unless the other is bound to ::.
19747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // This applies even if the IPv4 address is 0.0.0.0.
19847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // The socket arguments are optional; the sockets are checked to see if they
19947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // were explicitly bound to IPv6-any ('::'), and if so communication is
20047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // permitted.
20147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // NB: This scheme doesn't permit non-dualstack IPv6 sockets.
20247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote);
20347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
20447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
20547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  friend class VirtualSocket;
20647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
20747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::map<SocketAddress, VirtualSocket*> AddressMap;
20847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap;
20947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketServer* server_;
21147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool server_owned_;
21247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  MessageQueue* msg_queue_;
21347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool stop_on_idle_;
21447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 network_delay_;
21547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  in_addr next_ipv4_;
21647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  in6_addr next_ipv6_;
21747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint16 next_port_;
21847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  AddressMap* bindings_;
21947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ConnectionMap* connections_;
22047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
22147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 bandwidth_;
22247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 network_capacity_;
22347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 send_buffer_capacity_;
22447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 recv_buffer_capacity_;
22547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_mean_;
22647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_stddev_;
22747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay_samples_;
22847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  Function* delay_dist_;
22947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CriticalSection delay_crit_;
23047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
23147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  double drop_prob_;
23247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_EVIL_CONSTRUCTORS(VirtualSocketServer);
23347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
23447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
23547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
23647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
23747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_BASE_VIRTUALSOCKETSERVER_H_
238