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