OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_BASE_SOCKETPOOL_H_ | |
12 #define WEBRTC_BASE_SOCKETPOOL_H_ | |
13 | |
14 #include <deque> | |
15 #include <list> | |
16 #include "webrtc/base/logging.h" | |
17 #include "webrtc/base/sigslot.h" | |
18 #include "webrtc/base/socketaddress.h" | |
19 | |
20 namespace rtc { | |
21 | |
22 class AsyncSocket; | |
23 class LoggingAdapter; | |
24 class SocketFactory; | |
25 class SocketStream; | |
26 class StreamInterface; | |
27 | |
28 ////////////////////////////////////////////////////////////////////// | |
29 // StreamPool | |
30 ////////////////////////////////////////////////////////////////////// | |
31 | |
32 class StreamPool { | |
33 public: | |
34 virtual ~StreamPool() { } | |
35 | |
36 virtual StreamInterface* RequestConnectedStream(const SocketAddress& remote, | |
37 int* err) = 0; | |
38 virtual void ReturnConnectedStream(StreamInterface* stream) = 0; | |
39 }; | |
40 | |
41 /////////////////////////////////////////////////////////////////////////////// | |
42 // StreamCache - Caches a set of open streams, defers creation/destruction to | |
43 // the supplied StreamPool. | |
44 /////////////////////////////////////////////////////////////////////////////// | |
45 | |
46 class StreamCache : public StreamPool, public sigslot::has_slots<> { | |
47 public: | |
48 StreamCache(StreamPool* pool); | |
49 ~StreamCache() override; | |
50 | |
51 // StreamPool Interface | |
52 StreamInterface* RequestConnectedStream(const SocketAddress& remote, | |
53 int* err) override; | |
54 void ReturnConnectedStream(StreamInterface* stream) override; | |
55 | |
56 private: | |
57 typedef std::pair<SocketAddress, StreamInterface*> ConnectedStream; | |
58 typedef std::list<ConnectedStream> ConnectedList; | |
59 | |
60 void OnStreamEvent(StreamInterface* stream, int events, int err); | |
61 | |
62 // We delegate stream creation and deletion to this pool. | |
63 StreamPool* pool_; | |
64 // Streams that are in use (returned from RequestConnectedStream). | |
65 ConnectedList active_; | |
66 // Streams which were returned to us, but are still open. | |
67 ConnectedList cached_; | |
68 }; | |
69 | |
70 /////////////////////////////////////////////////////////////////////////////// | |
71 // NewSocketPool | |
72 // Creates a new stream on every request | |
73 /////////////////////////////////////////////////////////////////////////////// | |
74 | |
75 class NewSocketPool : public StreamPool { | |
76 public: | |
77 NewSocketPool(SocketFactory* factory); | |
78 ~NewSocketPool() override; | |
79 | |
80 // StreamPool Interface | |
81 StreamInterface* RequestConnectedStream(const SocketAddress& remote, | |
82 int* err) override; | |
83 void ReturnConnectedStream(StreamInterface* stream) override; | |
84 | |
85 private: | |
86 SocketFactory* factory_; | |
87 }; | |
88 | |
89 /////////////////////////////////////////////////////////////////////////////// | |
90 // ReuseSocketPool | |
91 // Maintains a single socket at a time, and will reuse it without closing if | |
92 // the destination address is the same. | |
93 /////////////////////////////////////////////////////////////////////////////// | |
94 | |
95 class ReuseSocketPool : public StreamPool, public sigslot::has_slots<> { | |
96 public: | |
97 ReuseSocketPool(SocketFactory* factory); | |
98 ~ReuseSocketPool() override; | |
99 | |
100 // StreamPool Interface | |
101 StreamInterface* RequestConnectedStream(const SocketAddress& remote, | |
102 int* err) override; | |
103 void ReturnConnectedStream(StreamInterface* stream) override; | |
104 | |
105 private: | |
106 void OnStreamEvent(StreamInterface* stream, int events, int err); | |
107 | |
108 SocketFactory* factory_; | |
109 SocketStream* stream_; | |
110 SocketAddress remote_; | |
111 bool checked_out_; // Whether the stream is currently checked out | |
112 }; | |
113 | |
114 /////////////////////////////////////////////////////////////////////////////// | |
115 // LoggingPoolAdapter - Adapts a StreamPool to supply streams with attached | |
116 // LoggingAdapters. | |
117 /////////////////////////////////////////////////////////////////////////////// | |
118 | |
119 class LoggingPoolAdapter : public StreamPool { | |
120 public: | |
121 LoggingPoolAdapter(StreamPool* pool, LoggingSeverity level, | |
122 const std::string& label, bool binary_mode); | |
123 ~LoggingPoolAdapter() override; | |
124 | |
125 // StreamPool Interface | |
126 StreamInterface* RequestConnectedStream(const SocketAddress& remote, | |
127 int* err) override; | |
128 void ReturnConnectedStream(StreamInterface* stream) override; | |
129 | |
130 private: | |
131 StreamPool* pool_; | |
132 LoggingSeverity level_; | |
133 std::string label_; | |
134 bool binary_mode_; | |
135 typedef std::deque<LoggingAdapter*> StreamList; | |
136 StreamList recycle_bin_; | |
137 }; | |
138 | |
139 ////////////////////////////////////////////////////////////////////// | |
140 | |
141 } // namespace rtc | |
142 | |
143 #endif // WEBRTC_BASE_SOCKETPOOL_H_ | |
OLD | NEW |