| 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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_(NULL), checked_out_(false) { |
| 151 } | 151 } |
| 152 | 152 |
| 153 ReuseSocketPool::~ReuseSocketPool() { | 153 ReuseSocketPool::~ReuseSocketPool() { |
| 154 ASSERT(!checked_out_); | 154 RTC_DCHECK(!checked_out_); |
| 155 delete stream_; | 155 delete stream_; |
| 156 } | 156 } |
| 157 | 157 |
| 158 StreamInterface* | 158 StreamInterface* |
| 159 ReuseSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { | 159 ReuseSocketPool::RequestConnectedStream(const SocketAddress& remote, int* err) { |
| 160 // Only one socket can be used from this "pool" at a time | 160 // Only one socket can be used from this "pool" at a time |
| 161 ASSERT(!checked_out_); | 161 RTC_DCHECK(!checked_out_); |
| 162 if (!stream_) { | 162 if (!stream_) { |
| 163 LOG_F(LS_VERBOSE) << "Creating new socket"; | 163 LOG_F(LS_VERBOSE) << "Creating new socket"; |
| 164 int family = remote.family(); | 164 int family = remote.family(); |
| 165 // TODO: Deal with this when we/I clean up DNS resolution. | 165 // TODO: Deal with this when we/I clean up DNS resolution. |
| 166 if (remote.IsUnresolvedIP()) { | 166 if (remote.IsUnresolvedIP()) { |
| 167 family = AF_INET; | 167 family = AF_INET; |
| 168 } | 168 } |
| 169 AsyncSocket* socket = | 169 AsyncSocket* socket = |
| 170 factory_->CreateAsyncSocket(family, SOCK_STREAM); | 170 factory_->CreateAsyncSocket(family, SOCK_STREAM); |
| 171 if (!socket) { | 171 if (!socket) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 191 } | 191 } |
| 192 stream_->SignalEvent.disconnect(this); | 192 stream_->SignalEvent.disconnect(this); |
| 193 checked_out_ = true; | 193 checked_out_ = true; |
| 194 if (err) | 194 if (err) |
| 195 *err = 0; | 195 *err = 0; |
| 196 return stream_; | 196 return stream_; |
| 197 } | 197 } |
| 198 | 198 |
| 199 void | 199 void |
| 200 ReuseSocketPool::ReturnConnectedStream(StreamInterface* stream) { | 200 ReuseSocketPool::ReturnConnectedStream(StreamInterface* stream) { |
| 201 ASSERT(stream == stream_); | 201 RTC_DCHECK(stream == stream_); |
| 202 ASSERT(checked_out_); | 202 RTC_DCHECK(checked_out_); |
| 203 checked_out_ = false; | 203 checked_out_ = false; |
| 204 // Until the socket is reused, monitor it to determine if it closes. | 204 // Until the socket is reused, monitor it to determine if it closes. |
| 205 stream_->SignalEvent.connect(this, &ReuseSocketPool::OnStreamEvent); | 205 stream_->SignalEvent.connect(this, &ReuseSocketPool::OnStreamEvent); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void | 208 void |
| 209 ReuseSocketPool::OnStreamEvent(StreamInterface* stream, int events, int err) { | 209 ReuseSocketPool::OnStreamEvent(StreamInterface* stream, int events, int err) { |
| 210 ASSERT(stream == stream_); | 210 RTC_DCHECK(stream == stream_); |
| 211 ASSERT(!checked_out_); | 211 RTC_DCHECK(!checked_out_); |
| 212 | 212 |
| 213 // If the stream was written to and then immediately returned to us then | 213 // If the stream was written to and then immediately returned to us then |
| 214 // we may get a writable notification for it, which we should ignore. | 214 // we may get a writable notification for it, which we should ignore. |
| 215 if (events == SE_WRITE) { | 215 if (events == SE_WRITE) { |
| 216 LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly writable: ignoring"; | 216 LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly writable: ignoring"; |
| 217 return; | 217 return; |
| 218 } | 218 } |
| 219 | 219 |
| 220 // If the peer sent data, we can't process it, so drop the connection. | 220 // If the peer sent data, we can't process it, so drop the connection. |
| 221 // If the socket has closed, clean it up. | 221 // If the socket has closed, clean it up. |
| 222 // In either case, we'll reconnect it the next time it is used. | 222 // In either case, we'll reconnect it the next time it is used. |
| 223 ASSERT(0 != (events & (SE_READ|SE_CLOSE))); | 223 RTC_DCHECK(0 != (events & (SE_READ | SE_CLOSE))); |
| 224 if (0 != (events & SE_CLOSE)) { | 224 if (0 != (events & SE_CLOSE)) { |
| 225 LOG_F(LS_VERBOSE) << "Connection closed with error: " << err; | 225 LOG_F(LS_VERBOSE) << "Connection closed with error: " << err; |
| 226 } else { | 226 } else { |
| 227 LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly readable: closing"; | 227 LOG_F(LS_VERBOSE) << "Pooled Socket unexpectedly readable: closing"; |
| 228 } | 228 } |
| 229 stream_->Close(); | 229 stream_->Close(); |
| 230 } | 230 } |
| 231 | 231 |
| 232 /////////////////////////////////////////////////////////////////////////////// | 232 /////////////////////////////////////////////////////////////////////////////// |
| 233 // LoggingPoolAdapter - Adapts a StreamPool to supply streams with attached | 233 // LoggingPoolAdapter - Adapts a StreamPool to supply streams with attached |
| 234 // LoggingAdapters. | 234 // LoggingAdapters. |
| 235 /////////////////////////////////////////////////////////////////////////////// | 235 /////////////////////////////////////////////////////////////////////////////// |
| 236 | 236 |
| 237 LoggingPoolAdapter::LoggingPoolAdapter( | 237 LoggingPoolAdapter::LoggingPoolAdapter( |
| 238 StreamPool* pool, LoggingSeverity level, const std::string& label, | 238 StreamPool* pool, LoggingSeverity level, const std::string& label, |
| 239 bool binary_mode) | 239 bool binary_mode) |
| 240 : pool_(pool), level_(level), label_(label), binary_mode_(binary_mode) { | 240 : pool_(pool), level_(level), label_(label), binary_mode_(binary_mode) { |
| 241 } | 241 } |
| 242 | 242 |
| 243 LoggingPoolAdapter::~LoggingPoolAdapter() { | 243 LoggingPoolAdapter::~LoggingPoolAdapter() { |
| 244 for (StreamList::iterator it = recycle_bin_.begin(); | 244 for (StreamList::iterator it = recycle_bin_.begin(); |
| 245 it != recycle_bin_.end(); ++it) { | 245 it != recycle_bin_.end(); ++it) { |
| 246 delete *it; | 246 delete *it; |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 | 249 |
| 250 StreamInterface* LoggingPoolAdapter::RequestConnectedStream( | 250 StreamInterface* LoggingPoolAdapter::RequestConnectedStream( |
| 251 const SocketAddress& remote, int* err) { | 251 const SocketAddress& remote, int* err) { |
| 252 if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) { | 252 if (StreamInterface* stream = pool_->RequestConnectedStream(remote, err)) { |
| 253 ASSERT(SS_CLOSED != stream->GetState()); | 253 RTC_DCHECK(SS_CLOSED != stream->GetState()); |
| 254 std::stringstream ss; | 254 std::stringstream ss; |
| 255 ss << label_ << "(0x" << std::setfill('0') << std::hex << std::setw(8) | 255 ss << label_ << "(0x" << std::setfill('0') << std::hex << std::setw(8) |
| 256 << stream << ")"; | 256 << stream << ")"; |
| 257 LOG_V(level_) << ss.str() | 257 LOG_V(level_) << ss.str() |
| 258 << ((SS_OPEN == stream->GetState()) ? " Connected" | 258 << ((SS_OPEN == stream->GetState()) ? " Connected" |
| 259 : " Connecting") | 259 : " Connecting") |
| 260 << " to " << remote; | 260 << " to " << remote; |
| 261 if (recycle_bin_.empty()) { | 261 if (recycle_bin_.empty()) { |
| 262 return new LoggingAdapter(stream, level_, ss.str(), binary_mode_); | 262 return new LoggingAdapter(stream, level_, ss.str(), binary_mode_); |
| 263 } | 263 } |
| 264 LoggingAdapter* logging = recycle_bin_.front(); | 264 LoggingAdapter* logging = recycle_bin_.front(); |
| 265 recycle_bin_.pop_front(); | 265 recycle_bin_.pop_front(); |
| 266 logging->set_label(ss.str()); | 266 logging->set_label(ss.str()); |
| 267 logging->Attach(stream); | 267 logging->Attach(stream); |
| 268 return logging; | 268 return logging; |
| 269 } | 269 } |
| 270 return NULL; | 270 return NULL; |
| 271 } | 271 } |
| 272 | 272 |
| 273 void LoggingPoolAdapter::ReturnConnectedStream(StreamInterface* stream) { | 273 void LoggingPoolAdapter::ReturnConnectedStream(StreamInterface* stream) { |
| 274 LoggingAdapter* logging = static_cast<LoggingAdapter*>(stream); | 274 LoggingAdapter* logging = static_cast<LoggingAdapter*>(stream); |
| 275 pool_->ReturnConnectedStream(logging->Detach()); | 275 pool_->ReturnConnectedStream(logging->Detach()); |
| 276 recycle_bin_.push_back(logging); | 276 recycle_bin_.push_back(logging); |
| 277 } | 277 } |
| 278 | 278 |
| 279 /////////////////////////////////////////////////////////////////////////////// | 279 /////////////////////////////////////////////////////////////////////////////// |
| 280 | 280 |
| 281 } // namespace rtc | 281 } // namespace rtc |
| OLD | NEW |