OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 LOG_F(LS_VERBOSE) << "Providing cached stream"; | 54 LOG_F(LS_VERBOSE) << "Providing cached stream"; |
55 return active_.front().second; | 55 return active_.front().second; |
56 } | 56 } |
57 } | 57 } |
58 if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) { | 58 if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) { |
59 // We track active streams so that we can remember their address | 59 // We track active streams so that we can remember their address |
60 active_.push_front(ConnectedStream(remote, stream)); | 60 active_.push_front(ConnectedStream(remote, stream)); |
61 LOG_F(LS_VERBOSE) << "Providing new stream"; | 61 LOG_F(LS_VERBOSE) << "Providing new stream"; |
62 return active_.front().second; | 62 return active_.front().second; |
63 } | 63 } |
64 return NULL; | 64 return nullptr; |
65 } | 65 } |
66 | 66 |
67 void StreamCache::ReturnConnectedStream(StreamInterface* stream) { | 67 void StreamCache::ReturnConnectedStream(StreamInterface* stream) { |
68 for (ConnectedList::iterator it = active_.begin(); it != active_.end(); | 68 for (ConnectedList::iterator it = active_.begin(); it != active_.end(); |
69 ++it) { | 69 ++it) { |
70 if (stream == it->second) { | 70 if (stream == it->second) { |
71 LOG_F(LS_VERBOSE) << "(" << it->first << ")"; | 71 LOG_F(LS_VERBOSE) << "(" << it->first << ")"; |
72 if (stream->GetState() == SS_CLOSED) { | 72 if (stream->GetState() == SS_CLOSED) { |
73 // Return closed streams | 73 // Return closed streams |
74 LOG_F(LS_VERBOSE) << "Returning closed stream"; | 74 LOG_F(LS_VERBOSE) << "Returning closed stream"; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 NewSocketPool::~NewSocketPool() { | 117 NewSocketPool::~NewSocketPool() { |
118 } | 118 } |
119 | 119 |
120 StreamInterface* | 120 StreamInterface* |
121 NewSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { | 121 NewSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { |
122 AsyncSocket* socket = | 122 AsyncSocket* socket = |
123 factory_->CreateAsyncSocket(remote.family(), SOCK_STREAM); | 123 factory_->CreateAsyncSocket(remote.family(), SOCK_STREAM); |
124 if (!socket) { | 124 if (!socket) { |
125 if (err) | 125 if (err) |
126 *err = -1; | 126 *err = -1; |
127 return NULL; | 127 return nullptr; |
128 } | 128 } |
129 if ((socket->Connect(remote) != 0) && !socket->IsBlocking()) { | 129 if ((socket->Connect(remote) != 0) && !socket->IsBlocking()) { |
130 if (err) | 130 if (err) |
131 *err = socket->GetError(); | 131 *err = socket->GetError(); |
132 delete socket; | 132 delete socket; |
133 return NULL; | 133 return nullptr; |
134 } | 134 } |
135 if (err) | 135 if (err) |
136 *err = 0; | 136 *err = 0; |
137 return new SocketStream(socket); | 137 return new SocketStream(socket); |
138 } | 138 } |
139 | 139 |
140 void | 140 void |
141 NewSocketPool::ReturnConnectedStream(StreamInterface* stream) { | 141 NewSocketPool::ReturnConnectedStream(StreamInterface* stream) { |
142 Thread::Current()->Dispose(stream); | 142 Thread::Current()->Dispose(stream); |
143 } | 143 } |
144 | 144 |
145 ////////////////////////////////////////////////////////////////////// | 145 ////////////////////////////////////////////////////////////////////// |
146 // ReuseSocketPool | 146 // ReuseSocketPool |
147 ////////////////////////////////////////////////////////////////////// | 147 ////////////////////////////////////////////////////////////////////// |
148 | 148 |
149 ReuseSocketPool::ReuseSocketPool(SocketFactory* factory) | 149 ReuseSocketPool::ReuseSocketPool(SocketFactory* factory) |
150 : factory_(factory), stream_(NULL), checked_out_(false) { | 150 : factory_(factory), stream_(nullptr), checked_out_(false) {} |
151 } | |
152 | 151 |
153 ReuseSocketPool::~ReuseSocketPool() { | 152 ReuseSocketPool::~ReuseSocketPool() { |
154 RTC_DCHECK(!checked_out_); | 153 RTC_DCHECK(!checked_out_); |
155 delete stream_; | 154 delete stream_; |
156 } | 155 } |
157 | 156 |
158 StreamInterface* | 157 StreamInterface* |
159 ReuseSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { | 158 ReuseSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { |
160 // Only one socket can be used from this "pool" at a time | 159 // Only one socket can be used from this "pool" at a time |
161 RTC_DCHECK(!checked_out_); | 160 RTC_DCHECK(!checked_out_); |
162 if (!stream_) { | 161 if (!stream_) { |
163 LOG_F(LS_VERBOSE) << "Creating new socket"; | 162 LOG_F(LS_VERBOSE) << "Creating new socket"; |
164 int family = remote.family(); | 163 int family = remote.family(); |
165 // TODO: Deal with this when we/I clean up DNS resolution. | 164 // TODO: Deal with this when we/I clean up DNS resolution. |
166 if (remote.IsUnresolvedIP()) { | 165 if (remote.IsUnresolvedIP()) { |
167 family = AF_INET; | 166 family = AF_INET; |
168 } | 167 } |
169 AsyncSocket* socket = | 168 AsyncSocket* socket = |
170 factory_->CreateAsyncSocket(family, SOCK_STREAM); | 169 factory_->CreateAsyncSocket(family, SOCK_STREAM); |
171 if (!socket) { | 170 if (!socket) { |
172 if (err) | 171 if (err) |
173 *err = -1; | 172 *err = -1; |
174 return NULL; | 173 return nullptr; |
175 } | 174 } |
176 stream_ = new SocketStream(socket); | 175 stream_ = new SocketStream(socket); |
177 } | 176 } |
178 if ((stream_->GetState() == SS_OPEN) && (remote == remote_)) { | 177 if ((stream_->GetState() == SS_OPEN) && (remote == remote_)) { |
179 LOG_F(LS_VERBOSE) << "Reusing connection to: " << remote_; | 178 LOG_F(LS_VERBOSE) << "Reusing connection to: " << remote_; |
180 } else { | 179 } else { |
181 remote_ = remote; | 180 remote_ = remote; |
182 stream_->Close(); | 181 stream_->Close(); |
183 if ((stream_->GetSocket()->Connect(remote_) != 0) | 182 if ((stream_->GetSocket()->Connect(remote_) != 0) |
184 && !stream_->GetSocket()->IsBlocking()) { | 183 && !stream_->GetSocket()->IsBlocking()) { |
185 if (err) | 184 if (err) |
186 *err = stream_->GetSocket()->GetError(); | 185 *err = stream_->GetSocket()->GetError(); |
187 return NULL; | 186 return nullptr; |
188 } else { | 187 } else { |
189 LOG_F(LS_VERBOSE) << "Opening connection to: " << remote_; | 188 LOG_F(LS_VERBOSE) << "Opening connection to: " << remote_; |
190 } | 189 } |
191 } | 190 } |
192 stream_->SignalEvent.disconnect(this); | 191 stream_->SignalEvent.disconnect(this); |
193 checked_out_ = true; | 192 checked_out_ = true; |
194 if (err) | 193 if (err) |
195 *err = 0; | 194 *err = 0; |
196 return stream_; | 195 return stream_; |
197 } | 196 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 << " to " << remote; | 259 << " to " << remote; |
261 if (recycle_bin_.empty()) { | 260 if (recycle_bin_.empty()) { |
262 return new LoggingAdapter(stream, level_, ss.str(), binary_mode_); | 261 return new LoggingAdapter(stream, level_, ss.str(), binary_mode_); |
263 } | 262 } |
264 LoggingAdapter* logging = recycle_bin_.front(); | 263 LoggingAdapter* logging = recycle_bin_.front(); |
265 recycle_bin_.pop_front(); | 264 recycle_bin_.pop_front(); |
266 logging->set_label(ss.str()); | 265 logging->set_label(ss.str()); |
267 logging->Attach(stream); | 266 logging->Attach(stream); |
268 return logging; | 267 return logging; |
269 } | 268 } |
270 return NULL; | 269 return nullptr; |
271 } | 270 } |
272 | 271 |
273 void LoggingPoolAdapter::ReturnConnectedStream(StreamInterface* stream) { | 272 void LoggingPoolAdapter::ReturnConnectedStream(StreamInterface* stream) { |
274 LoggingAdapter* logging = static_cast<LoggingAdapter*>(stream); | 273 LoggingAdapter* logging = static_cast<LoggingAdapter*>(stream); |
275 pool_->ReturnConnectedStream(logging->Detach()); | 274 pool_->ReturnConnectedStream(logging->Detach()); |
276 recycle_bin_.push_back(logging); | 275 recycle_bin_.push_back(logging); |
277 } | 276 } |
278 | 277 |
279 /////////////////////////////////////////////////////////////////////////////// | 278 /////////////////////////////////////////////////////////////////////////////// |
280 | 279 |
281 } // namespace rtc | 280 } // namespace rtc |
OLD | NEW |