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

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/p2ptransportchannel.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
// P2PTransportChannel wraps up the state management of the connection between
 
29
// two P2P clients.  Clients have candidate ports for connecting, and
 
30
// connections which are combinations of candidates from each end (Alice and
 
31
// Bob each have candidates, one candidate from Alice and one candidate from
 
32
// Bob are used to make a connection, repeat to make many connections).
 
33
//
 
34
// When all of the available connections become invalid (non-writable), we
 
35
// kick off a process of determining more candidates and more connections.
 
36
//
 
37
#ifndef TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
 
38
#define TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
 
39
 
 
40
#include <map>
 
41
#include <vector>
 
42
#include <string>
 
43
#include "talk/base/sigslot.h"
 
44
#include "talk/p2p/base/candidate.h"
 
45
#include "talk/p2p/base/port.h"
 
46
#include "talk/p2p/base/portallocator.h"
 
47
#include "talk/p2p/base/transport.h"
 
48
#include "talk/p2p/base/transportchannelimpl.h"
 
49
#include "talk/p2p/base/p2ptransport.h"
 
50
 
 
51
namespace cricket {
 
52
 
 
53
// Adds the port on which the candidate originated.
 
54
class RemoteCandidate : public Candidate {
 
55
 public:
 
56
  RemoteCandidate(const Candidate& c, Port* origin_port)
 
57
    : Candidate(c), origin_port_(origin_port) {}
 
58
 
 
59
  Port* origin_port() { return origin_port_; }
 
60
 
 
61
 private:
 
62
  Port* origin_port_;
 
63
};
 
64
 
 
65
// P2PTransportChannel manages the candidates and connection process to keep
 
66
// two P2P clients connected to each other.
 
67
class P2PTransportChannel : public TransportChannelImpl,
 
68
    public talk_base::MessageHandler {
 
69
 public:
 
70
  P2PTransportChannel(const std::string &name,
 
71
                      const std::string &content_type,
 
72
                      P2PTransport* transport,
 
73
                      PortAllocator *allocator);
 
74
  virtual ~P2PTransportChannel();
 
75
 
 
76
  // From TransportChannelImpl:
 
77
  virtual Transport* GetTransport() { return transport_; }
 
78
  virtual void Connect();
 
79
  virtual void Reset();
 
80
  virtual void OnSignalingReady();
 
81
 
 
82
  // From TransportChannel:
 
83
  virtual int SendPacket(const char *data, size_t len);
 
84
  virtual int SetOption(talk_base::Socket::Option opt, int value);
 
85
  virtual int GetError() { return error_; }
 
86
 
 
87
  // This hack is here to allow the SocketMonitor to downcast to the
 
88
  // P2PTransportChannel safely.
 
89
  virtual P2PTransportChannel* GetP2PChannel() { return this; }
 
90
 
 
91
  // These are used by the connection monitor.
 
92
  sigslot::signal1<P2PTransportChannel*> SignalConnectionMonitor;
 
93
  const std::vector<Connection *>& connections() const { return connections_; }
 
94
  Connection* best_connection() const { return best_connection_; }
 
95
 
 
96
  void set_incoming_only(bool value) { incoming_only_ = value; }
 
97
 
 
98
  // Handler for internal messages.
 
99
  virtual void OnMessage(talk_base::Message *pmsg);
 
100
 
 
101
  virtual void OnCandidate(const Candidate& candidate);
 
102
 
 
103
 private:
 
104
  void Allocate();
 
105
  void CancelPendingAllocate();
 
106
  void UpdateConnectionStates();
 
107
  void RequestSort();
 
108
  void SortConnections();
 
109
  void SwitchBestConnectionTo(Connection* conn);
 
110
  void UpdateChannelState();
 
111
  void HandleWritable();
 
112
  void HandleNotWritable();
 
113
  void HandleAllTimedOut();
 
114
  Connection* GetBestConnectionOnNetwork(talk_base::Network* network);
 
115
  bool CreateConnections(const Candidate &remote_candidate, Port* origin_port,
 
116
                         bool readable);
 
117
  bool CreateConnection(Port* port, const Candidate& remote_candidate,
 
118
                        Port* origin_port, bool readable);
 
119
  bool FindConnection(cricket::Connection* connection) const;
 
120
  void RememberRemoteCandidate(const Candidate& remote_candidate,
 
121
                               Port* origin_port);
 
122
  void OnUnknownAddress(Port *port, const talk_base::SocketAddress &addr,
 
123
                        StunMessage *stun_msg,
 
124
                        const std::string &remote_username, bool port_muxed);
 
125
  void OnPortReady(PortAllocatorSession *session, Port* port);
 
126
  void OnCandidatesReady(PortAllocatorSession *session,
 
127
                         const std::vector<Candidate>& candidates);
 
128
  void OnCandidatesAllocationDone(PortAllocatorSession* session);
 
129
  void OnConnectionStateChange(Connection *connection);
 
130
  void OnConnectionDestroyed(Connection *connection);
 
131
  void OnPortDestroyed(Port* port);
 
132
  void OnReadPacket(Connection *connection, const char *data, size_t len);
 
133
  void OnSort();
 
134
  void OnPing();
 
135
  bool IsPingable(Connection* conn);
 
136
  Connection* FindNextPingableConnection();
 
137
  uint32 NumPingableConnections();
 
138
  PortAllocatorSession* allocator_session() {
 
139
    return allocator_sessions_.back();
 
140
  }
 
141
  void AddAllocatorSession(PortAllocatorSession* session);
 
142
 
 
143
  talk_base::Thread* thread() const { return worker_thread_; }
 
144
 
 
145
  P2PTransport* transport_;
 
146
  PortAllocator *allocator_;
 
147
  talk_base::Thread *worker_thread_;
 
148
  bool incoming_only_;
 
149
  bool waiting_for_signaling_;
 
150
  int error_;
 
151
  std::vector<PortAllocatorSession*> allocator_sessions_;
 
152
  std::vector<Port *> ports_;
 
153
  std::vector<Connection *> connections_;
 
154
  Connection *best_connection_;
 
155
  std::vector<RemoteCandidate> remote_candidates_;
 
156
  // indicates whether StartGetAllCandidates has been called
 
157
  bool pinging_started_;
 
158
  bool sort_dirty_;  // indicates whether another sort is needed right now
 
159
  bool was_writable_;
 
160
  bool was_timed_out_;
 
161
  typedef std::map<talk_base::Socket::Option, int> OptionMap;
 
162
  OptionMap options_;
 
163
 
 
164
  DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel);
 
165
};
 
166
 
 
167
}  // namespace cricket
 
168
 
 
169
#endif  // TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_