1/*
2 * libjingle
3 * Copyright 2004--2006, 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_SESSION_TUNNEL_PSEUDOTCPCHANNEL_H_
29#define TALK_SESSION_TUNNEL_PSEUDOTCPCHANNEL_H_
30
31#include "talk/p2p/base/pseudotcp.h"
32#include "talk/p2p/base/session.h"
33#include "webrtc/base/criticalsection.h"
34#include "webrtc/base/messagequeue.h"
35#include "webrtc/base/stream.h"
36
37namespace rtc {
38class Thread;
39}
40
41namespace cricket {
42
43class Candidate;
44class TransportChannel;
45
46///////////////////////////////////////////////////////////////////////////////
47// PseudoTcpChannel
48// Note: The PseudoTcpChannel must persist until both of:
49// 1) The StreamInterface provided via GetStream has been closed.
50//    This is tracked via non-null stream_.
51// 2) The PseudoTcp session has completed.
52//    This is tracked via non-null worker_thread_.  When PseudoTcp is done,
53//    the TransportChannel is signalled to tear-down.  Once the channel is
54//    torn down, the worker thread is purged.
55// These indicators are checked by CheckDestroy, invoked whenever one of them
56// changes.
57///////////////////////////////////////////////////////////////////////////////
58// PseudoTcpChannel::GetStream
59// Note: The stream pointer returned by GetStream is owned by the caller.
60// They can close & immediately delete the stream while PseudoTcpChannel still
61// has cleanup work to do.  They can also close the stream but not delete it
62// until long after PseudoTcpChannel has finished.  We must cope with both.
63///////////////////////////////////////////////////////////////////////////////
64
65class PseudoTcpChannel
66    : public IPseudoTcpNotify,
67      public rtc::MessageHandler,
68      public sigslot::has_slots<> {
69 public:
70  // Signal thread methods
71  PseudoTcpChannel(rtc::Thread* stream_thread,
72                   Session* session);
73
74  bool Connect(const std::string& content_name,
75               const std::string& channel_name,
76               int component);
77  rtc::StreamInterface* GetStream();
78
79  sigslot::signal1<PseudoTcpChannel*> SignalChannelClosed;
80
81  // Call this when the Session used to create this channel is being torn
82  // down, to ensure that things get cleaned up properly.
83  void OnSessionTerminate(Session* session);
84
85  // See the PseudoTcp class for available options.
86  void GetOption(PseudoTcp::Option opt, int* value);
87  void SetOption(PseudoTcp::Option opt, int value);
88
89 private:
90  class InternalStream;
91  friend class InternalStream;
92
93  virtual ~PseudoTcpChannel();
94
95  // Stream thread methods
96  rtc::StreamState GetState() const;
97  rtc::StreamResult Read(void* buffer, size_t buffer_len,
98                               size_t* read, int* error);
99  rtc::StreamResult Write(const void* data, size_t data_len,
100                                size_t* written, int* error);
101  void Close();
102
103  // Multi-thread methods
104  void OnMessage(rtc::Message* pmsg);
105  void AdjustClock(bool clear = true);
106  void CheckDestroy();
107
108  // Signal thread methods
109  void OnChannelDestroyed(TransportChannel* channel);
110
111  // Worker thread methods
112  void OnChannelWritableState(TransportChannel* channel);
113  void OnChannelRead(TransportChannel* channel, const char* data, size_t size,
114                     const rtc::PacketTime& packet_time, int flags);
115  void OnChannelConnectionChanged(TransportChannel* channel,
116                                  const Candidate& candidate);
117
118  virtual void OnTcpOpen(PseudoTcp* ptcp);
119  virtual void OnTcpReadable(PseudoTcp* ptcp);
120  virtual void OnTcpWriteable(PseudoTcp* ptcp);
121  virtual void OnTcpClosed(PseudoTcp* ptcp, uint32 nError);
122  virtual IPseudoTcpNotify::WriteResult TcpWritePacket(PseudoTcp* tcp,
123                                                       const char* buffer,
124                                                       size_t len);
125
126  rtc::Thread* signal_thread_, * worker_thread_, * stream_thread_;
127  Session* session_;
128  TransportChannel* channel_;
129  std::string content_name_;
130  std::string channel_name_;
131  PseudoTcp* tcp_;
132  InternalStream* stream_;
133  bool stream_readable_, pending_read_event_;
134  bool ready_to_connect_;
135  mutable rtc::CriticalSection cs_;
136};
137
138}  // namespace cricket
139
140#endif  // TALK_SESSION_TUNNEL_PSEUDOTCPCHANNEL_H_
141