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_BASE_SOCKETPOOL_H_ 29 #define TALK_BASE_SOCKETPOOL_H_ 30 31 #include <deque> 32 #include <list> 33 #include "talk/base/logging.h" 34 #include "talk/base/sigslot.h" 35 #include "talk/base/socketaddress.h" 36 37 namespace talk_base { 38 39 class AsyncSocket; 40 class LoggingAdapter; 41 class SocketFactory; 42 class SocketStream; 43 class StreamInterface; 44 45 ////////////////////////////////////////////////////////////////////// 46 // StreamPool 47 ////////////////////////////////////////////////////////////////////// 48 49 class StreamPool { 50 public: 51 virtual ~StreamPool() { } 52 53 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, 54 int* err) = 0; 55 virtual void ReturnConnectedStream(StreamInterface* stream) = 0; 56 }; 57 58 /////////////////////////////////////////////////////////////////////////////// 59 // StreamCache - Caches a set of open streams, defers creation/destruction to 60 // the supplied StreamPool. 61 /////////////////////////////////////////////////////////////////////////////// 62 63 class StreamCache : public StreamPool, public sigslot::has_slots<> { 64 public: 65 StreamCache(StreamPool* pool); 66 virtual ~StreamCache(); 67 68 // StreamPool Interface 69 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, 70 int* err); 71 virtual void ReturnConnectedStream(StreamInterface* stream); 72 73 private: 74 typedef std::pair<SocketAddress, StreamInterface*> ConnectedStream; 75 typedef std::list<ConnectedStream> ConnectedList; 76 77 void OnStreamEvent(StreamInterface* stream, int events, int err); 78 79 // We delegate stream creation and deletion to this pool. 80 StreamPool* pool_; 81 // Streams that are in use (returned from RequestConnectedStream). 82 ConnectedList active_; 83 // Streams which were returned to us, but are still open. 84 ConnectedList cached_; 85 }; 86 87 /////////////////////////////////////////////////////////////////////////////// 88 // NewSocketPool 89 // Creates a new stream on every request 90 /////////////////////////////////////////////////////////////////////////////// 91 92 class NewSocketPool : public StreamPool { 93 public: 94 NewSocketPool(SocketFactory* factory); 95 virtual ~NewSocketPool(); 96 97 // StreamPool Interface 98 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, 99 int* err); 100 virtual void ReturnConnectedStream(StreamInterface* stream); 101 102 private: 103 SocketFactory* factory_; 104 }; 105 106 /////////////////////////////////////////////////////////////////////////////// 107 // ReuseSocketPool 108 // Maintains a single socket at a time, and will reuse it without closing if 109 // the destination address is the same. 110 /////////////////////////////////////////////////////////////////////////////// 111 112 class ReuseSocketPool : public StreamPool, public sigslot::has_slots<> { 113 public: 114 ReuseSocketPool(SocketFactory* factory); 115 virtual ~ReuseSocketPool(); 116 117 // StreamPool Interface 118 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, 119 int* err); 120 virtual void ReturnConnectedStream(StreamInterface* stream); 121 122 private: 123 void OnStreamEvent(StreamInterface* stream, int events, int err); 124 125 SocketFactory* factory_; 126 SocketStream* stream_; 127 SocketAddress remote_; 128 bool checked_out_; // Whether the stream is currently checked out 129 }; 130 131 /////////////////////////////////////////////////////////////////////////////// 132 // LoggingPoolAdapter - Adapts a StreamPool to supply streams with attached 133 // LoggingAdapters. 134 /////////////////////////////////////////////////////////////////////////////// 135 136 class LoggingPoolAdapter : public StreamPool { 137 public: 138 LoggingPoolAdapter(StreamPool* pool, LoggingSeverity level, 139 const std::string& label, bool binary_mode); 140 virtual ~LoggingPoolAdapter(); 141 142 // StreamPool Interface 143 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, 144 int* err); 145 virtual void ReturnConnectedStream(StreamInterface* stream); 146 147 private: 148 StreamPool* pool_; 149 LoggingSeverity level_; 150 std::string label_; 151 bool binary_mode_; 152 typedef std::deque<LoggingAdapter*> StreamList; 153 StreamList recycle_bin_; 154 }; 155 156 ////////////////////////////////////////////////////////////////////// 157 158 } // namespace talk_base 159 160 #endif // TALK_BASE_SOCKETPOOL_H_ 161