You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
9.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include <iomanip>
  28. #include "talk/base/asyncsocket.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/socketfactory.h"
  31. #include "talk/base/socketpool.h"
  32. #include "talk/base/socketstream.h"
  33. #include "talk/base/thread.h"
  34. namespace talk_base {
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // StreamCache - Caches a set of open streams, defers creation to a separate
  37. // StreamPool.
  38. ///////////////////////////////////////////////////////////////////////////////
  39. StreamCache::StreamCache(StreamPool* pool) : pool_(pool) {
  40. }
  41. StreamCache::~StreamCache() {
  42. for (ConnectedList::iterator it = active_.begin(); it != active_.end();
  43. ++it) {
  44. delete it->second;
  45. }
  46. for (ConnectedList::iterator it = cached_.begin(); it != cached_.end();
  47. ++it) {
  48. delete it->second;
  49. }
  50. }
  51. StreamInterface* StreamCache::RequestConnectedStream(
  52. const SocketAddress& remote, int* err) {
  53. LOG_F(LS_VERBOSE) << "(" << remote << ")";
  54. for (ConnectedList::iterator it = cached_.begin(); it != cached_.end();
  55. ++it) {
  56. if (remote == it->first) {
  57. it->second->SignalEvent.disconnect(this);
  58. // Move from cached_ to active_
  59. active_.push_front(*it);
  60. cached_.erase(it);
  61. if (err)
  62. *err = 0;
  63. LOG_F(LS_VERBOSE) << "Providing cached stream";
  64. return active_.front().second;
  65. }
  66. }
  67. if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) {
  68. // We track active streams so that we can remember their address
  69. active_.push_front(ConnectedStream(remote, stream));
  70. LOG_F(LS_VERBOSE) << "Providing new stream";
  71. return active_.front().second;
  72. }
  73. return NULL;
  74. }
  75. void StreamCache::ReturnConnectedStream(StreamInterface* stream) {
  76. for (ConnectedList::iterator it = active_.begin(); it != active_.end();
  77. ++it) {
  78. if (stream == it->second) {
  79. LOG_F(LS_VERBOSE) << "(" << it->first << ")";
  80. if (stream->GetState() == SS_CLOSED) {
  81. // Return closed streams
  82. LOG_F(LS_VERBOSE) << "Returning closed stream";
  83. pool_->ReturnConnectedStream(it->second);
  84. } else {
  85. // Monitor open streams
  86. stream->SignalEvent.connect(this, &StreamCache::OnStreamEvent);
  87. LOG_F(LS_VERBOSE) << "Caching stream";
  88. cached_.push_front(*it);
  89. }
  90. active_.erase(it);
  91. return;
  92. }
  93. }
  94. ASSERT(false);
  95. }
  96. void StreamCache::OnStreamEvent(StreamInterface* stream, int events, int err) {
  97. if ((events & SE_CLOSE) == 0) {
  98. LOG_F(LS_WARNING) << "(" << events << ", " << err
  99. << ") received non-close event";
  100. return;
  101. }
  102. for (ConnectedList::iterator it = cached_.begin(); it != cached_.end();
  103. ++it) {
  104. if (stream == it->second) {
  105. LOG_F(LS_VERBOSE) << "(" << it->first << ")";
  106. // We don't cache closed streams, so return it.
  107. it->second->SignalEvent.disconnect(this);
  108. LOG_F(LS_VERBOSE) << "Returning closed stream";
  109. pool_->ReturnConnectedStream(it->second);
  110. cached_.erase(it);
  111. return;
  112. }
  113. }
  114. ASSERT(false);
  115. }
  116. //////////////////////////////////////////////////////////////////////
  117. // NewSocketPool
  118. //////////////////////////////////////////////////////////////////////
  119. NewSocketPool::NewSocketPool(SocketFactory* factory) : factory_(factory) {
  120. }
  121. NewSocketPool::~NewSocketPool() {
  122. }
  123. StreamInterface*
  124. NewSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) {
  125. AsyncSocket* socket =
  126. factory_->CreateAsyncSocket(remote.family(), SOCK_STREAM);
  127. if (!socket) {
  128. if (err)
  129. *err = -1;
  130. return NULL;
  131. }
  132. if ((socket->Connect(remote) != 0) && !socket->IsBlocking()) {
  133. if (err)
  134. *err = socket->GetError();
  135. delete socket;
  136. return NULL;
  137. }
  138. if (err)
  139. *err = 0;
  140. return new SocketStream(socket);
  141. }
  142. void
  143. NewSocketPool::ReturnConnectedStream(StreamInterface* stream) {
  144. Thread::Current()->Dispose(stream);
  145. }
  146. //////////////////////////////////////////////////////////////////////
  147. // ReuseSocketPool
  148. //////////////////////////////////////////////////////////////////////
  149. ReuseSocketPool::ReuseSocketPool(SocketFactory* factory)
  150. : factory_(factory), stream_(NULL), checked_out_(false) {
  151. }
  152. ReuseSocketPool::~ReuseSocketPool() {
  153. ASSERT(!checked_out_);
  154. delete stream_;
  155. }
  156. StreamInterface*
  157. ReuseSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) {
  158. // Only one socket can be used from this "pool" at a time
  159. ASSERT(!checked_out_);
  160. if (!stream_) {
  161. LOG_F(LS_VERBOSE) << "Creating new socket";
  162. int family = remote.family();
  163. // TODO: Deal with this when we/I clean up DNS resolution.
  164. if (remote.IsUnresolvedIP()) {
  165. family = AF_INET;
  166. }
  167. AsyncSocket* socket =
  168. factory_->CreateAsyncSocket(family, SOCK_STREAM);
  169. if (!socket) {
  170. if (err)
  171. *err = -1;
  172. return NULL;
  173. }
  174. stream_ = new SocketStream(socket);
  175. }
  176. if ((stream_->GetState() == SS_OPEN) && (remote == remote_)) {
  177. LOG_F(LS_VERBOSE) << "Reusing connection to: " << remote_;
  178. } else {
  179. remote_ = remote;
  180. stream_->Close();
  181. if ((stream_->GetSocket()->Connect(remote_) != 0)
  182. && !stream_->GetSocket()->IsBlocking()) {
  183. if (err)
  184. *err = stream_->GetSocket()->GetError();
  185. return NULL;
  186. } else {
  187. LOG_F(LS_VERBOSE) << "Opening connection to: " << remote_;
  188. }
  189. }
  190. stream_->SignalEvent.disconnect(this);
  191. checked_out_ = true;
  192. if (err)
  193. *err = 0;
  194. return stream_;
  195. }
  196. void
  197. ReuseSocketPool::ReturnConnectedStream(StreamInterface* stream) {
  198. ASSERT(stream == stream_);
  199. ASSERT(checked_out_);
  200. checked_out_ = false;
  201. // Until the socket is reused, monitor it to determine if it closes.
  202. stream_->SignalEvent.connect(this, &ReuseSocketPool::OnStreamEvent);
  203. }
  204. void
  205. ReuseSocketPool::OnStreamEvent(StreamInterface* stream, int events, int err) {
  206. ASSERT(stream == stream_);
  207. ASSERT(!checked_out_);
  208. // If the stream was written to and then immediately returned to us then
  209. // we may get a writable notification for it, which we should ignore.
  210. if (events == SE_WRITE) {
  211. LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly writable: ignoring";
  212. return;
  213. }
  214. // If the peer sent data, we can't process it, so drop the connection.
  215. // If the socket has closed, clean it up.
  216. // In either case, we'll reconnect it the next time it is used.
  217. ASSERT(0 != (events & (SE_READ|SE_CLOSE)));
  218. if (0 != (events & SE_CLOSE)) {
  219. LOG_F(LS_VERBOSE) << "Connection closed with error: " << err;
  220. } else {
  221. LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly readable: closing";
  222. }
  223. stream_->Close();
  224. }
  225. ///////////////////////////////////////////////////////////////////////////////
  226. // LoggingPoolAdapter - Adapts a StreamPool to supply streams with attached
  227. // LoggingAdapters.
  228. ///////////////////////////////////////////////////////////////////////////////
  229. LoggingPoolAdapter::LoggingPoolAdapter(
  230. StreamPool* pool, LoggingSeverity level, const std::string& label,
  231. bool binary_mode)
  232. : pool_(pool), level_(level), label_(label), binary_mode_(binary_mode) {
  233. }
  234. LoggingPoolAdapter::~LoggingPoolAdapter() {
  235. for (StreamList::iterator it = recycle_bin_.begin();
  236. it != recycle_bin_.end(); ++it) {
  237. delete *it;
  238. }
  239. }
  240. StreamInterface* LoggingPoolAdapter::RequestConnectedStream(
  241. const SocketAddress& remote, int* err) {
  242. if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) {
  243. ASSERT(SS_CLOSED != stream->GetState());
  244. std::stringstream ss;
  245. ss << label_ << "(0x" << std::setfill('0') << std::hex << std::setw(8)
  246. << stream << ")";
  247. LOG_V(level_) << ss.str()
  248. << ((SS_OPEN == stream->GetState()) ? " Connected"
  249. : " Connecting")
  250. << " to " << remote;
  251. if (recycle_bin_.empty()) {
  252. return new LoggingAdapter(stream, level_, ss.str(), binary_mode_);
  253. }
  254. LoggingAdapter* logging = recycle_bin_.front();
  255. recycle_bin_.pop_front();
  256. logging->set_label(ss.str());
  257. logging->Attach(stream);
  258. return logging;
  259. }
  260. return NULL;
  261. }
  262. void LoggingPoolAdapter::ReturnConnectedStream(StreamInterface* stream) {
  263. LoggingAdapter* logging = static_cast<LoggingAdapter*>(stream);
  264. pool_->ReturnConnectedStream(logging->Detach());
  265. recycle_bin_.push_back(logging);
  266. }
  267. ///////////////////////////////////////////////////////////////////////////////
  268. } // namespace talk_base