~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/base/virtualsocketserver.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
28
#ifndef TALK_BASE_VIRTUALSOCKETSERVER_H_
 
29
#define TALK_BASE_VIRTUALSOCKETSERVER_H_
 
30
 
 
31
#include <cassert>
 
32
#include <deque>
 
33
#include <map>
 
34
 
 
35
#include "talk/base/messagequeue.h"
 
36
#include "talk/base/socketserver.h"
 
37
 
 
38
namespace talk_base {
 
39
 
 
40
class VirtualSocket;
 
41
class SocketAddressPair;
 
42
 
 
43
// Simulates a network in the same manner as a loopback interface.  The
 
44
// interface can create as many addresses as you want.  All of the sockets
 
45
// created by this network will be able to communicate with one another, unless
 
46
// they are bound to addresses from incompatible families.
 
47
class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
 
48
 public:
 
49
  // TODO: Add "owned" parameter.
 
50
  // If "owned" is set, the supplied socketserver will be deleted later.
 
51
  explicit VirtualSocketServer(SocketServer* ss);
 
52
  virtual ~VirtualSocketServer();
 
53
 
 
54
  SocketServer* socketserver() { return server_; }
 
55
 
 
56
  // Limits the network bandwidth (maximum bytes per second).  Zero means that
 
57
  // all sends occur instantly.  Defaults to 0.
 
58
  uint32 bandwidth() const { return bandwidth_; }
 
59
  void set_bandwidth(uint32 bandwidth) { bandwidth_ = bandwidth; }
 
60
 
 
61
  // Limits the amount of data which can be in flight on the network without
 
62
  // packet loss (on a per sender basis).  Defaults to 64 KB.
 
63
  uint32 network_capacity() const { return network_capacity_; }
 
64
  void set_network_capacity(uint32 capacity) {
 
65
    network_capacity_ = capacity;
 
66
  }
 
67
 
 
68
  // The amount of data which can be buffered by tcp on the sender's side
 
69
  uint32 send_buffer_capacity() const { return send_buffer_capacity_; }
 
70
  void set_send_buffer_capacity(uint32 capacity) {
 
71
    send_buffer_capacity_ = capacity;
 
72
  }
 
73
 
 
74
  // The amount of data which can be buffered by tcp on the receiver's side
 
75
  uint32 recv_buffer_capacity() const { return recv_buffer_capacity_; }
 
76
  void set_recv_buffer_capacity(uint32 capacity) {
 
77
    recv_buffer_capacity_ = capacity;
 
78
  }
 
79
 
 
80
  // Controls the (transit) delay for packets sent in the network.  This does
 
81
  // not inclue the time required to sit in the send queue.  Both of these
 
82
  // values are measured in milliseconds.  Defaults to no delay.
 
83
  uint32 delay_mean() const { return delay_mean_; }
 
84
  uint32 delay_stddev() const { return delay_stddev_; }
 
85
  uint32 delay_samples() const { return delay_samples_; }
 
86
  void set_delay_mean(uint32 delay_mean) { delay_mean_ = delay_mean; }
 
87
  void set_delay_stddev(uint32 delay_stddev) {
 
88
    delay_stddev_ = delay_stddev;
 
89
  }
 
90
  void set_delay_samples(uint32 delay_samples) {
 
91
    delay_samples_ = delay_samples;
 
92
  }
 
93
 
 
94
  // If the (transit) delay parameters are modified, this method should be
 
95
  // called to recompute the new distribution.
 
96
  void UpdateDelayDistribution();
 
97
 
 
98
  // Controls the (uniform) probability that any sent packet is dropped.  This
 
99
  // is separate from calculations to drop based on queue size.
 
100
  double drop_probability() { return drop_prob_; }
 
101
  void set_drop_probability(double drop_prob) {
 
102
    assert((0 <= drop_prob) && (drop_prob <= 1));
 
103
    drop_prob_ = drop_prob;
 
104
  }
 
105
 
 
106
  // SocketFactory:
 
107
  virtual Socket* CreateSocket(int type);
 
108
  virtual Socket* CreateSocket(int family, int type);
 
109
 
 
110
  virtual AsyncSocket* CreateAsyncSocket(int type);
 
111
  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
 
112
 
 
113
  // SocketServer:
 
114
  virtual void SetMessageQueue(MessageQueue* queue);
 
115
  virtual bool Wait(int cms, bool process_io);
 
116
  virtual void WakeUp();
 
117
 
 
118
  typedef std::pair<double, double> Point;
 
119
  typedef std::vector<Point> Function;
 
120
 
 
121
  static Function* CreateDistribution(uint32 mean, uint32 stddev,
 
122
                                      uint32 samples);
 
123
 
 
124
  // Similar to Thread::ProcessMessages, but it only processes messages until
 
125
  // there are no immediate messages or pending network traffic.  Returns false
 
126
  // if Thread::Stop() was called.
 
127
  bool ProcessMessagesUntilIdle();
 
128
 
 
129
 protected:
 
130
  // Returns a new IP not used before in this network.
 
131
  IPAddress GetNextIP(int family);
 
132
  uint16 GetNextPort();
 
133
 
 
134
  VirtualSocket* CreateSocketInternal(int family, int type);
 
135
 
 
136
  // Binds the given socket to addr, assigning and IP and Port if necessary
 
137
  int Bind(VirtualSocket* socket, SocketAddress* addr);
 
138
 
 
139
  // Binds the given socket to the given (fully-defined) address.
 
140
  int Bind(VirtualSocket* socket, const SocketAddress& addr);
 
141
 
 
142
  // Find the socket bound to the given address
 
143
  VirtualSocket* LookupBinding(const SocketAddress& addr);
 
144
 
 
145
  int Unbind(const SocketAddress& addr, VirtualSocket* socket);
 
146
 
 
147
  // Adds a mapping between this socket pair and the socket.
 
148
  void AddConnection(const SocketAddress& client,
 
149
                     const SocketAddress& server,
 
150
                     VirtualSocket* socket);
 
151
 
 
152
  // Find the socket pair corresponding to this server address.
 
153
  VirtualSocket* LookupConnection(const SocketAddress& client,
 
154
                                  const SocketAddress& server);
 
155
 
 
156
  void RemoveConnection(const SocketAddress& client,
 
157
                        const SocketAddress& server);
 
158
 
 
159
  // Connects the given socket to the socket at the given address
 
160
  int Connect(VirtualSocket* socket, const SocketAddress& remote_addr,
 
161
              bool use_delay);
 
162
 
 
163
  // Sends a disconnect message to the socket at the given address
 
164
  bool Disconnect(VirtualSocket* socket);
 
165
 
 
166
  // Sends the given packet to the socket at the given address (if one exists).
 
167
  int SendUdp(VirtualSocket* socket, const char* data, size_t data_size,
 
168
              const SocketAddress& remote_addr);
 
169
 
 
170
  // Moves as much data as possible from the sender's buffer to the network
 
171
  void SendTcp(VirtualSocket* socket);
 
172
 
 
173
  // Places a packet on the network.
 
174
  void AddPacketToNetwork(VirtualSocket* socket, VirtualSocket* recipient,
 
175
                          uint32 cur_time, const char* data, size_t data_size,
 
176
                          size_t header_size, bool ordered);
 
177
 
 
178
  // Removes stale packets from the network
 
179
  void PurgeNetworkPackets(VirtualSocket* socket, uint32 cur_time);
 
180
 
 
181
  // Computes the number of milliseconds required to send a packet of this size.
 
182
  uint32 SendDelay(uint32 size);
 
183
 
 
184
  // Returns a random transit delay chosen from the appropriate distribution.
 
185
  uint32 GetRandomTransitDelay();
 
186
 
 
187
  // Basic operations on functions.  Those that return a function also take
 
188
  // ownership of the function given (and hence, may modify or delete it).
 
189
  static Function* Accumulate(Function* f);
 
190
  static Function* Invert(Function* f);
 
191
  static Function* Resample(Function* f, double x1, double x2, uint32 samples);
 
192
  static double Evaluate(Function* f, double x);
 
193
 
 
194
  // NULL out our message queue if it goes away. Necessary in the case where
 
195
  // our lifetime is greater than that of the thread we are using, since we
 
196
  // try to send Close messages for all connected sockets when we shutdown.
 
197
  void OnMessageQueueDestroyed() { msg_queue_ = NULL; }
 
198
 
 
199
  // Determine if two sockets should be able to communicate.
 
200
  // We don't (currently) specify an address family for sockets; instead,
 
201
  // the currently bound address is used to infer the address family.
 
202
  // Any socket that is not explicitly bound to an IPv4 address is assumed to be
 
203
  // dual-stack capable.
 
204
  // This function tests if two addresses can communicate, as well as the
 
205
  // sockets to which they may be bound (the addresses may or may not yet be
 
206
  // bound to the sockets).
 
207
  // First the addresses are tested (after normalization):
 
208
  // If both have the same family, then communication is OK.
 
209
  // If only one is IPv4 then false, unless the other is bound to ::.
 
210
  // This applies even if the IPv4 address is 0.0.0.0.
 
211
  // The socket arguments are optional; the sockets are checked to see if they
 
212
  // were explicitly bound to IPv6-any ('::'), and if so communication is
 
213
  // permitted.
 
214
  // NB: This scheme doesn't permit non-dualstack IPv6 sockets.
 
215
  static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote);
 
216
 
 
217
 private:
 
218
  friend class VirtualSocket;
 
219
 
 
220
  typedef std::map<SocketAddress, VirtualSocket*> AddressMap;
 
221
  typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap;
 
222
 
 
223
  SocketServer* server_;
 
224
  bool server_owned_;
 
225
  MessageQueue* msg_queue_;
 
226
  bool stop_on_idle_;
 
227
  uint32 network_delay_;
 
228
  in_addr next_ipv4_;
 
229
  in6_addr next_ipv6_;
 
230
  uint16 next_port_;
 
231
  AddressMap* bindings_;
 
232
  ConnectionMap* connections_;
 
233
 
 
234
  uint32 bandwidth_;
 
235
  uint32 network_capacity_;
 
236
  uint32 send_buffer_capacity_;
 
237
  uint32 recv_buffer_capacity_;
 
238
  uint32 delay_mean_;
 
239
  uint32 delay_stddev_;
 
240
  uint32 delay_samples_;
 
241
  Function* delay_dist_;
 
242
  CriticalSection delay_crit_;
 
243
 
 
244
  double drop_prob_;
 
245
  DISALLOW_EVIL_CONSTRUCTORS(VirtualSocketServer);
 
246
};
 
247
 
 
248
}  // namespace talk_base
 
249
 
 
250
#endif  // TALK_BASE_VIRTUALSOCKETSERVER_H_