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.

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