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.

696 lines
29 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_P2P_BASE_SESSION_H_
  28. #define TALK_P2P_BASE_SESSION_H_
  29. #include <list>
  30. #include <map>
  31. #include <string>
  32. #include <vector>
  33. #include "talk/base/refcount.h"
  34. #include "talk/base/scoped_ptr.h"
  35. #include "talk/base/scoped_ref_ptr.h"
  36. #include "talk/base/socketaddress.h"
  37. #include "talk/p2p/base/parsing.h"
  38. #include "talk/p2p/base/port.h"
  39. #include "talk/p2p/base/sessionclient.h"
  40. #include "talk/p2p/base/sessionmanager.h"
  41. #include "talk/p2p/base/sessionmessages.h"
  42. #include "talk/p2p/base/transport.h"
  43. #include "talk/xmllite/xmlelement.h"
  44. #include "talk/xmpp/constants.h"
  45. namespace cricket {
  46. class BaseSession;
  47. class P2PTransportChannel;
  48. class Transport;
  49. class TransportChannel;
  50. class TransportChannelProxy;
  51. class TransportChannelImpl;
  52. typedef talk_base::RefCountedObject<talk_base::scoped_ptr<Transport> >
  53. TransportWrapper;
  54. // Used for errors that will send back a specific error message to the
  55. // remote peer. We add "type" to the errors because it's needed for
  56. // SignalErrorMessage.
  57. struct MessageError : ParseError {
  58. buzz::QName type;
  59. // if unset, assume type is a parse error
  60. MessageError() : ParseError(), type(buzz::QN_STANZA_BAD_REQUEST) {}
  61. void SetType(const buzz::QName type) {
  62. this->type = type;
  63. }
  64. };
  65. // Used for errors that may be returned by public session methods that
  66. // can fail.
  67. // TODO: Use this error in Session::Initiate and
  68. // Session::Accept.
  69. struct SessionError : WriteError {
  70. };
  71. // Bundles a Transport and ChannelMap together. ChannelMap is used to
  72. // create transport channels before receiving or sending a session
  73. // initiate, and for speculatively connecting channels. Previously, a
  74. // session had one ChannelMap and transport. Now, with multiple
  75. // transports per session, we need multiple ChannelMaps as well.
  76. typedef std::map<int, TransportChannelProxy*> ChannelMap;
  77. class TransportProxy : public sigslot::has_slots<>,
  78. public CandidateTranslator {
  79. public:
  80. TransportProxy(
  81. const std::string& sid,
  82. const std::string& content_name,
  83. TransportWrapper* transport)
  84. : sid_(sid),
  85. content_name_(content_name),
  86. transport_(transport),
  87. connecting_(false),
  88. negotiated_(false),
  89. sent_candidates_(false),
  90. candidates_allocated_(false) {
  91. transport_->get()->SignalCandidatesReady.connect(
  92. this, &TransportProxy::OnTransportCandidatesReady);
  93. }
  94. ~TransportProxy();
  95. std::string content_name() const { return content_name_; }
  96. // TODO(juberti): It's not good form to expose the object you're wrapping,
  97. // since callers can mutate it. Can we make this return a const Transport*?
  98. Transport* impl() const { return transport_->get(); }
  99. std::string type() const;
  100. bool negotiated() const { return negotiated_; }
  101. const Candidates& sent_candidates() const { return sent_candidates_; }
  102. const Candidates& unsent_candidates() const { return unsent_candidates_; }
  103. bool candidates_allocated() const { return candidates_allocated_; }
  104. void set_candidates_allocated(bool allocated) {
  105. candidates_allocated_ = allocated;
  106. }
  107. TransportChannel* GetChannel(int component);
  108. TransportChannel* CreateChannel(const std::string& channel_name,
  109. int component);
  110. bool HasChannel(int component);
  111. void DestroyChannel(int component);
  112. void AddSentCandidates(const Candidates& candidates);
  113. void AddUnsentCandidates(const Candidates& candidates);
  114. void ClearSentCandidates() { sent_candidates_.clear(); }
  115. void ClearUnsentCandidates() { unsent_candidates_.clear(); }
  116. // Start the connection process for any channels, creating impls if needed.
  117. void ConnectChannels();
  118. // Hook up impls to the proxy channels. Doesn't change connect state.
  119. void CompleteNegotiation();
  120. // Mux this proxy onto the specified proxy's transport.
  121. bool SetupMux(TransportProxy* proxy);
  122. // Simple functions that thunk down to the same functions on Transport.
  123. void SetRole(TransportRole role);
  124. bool SetLocalTransportDescription(const TransportDescription& description,
  125. ContentAction action);
  126. bool SetRemoteTransportDescription(const TransportDescription& description,
  127. ContentAction action);
  128. void OnSignalingReady();
  129. bool OnRemoteCandidates(const Candidates& candidates, std::string* error);
  130. // CandidateTranslator methods.
  131. virtual bool GetChannelNameFromComponent(
  132. int component, std::string* channel_name) const;
  133. virtual bool GetComponentFromChannelName(
  134. const std::string& channel_name, int* component) const;
  135. // Called when a transport signals that it has new candidates.
  136. void OnTransportCandidatesReady(cricket::Transport* transport,
  137. const Candidates& candidates) {
  138. SignalCandidatesReady(this, candidates);
  139. }
  140. // Handles sending of ready candidates and receiving of remote candidates.
  141. sigslot::signal2<TransportProxy*,
  142. const std::vector<Candidate>&> SignalCandidatesReady;
  143. private:
  144. TransportChannelProxy* GetChannelProxy(int component) const;
  145. TransportChannelProxy* GetChannelProxyByName(const std::string& name) const;
  146. void ReplaceChannelProxyImpl(TransportChannelProxy* channel_proxy,
  147. size_t index);
  148. TransportChannelImpl* GetOrCreateChannelProxyImpl(int component);
  149. void SetChannelProxyImpl(int component,
  150. TransportChannelProxy* proxy);
  151. std::string sid_;
  152. std::string content_name_;
  153. talk_base::scoped_refptr<TransportWrapper> transport_;
  154. bool connecting_;
  155. bool negotiated_;
  156. ChannelMap channels_;
  157. Candidates sent_candidates_;
  158. Candidates unsent_candidates_;
  159. bool candidates_allocated_;
  160. };
  161. typedef std::map<std::string, TransportProxy*> TransportMap;
  162. // A BaseSession manages general session state. This includes negotiation
  163. // of both the application-level and network-level protocols: the former
  164. // defines what will be sent and the latter defines how it will be sent. Each
  165. // network-level protocol is represented by a Transport object. Each Transport
  166. // participates in the network-level negotiation. The individual streams of
  167. // packets are represented by TransportChannels. The application-level protocol
  168. // is represented by SessionDecription objects.
  169. class BaseSession : public sigslot::has_slots<>,
  170. public talk_base::MessageHandler {
  171. public:
  172. enum {
  173. MSG_TIMEOUT = 0,
  174. MSG_ERROR,
  175. MSG_STATE,
  176. };
  177. enum State {
  178. STATE_INIT = 0,
  179. STATE_SENTINITIATE, // sent initiate, waiting for Accept or Reject
  180. STATE_RECEIVEDINITIATE, // received an initiate. Call Accept or Reject
  181. STATE_SENTPRACCEPT, // sent provisional Accept
  182. STATE_SENTACCEPT, // sent accept. begin connecting transport
  183. STATE_RECEIVEDPRACCEPT, // received provisional Accept, waiting for Accept
  184. STATE_RECEIVEDACCEPT, // received accept. begin connecting transport
  185. STATE_SENTMODIFY, // sent modify, waiting for Accept or Reject
  186. STATE_RECEIVEDMODIFY, // received modify, call Accept or Reject
  187. STATE_SENTREJECT, // sent reject after receiving initiate
  188. STATE_RECEIVEDREJECT, // received reject after sending initiate
  189. STATE_SENTREDIRECT, // sent direct after receiving initiate
  190. STATE_SENTTERMINATE, // sent terminate (any time / either side)
  191. STATE_RECEIVEDTERMINATE, // received terminate (any time / either side)
  192. STATE_INPROGRESS, // session accepted and in progress
  193. STATE_DEINIT, // session is being destroyed
  194. };
  195. enum Error {
  196. ERROR_NONE = 0, // no error
  197. ERROR_TIME = 1, // no response to signaling
  198. ERROR_RESPONSE = 2, // error during signaling
  199. ERROR_NETWORK = 3, // network error, could not allocate network resources
  200. ERROR_CONTENT = 4, // channel errors in SetLocalContent/SetRemoteContent
  201. ERROR_TRANSPORT = 5, // transport error of some kind
  202. };
  203. // Convert State to a readable string.
  204. static std::string StateToString(State state);
  205. BaseSession(talk_base::Thread* signaling_thread,
  206. talk_base::Thread* worker_thread,
  207. PortAllocator* port_allocator,
  208. const std::string& sid,
  209. const std::string& content_type,
  210. bool initiator);
  211. virtual ~BaseSession();
  212. talk_base::Thread* signaling_thread() { return signaling_thread_; }
  213. talk_base::Thread* worker_thread() { return worker_thread_; }
  214. PortAllocator* port_allocator() { return port_allocator_; }
  215. // The ID of this session.
  216. const std::string& id() const { return sid_; }
  217. // TODO(juberti): This data is largely redundant, as it can now be obtained
  218. // from local/remote_description(). Remove these functions and members.
  219. // Returns the XML namespace identifying the type of this session.
  220. const std::string& content_type() const { return content_type_; }
  221. // Returns the XML namespace identifying the transport used for this session.
  222. const std::string& transport_type() const { return transport_type_; }
  223. // Indicates whether we initiated this session.
  224. bool initiator() const { return initiator_; }
  225. // Returns the application-level description given by our client.
  226. // If we are the recipient, this will be NULL until we send an accept.
  227. const SessionDescription* local_description() const {
  228. return local_description_;
  229. }
  230. // Returns the application-level description given by the other client.
  231. // If we are the initiator, this will be NULL until we receive an accept.
  232. const SessionDescription* remote_description() const {
  233. return remote_description_;
  234. }
  235. SessionDescription* remote_description() {
  236. return remote_description_;
  237. }
  238. // Takes ownership of SessionDescription*
  239. bool set_local_description(const SessionDescription* sdesc) {
  240. if (sdesc != local_description_) {
  241. delete local_description_;
  242. local_description_ = sdesc;
  243. }
  244. return true;
  245. }
  246. // Takes ownership of SessionDescription*
  247. bool set_remote_description(SessionDescription* sdesc) {
  248. if (sdesc != remote_description_) {
  249. delete remote_description_;
  250. remote_description_ = sdesc;
  251. }
  252. return true;
  253. }
  254. const SessionDescription* initiator_description() const {
  255. if (initiator_) {
  256. return local_description_;
  257. } else {
  258. return remote_description_;
  259. }
  260. }
  261. // Returns the current state of the session. See the enum above for details.
  262. // Each time the state changes, we will fire this signal.
  263. State state() const { return state_; }
  264. sigslot::signal2<BaseSession* , State> SignalState;
  265. // Returns the last error in the session. See the enum above for details.
  266. // Each time the an error occurs, we will fire this signal.
  267. Error error() const { return error_; }
  268. sigslot::signal2<BaseSession* , Error> SignalError;
  269. // Updates the state, signaling if necessary.
  270. virtual void SetState(State state);
  271. // Updates the error state, signaling if necessary.
  272. virtual void SetError(Error error);
  273. // Fired when the remote description is updated, with the updated
  274. // contents.
  275. sigslot::signal2<BaseSession* , const ContentInfos&>
  276. SignalRemoteDescriptionUpdate;
  277. // Returns the transport that has been negotiated or NULL if
  278. // negotiation is still in progress.
  279. Transport* GetTransport(const std::string& content_name);
  280. // Creates a new channel with the given names. This method may be called
  281. // immediately after creating the session. However, the actual
  282. // implementation may not be fixed until transport negotiation completes.
  283. // This will usually be called from the worker thread, but that
  284. // shouldn't be an issue since the main thread will be blocked in
  285. // Send when doing so.
  286. virtual TransportChannel* CreateChannel(const std::string& content_name,
  287. const std::string& channel_name,
  288. int component);
  289. // Returns the channel with the given names.
  290. virtual TransportChannel* GetChannel(const std::string& content_name,
  291. int component);
  292. // Destroys the channel with the given names.
  293. // This will usually be called from the worker thread, but that
  294. // shouldn't be an issue since the main thread will be blocked in
  295. // Send when doing so.
  296. virtual void DestroyChannel(const std::string& content_name,
  297. int component);
  298. protected:
  299. bool PushdownTransportDescription(ContentSource source,
  300. ContentAction action);
  301. void set_initiator(bool initiator) { initiator_ = initiator; }
  302. // Specifies the identity to use in this session.
  303. void set_identity(talk_base::SSLIdentity* identity) { identity_ = identity; }
  304. const TransportMap& transport_proxies() const { return transports_; }
  305. // Get a TransportProxy by content_name or transport. NULL if not found.
  306. TransportProxy* GetTransportProxy(const std::string& content_name);
  307. TransportProxy* GetTransportProxy(const Transport* transport);
  308. TransportProxy* GetFirstTransportProxy();
  309. void DestroyTransportProxy(const std::string& content_name);
  310. // TransportProxy is owned by session. Return proxy just for convenience.
  311. TransportProxy* GetOrCreateTransportProxy(const std::string& content_name);
  312. // Creates the actual transport object. Overridable for testing.
  313. virtual Transport* CreateTransport(const std::string& content_name);
  314. void OnSignalingReady();
  315. void SpeculativelyConnectAllTransportChannels();
  316. // Helper method to provide remote candidates to the transport.
  317. bool OnRemoteCandidates(const std::string& content_name,
  318. const Candidates& candidates,
  319. std::string* error);
  320. // This method will mux transport channels by content_name.
  321. // First content is used for muxing.
  322. bool MaybeEnableMuxingSupport();
  323. // Called when a transport requests signaling.
  324. virtual void OnTransportRequestSignaling(Transport* transport) {
  325. }
  326. // Called when the first channel of a transport begins connecting. We use
  327. // this to start a timer, to make sure that the connection completes in a
  328. // reasonable amount of time.
  329. virtual void OnTransportConnecting(Transport* transport) {
  330. }
  331. // Called when a transport changes its writable state. We track this to make
  332. // sure that the transport becomes writable within a reasonable amount of
  333. // time. If this does not occur, we signal an error.
  334. virtual void OnTransportWritable(Transport* transport) {
  335. }
  336. virtual void OnTransportReadable(Transport* transport) {
  337. }
  338. // Called when a transport signals that it has new candidates.
  339. virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
  340. const Candidates& candidates) {
  341. }
  342. // Called when a transport signals that it found an error in an incoming
  343. // message.
  344. virtual void OnTransportSendError(Transport* transport,
  345. const buzz::XmlElement* stanza,
  346. const buzz::QName& name,
  347. const std::string& type,
  348. const std::string& text,
  349. const buzz::XmlElement* extra_info) {
  350. }
  351. virtual void OnTransportRouteChange(
  352. Transport* transport,
  353. int component,
  354. const cricket::Candidate& remote_candidate) {
  355. }
  356. virtual void OnTransportCandidatesAllocationDone(Transport* transport);
  357. // Called when all transport channels allocated required candidates.
  358. // This method should be used as an indication of candidates gathering process
  359. // is completed and application can now send local candidates list to remote.
  360. virtual void OnCandidatesAllocationDone() {
  361. }
  362. // Handles the ice role change callback from Transport. This must be
  363. // propagated to all the transports.
  364. virtual void OnRoleConflict();
  365. // Handles messages posted to us.
  366. virtual void OnMessage(talk_base::Message *pmsg);
  367. protected:
  368. State state_;
  369. Error error_;
  370. private:
  371. // Helper methods to push local and remote transport descriptions.
  372. bool PushdownLocalTransportDescription(
  373. const SessionDescription* sdesc, ContentAction action);
  374. bool PushdownRemoteTransportDescription(
  375. const SessionDescription* sdesc, ContentAction action);
  376. bool IsCandidateAllocationDone() const;
  377. void MaybeCandidateAllocationDone();
  378. // This method will delete the Transport and TransportChannelImpls and
  379. // replace those with the selected Transport objects. Selection is done
  380. // based on the content_name and in this case first MediaContent information
  381. // is used for mux.
  382. bool SetSelectedProxy(const std::string& content_name,
  383. const ContentGroup* muxed_group);
  384. // Log session state.
  385. void LogState(State old_state, State new_state);
  386. // Returns true and the TransportInfo of the given |content_name|
  387. // from |description|. Returns false if it's not available.
  388. bool GetTransportDescription(const SessionDescription* description,
  389. const std::string& content_name,
  390. TransportDescription* info);
  391. talk_base::Thread* signaling_thread_;
  392. talk_base::Thread* worker_thread_;
  393. PortAllocator* port_allocator_;
  394. std::string sid_;
  395. std::string content_type_;
  396. std::string transport_type_;
  397. bool initiator_;
  398. talk_base::SSLIdentity* identity_;
  399. const SessionDescription* local_description_;
  400. SessionDescription* remote_description_;
  401. bool transport_muxed_;
  402. uint64 ice_tiebreaker_;
  403. // This flag will be set to true after the first role switch. This flag
  404. // will enable us to stop any role switch during the call.
  405. bool role_switch_;
  406. TransportMap transports_;
  407. };
  408. // A specific Session created by the SessionManager, using XMPP for protocol.
  409. class Session : public BaseSession {
  410. public:
  411. // Returns the manager that created and owns this session.
  412. SessionManager* session_manager() const { return session_manager_; }
  413. // Returns the client that is handling the application data of this session.
  414. SessionClient* client() const { return client_; }
  415. // Returns the JID of this client.
  416. const std::string& local_name() const { return local_name_; }
  417. // Returns the JID of the other peer in this session.
  418. const std::string& remote_name() const { return remote_name_; }
  419. // Set the JID of the other peer in this session.
  420. // Typically the remote_name_ is set when the session is initiated.
  421. // However, sometimes (e.g when a proxy is used) the peer name is
  422. // known after the BaseSession has been initiated and it must be updated
  423. // explicitly.
  424. void set_remote_name(const std::string& name) { remote_name_ = name; }
  425. // Set the JID of the initiator of this session. Allows for the overriding
  426. // of the initiator to be a third-party, eg. the MUC JID when creating p2p
  427. // sessions.
  428. void set_initiator_name(const std::string& name) { initiator_name_ = name; }
  429. // Indicates the JID of the entity who initiated this session.
  430. // In special cases, may be different than both local_name and remote_name.
  431. const std::string& initiator_name() const { return initiator_name_; }
  432. SignalingProtocol current_protocol() const { return current_protocol_; }
  433. void set_current_protocol(SignalingProtocol protocol) {
  434. current_protocol_ = protocol;
  435. }
  436. // Updates the error state, signaling if necessary.
  437. virtual void SetError(Error error);
  438. // When the session needs to send signaling messages, it beings by requesting
  439. // signaling. The client should handle this by calling OnSignalingReady once
  440. // it is ready to send the messages.
  441. // (These are called only by SessionManager.)
  442. sigslot::signal1<Session*> SignalRequestSignaling;
  443. void OnSignalingReady() { BaseSession::OnSignalingReady(); }
  444. // Takes ownership of session description.
  445. // TODO: Add an error argument to pass back to the caller.
  446. bool Initiate(const std::string& to,
  447. const SessionDescription* sdesc);
  448. // When we receive an initiate, we create a session in the
  449. // RECEIVEDINITIATE state and respond by accepting or rejecting.
  450. // Takes ownership of session description.
  451. // TODO: Add an error argument to pass back to the caller.
  452. bool Accept(const SessionDescription* sdesc);
  453. bool Reject(const std::string& reason);
  454. bool Terminate() {
  455. return TerminateWithReason(STR_TERMINATE_SUCCESS);
  456. }
  457. bool TerminateWithReason(const std::string& reason);
  458. // Fired whenever we receive a terminate message along with a reason
  459. sigslot::signal2<Session*, const std::string&> SignalReceivedTerminateReason;
  460. // The two clients in the session may also send one another
  461. // arbitrary XML messages, which are called "info" messages. Sending
  462. // takes ownership of the given elements. The signal does not; the
  463. // parent element will be deleted after the signal.
  464. bool SendInfoMessage(const XmlElements& elems);
  465. bool SendDescriptionInfoMessage(const ContentInfos& contents);
  466. sigslot::signal2<Session*, const buzz::XmlElement*> SignalInfoMessage;
  467. private:
  468. // Creates or destroys a session. (These are called only SessionManager.)
  469. Session(SessionManager *session_manager,
  470. const std::string& local_name, const std::string& initiator_name,
  471. const std::string& sid, const std::string& content_type,
  472. SessionClient* client);
  473. ~Session();
  474. // For each transport info, create a transport proxy. Can fail for
  475. // incompatible transport types.
  476. bool CreateTransportProxies(const TransportInfos& tinfos,
  477. SessionError* error);
  478. bool OnRemoteCandidates(const TransportInfos& tinfos,
  479. ParseError* error);
  480. // Returns a TransportInfo without candidates for each content name.
  481. // Uses the transport_type_ of the session.
  482. TransportInfos GetEmptyTransportInfos(const ContentInfos& contents) const;
  483. // Maps passed to serialization functions.
  484. TransportParserMap GetTransportParsers();
  485. ContentParserMap GetContentParsers();
  486. CandidateTranslatorMap GetCandidateTranslators();
  487. virtual void OnTransportRequestSignaling(Transport* transport);
  488. virtual void OnTransportConnecting(Transport* transport);
  489. virtual void OnTransportWritable(Transport* transport);
  490. virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
  491. const Candidates& candidates);
  492. virtual void OnTransportSendError(Transport* transport,
  493. const buzz::XmlElement* stanza,
  494. const buzz::QName& name,
  495. const std::string& type,
  496. const std::string& text,
  497. const buzz::XmlElement* extra_info);
  498. virtual void OnMessage(talk_base::Message *pmsg);
  499. // Send various kinds of session messages.
  500. bool SendInitiateMessage(const SessionDescription* sdesc,
  501. SessionError* error);
  502. bool SendAcceptMessage(const SessionDescription* sdesc, SessionError* error);
  503. bool SendRejectMessage(const std::string& reason, SessionError* error);
  504. bool SendTerminateMessage(const std::string& reason, SessionError* error);
  505. bool SendTransportInfoMessage(const TransportInfo& tinfo,
  506. SessionError* error);
  507. bool SendTransportInfoMessage(const TransportProxy* transproxy,
  508. const Candidates& candidates,
  509. SessionError* error);
  510. bool ResendAllTransportInfoMessages(SessionError* error);
  511. bool SendAllUnsentTransportInfoMessages(SessionError* error);
  512. // Both versions of SendMessage send a message of the given type to
  513. // the other client. Can pass either a set of elements or an
  514. // "action", which must have a WriteSessionAction method to go along
  515. // with it. Sending with an action supports sending a "hybrid"
  516. // message. Sending with elements must be sent as Jingle or Gingle.
  517. // When passing elems, must be either Jingle or Gingle protocol.
  518. // Takes ownership of action_elems.
  519. bool SendMessage(ActionType type, const XmlElements& action_elems,
  520. SessionError* error);
  521. // When passing an action, may be Hybrid protocol.
  522. template <typename Action>
  523. bool SendMessage(ActionType type, const Action& action,
  524. SessionError* error);
  525. // Helper methods to write the session message stanza.
  526. template <typename Action>
  527. bool WriteActionMessage(ActionType type, const Action& action,
  528. buzz::XmlElement* stanza, WriteError* error);
  529. template <typename Action>
  530. bool WriteActionMessage(SignalingProtocol protocol,
  531. ActionType type, const Action& action,
  532. buzz::XmlElement* stanza, WriteError* error);
  533. // Sending messages in hybrid form requires being able to write them
  534. // on a per-protocol basis with a common method signature, which all
  535. // of these have.
  536. bool WriteSessionAction(SignalingProtocol protocol,
  537. const SessionInitiate& init,
  538. XmlElements* elems, WriteError* error);
  539. bool WriteSessionAction(SignalingProtocol protocol,
  540. const TransportInfo& tinfo,
  541. XmlElements* elems, WriteError* error);
  542. bool WriteSessionAction(SignalingProtocol protocol,
  543. const SessionTerminate& term,
  544. XmlElements* elems, WriteError* error);
  545. // Sends a message back to the other client indicating that we have received
  546. // and accepted their message.
  547. void SendAcknowledgementMessage(const buzz::XmlElement* stanza);
  548. // Once signaling is ready, the session will use this signal to request the
  549. // sending of each message. When messages are received by the other client,
  550. // they should be handed to OnIncomingMessage.
  551. // (These are called only by SessionManager.)
  552. sigslot::signal2<Session* , const buzz::XmlElement*> SignalOutgoingMessage;
  553. void OnIncomingMessage(const SessionMessage& msg);
  554. void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
  555. const buzz::XmlElement* response_stanza,
  556. const SessionMessage& msg);
  557. void OnInitiateAcked();
  558. void OnFailedSend(const buzz::XmlElement* orig_stanza,
  559. const buzz::XmlElement* error_stanza);
  560. // Invoked when an error is found in an incoming message. This is translated
  561. // into the appropriate XMPP response by SessionManager.
  562. sigslot::signal6<BaseSession*,
  563. const buzz::XmlElement*,
  564. const buzz::QName&,
  565. const std::string&,
  566. const std::string&,
  567. const buzz::XmlElement*> SignalErrorMessage;
  568. // Handlers for the various types of messages. These functions may take
  569. // pointers to the whole stanza or to just the session element.
  570. bool OnInitiateMessage(const SessionMessage& msg, MessageError* error);
  571. bool OnAcceptMessage(const SessionMessage& msg, MessageError* error);
  572. bool OnRejectMessage(const SessionMessage& msg, MessageError* error);
  573. bool OnInfoMessage(const SessionMessage& msg);
  574. bool OnTerminateMessage(const SessionMessage& msg, MessageError* error);
  575. bool OnTransportInfoMessage(const SessionMessage& msg, MessageError* error);
  576. bool OnTransportAcceptMessage(const SessionMessage& msg, MessageError* error);
  577. bool OnDescriptionInfoMessage(const SessionMessage& msg, MessageError* error);
  578. bool OnRedirectError(const SessionRedirect& redirect, SessionError* error);
  579. // Verifies that we are in the appropriate state to receive this message.
  580. bool CheckState(State state, MessageError* error);
  581. SessionManager* session_manager_;
  582. bool initiate_acked_;
  583. std::string local_name_;
  584. std::string initiator_name_;
  585. std::string remote_name_;
  586. SessionClient* client_;
  587. TransportParser* transport_parser_;
  588. // Keeps track of what protocol we are speaking.
  589. SignalingProtocol current_protocol_;
  590. friend class SessionManager; // For access to constructor, destructor,
  591. // and signaling related methods.
  592. };
  593. } // namespace cricket
  594. #endif // TALK_P2P_BASE_SESSION_H_