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_TRANSPORTCHANNEL_H_ 29 #define TALK_P2P_BASE_TRANSPORTCHANNEL_H_ 30 31 #include <string> 32 #include "talk/base/basictypes.h" 33 #include "talk/base/sigslot.h" 34 #include "talk/base/socket.h" 35 36 namespace cricket { 37 38 class P2PTransportChannel; 39 40 // A TransportChannel represents one logical stream of packets that are sent 41 // between the two sides of a session. 42 class TransportChannel: public sigslot::has_slots<> { 43 public: TransportChannel(const std::string & name,const std::string & content_type)44 TransportChannel(const std::string& name, const std::string &content_type) 45 : name_(name), content_type_(content_type), 46 readable_(false), writable_(false) {} ~TransportChannel()47 virtual ~TransportChannel() {} 48 49 // Returns the name of this channel. name()50 const std::string& name() const { return name_; } content_type()51 const std::string& content_type() const { return content_type_; } 52 53 // Returns the readable and states of this channel. Each time one of these 54 // states changes, a signal is raised. These states are aggregated by the 55 // TransportManager. readable()56 bool readable() const { return readable_; } writable()57 bool writable() const { return writable_; } 58 sigslot::signal1<TransportChannel*> SignalReadableState; 59 sigslot::signal1<TransportChannel*> SignalWritableState; 60 61 // Attempts to send the given packet. The return value is < 0 on failure. 62 virtual int SendPacket(const char *data, size_t len) = 0; 63 64 // Sets a socket option on this channel. Note that not all options are 65 // supported by all transport types. 66 virtual int SetOption(talk_base::Socket::Option opt, int value) = 0; 67 68 // Returns the most recent error that occurred on this channel. 69 virtual int GetError() = 0; 70 71 // This hack is here to allow the SocketMonitor to downcast to the 72 // P2PTransportChannel safely. 73 // TODO: Generalize network monitoring. GetP2PChannel()74 virtual P2PTransportChannel* GetP2PChannel() { return NULL; } 75 76 // Signalled each time a packet is received on this channel. 77 sigslot::signal3<TransportChannel*, const char*, size_t> SignalReadPacket; 78 79 // This signal occurs when there is a change in the way that packets are 80 // being routed. The address indicates the address of the first hop in the 81 // new route, if this is known. If this cannot be determined or is not well- 82 // defined, then the channel may give an address of 0. 83 sigslot::signal2<TransportChannel*, const talk_base::SocketAddress&> 84 SignalRouteChange; 85 86 // Invoked when the channel is being destroyed. 87 sigslot::signal1<TransportChannel*> SignalDestroyed; 88 89 // Debugging description of this transport channel. 90 std::string ToString() const; 91 92 protected: 93 // Sets the readable state, signaling if necessary. 94 void set_readable(bool readable); 95 96 // Sets the writable state, signaling if necessary. 97 void set_writable(bool writable); 98 99 private: 100 std::string name_; 101 std::string content_type_; 102 bool readable_; 103 bool writable_; 104 105 DISALLOW_EVIL_CONSTRUCTORS(TransportChannel); 106 }; 107 108 } // namespace cricket 109 110 #endif // TALK_P2P_BASE_TRANSPORTCHANNEL_H_ 111