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_P2P_BASE_RAWTRANSPORTCHANNEL_H_ 29 #define TALK_P2P_BASE_RAWTRANSPORTCHANNEL_H_ 30 31 #include <string> 32 #include <vector> 33 #include "talk/base/messagequeue.h" 34 #include "talk/p2p/base/transportchannelimpl.h" 35 #include "talk/p2p/base/rawtransport.h" 36 #include "talk/p2p/base/candidate.h" 37 38 #if defined(FEATURE_ENABLE_PSTN) 39 40 namespace talk_base { 41 class Thread; 42 } 43 44 namespace cricket { 45 46 class Port; 47 class Connection; 48 class StunPort; 49 class RelayPort; 50 class PortAllocator; 51 class PortAllocatorSession; 52 53 // Implements a channel that just sends bare packets once we have received the 54 // address of the other side. We pick a single address to send them based on 55 // a simple investigation of NAT type. 56 class RawTransportChannel : public TransportChannelImpl, 57 public talk_base::MessageHandler { 58 public: 59 RawTransportChannel(const std::string &name, 60 const std::string &content_type, 61 RawTransport* transport, 62 talk_base::Thread *worker_thread, 63 PortAllocator *allocator); 64 virtual ~RawTransportChannel(); 65 66 // Implementation of normal channel packet sending. 67 virtual int SendPacket(const char *data, size_t len); 68 virtual int SetOption(talk_base::Socket::Option opt, int value); 69 virtual int GetError(); 70 71 // Returns the raw transport that created this channel. 72 virtual Transport* GetTransport() { return raw_transport_; } 73 74 // Creates an allocator session to start figuring out which type of 75 // port we should send to the other client. This will send 76 // SignalAvailableCandidate once we have decided. 77 virtual void Connect(); 78 79 // Resets state back to unconnected. 80 virtual void Reset(); 81 82 // We don't actually worry about signaling since we can't send new candidates. 83 virtual void OnSignalingReady() {} 84 85 // Handles a message setting the remote address. We are writable once we 86 // have this since we now know where to send. 87 virtual void OnCandidate(const Candidate& candidate); 88 89 void OnRemoteAddress(const talk_base::SocketAddress& remote_address); 90 91 92 private: 93 RawTransport* raw_transport_; 94 talk_base::Thread *worker_thread_; 95 PortAllocator* allocator_; 96 PortAllocatorSession* allocator_session_; 97 StunPort* stun_port_; 98 RelayPort* relay_port_; 99 Port* port_; 100 bool use_relay_; 101 talk_base::SocketAddress remote_address_; 102 103 // Called when the allocator creates another port. 104 void OnPortReady(PortAllocatorSession* session, Port* port); 105 106 // Called when one of the ports we are using has determined its address. 107 void OnCandidatesReady(PortAllocatorSession *session, 108 const std::vector<Candidate>& candidates); 109 110 // Called once we have chosen the port to use for communication with the 111 // other client. This will send its address and prepare the port for use. 112 void SetPort(Port* port); 113 114 // Called once we have a port and a remote address. This will set mark the 115 // channel as writable and signal the route to the client. 116 void SetWritable(); 117 118 // Called when we receive a packet from the other client. 119 void OnReadPacket(Port* port, const char* data, size_t size, 120 const talk_base::SocketAddress& addr); 121 122 // Handles a message to destroy unused ports. 123 virtual void OnMessage(talk_base::Message *msg); 124 125 DISALLOW_EVIL_CONSTRUCTORS(RawTransportChannel); 126 }; 127 128 } // namespace cricket 129 130 #endif // defined(FEATURE_ENABLE_PSTN) 131 #endif // TALK_P2P_BASE_RAWTRANSPORTCHANNEL_H_ 132