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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 * |Writable: N|OnConnect |Writable: N|Completed |Writable: Y| | 60 * |Writable: N|OnConnect |Writable: N|Completed |Writable: Y| |
61 * |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N| | 61 * |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N| |
62 * |PretendWri:N| |PretendWri:N| |PretendWri:N| | 62 * |PretendWri:N| |PretendWri:N| |PretendWri:N| |
63 * +------------+ +------------+ +------------+ | 63 * +------------+ +------------+ +------------+ |
64 * | 64 * |
65 */ | 65 */ |
66 | 66 |
67 #include "webrtc/p2p/base/tcpport.h" | 67 #include "webrtc/p2p/base/tcpport.h" |
68 | 68 |
69 #include "webrtc/p2p/base/common.h" | 69 #include "webrtc/p2p/base/common.h" |
| 70 #include "webrtc/base/checks.h" |
70 #include "webrtc/base/common.h" | 71 #include "webrtc/base/common.h" |
71 #include "webrtc/base/logging.h" | 72 #include "webrtc/base/logging.h" |
72 | 73 |
73 namespace cricket { | 74 namespace cricket { |
74 | 75 |
75 TCPPort::TCPPort(rtc::Thread* thread, | 76 TCPPort::TCPPort(rtc::Thread* thread, |
76 rtc::PacketSocketFactory* factory, | 77 rtc::PacketSocketFactory* factory, |
77 rtc::Network* network, | 78 rtc::Network* network, |
78 const rtc::IPAddress& ip, | 79 const rtc::IPAddress& ip, |
79 uint16_t min_port, | 80 uint16_t min_port, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 return SOCKET_ERROR; | 245 return SOCKET_ERROR; |
245 } | 246 } |
246 } | 247 } |
247 | 248 |
248 int TCPPort::GetError() { | 249 int TCPPort::GetError() { |
249 return error_; | 250 return error_; |
250 } | 251 } |
251 | 252 |
252 void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket, | 253 void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket, |
253 rtc::AsyncPacketSocket* new_socket) { | 254 rtc::AsyncPacketSocket* new_socket) { |
254 ASSERT(socket == socket_); | 255 RTC_DCHECK(socket == socket_); |
255 | 256 |
256 Incoming incoming; | 257 Incoming incoming; |
257 incoming.addr = new_socket->GetRemoteAddress(); | 258 incoming.addr = new_socket->GetRemoteAddress(); |
258 incoming.socket = new_socket; | 259 incoming.socket = new_socket; |
259 incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); | 260 incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); |
260 incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); | 261 incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); |
261 incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket); | 262 incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket); |
262 | 263 |
263 LOG_J(LS_VERBOSE, this) << "Accepted connection from " | 264 LOG_J(LS_VERBOSE, this) << "Accepted connection from " |
264 << incoming.addr.ToSensitiveString(); | 265 << incoming.addr.ToSensitiveString(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 connection_pending_(false), | 314 connection_pending_(false), |
314 pretending_to_be_writable_(false), | 315 pretending_to_be_writable_(false), |
315 reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) { | 316 reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) { |
316 if (outgoing_) { | 317 if (outgoing_) { |
317 CreateOutgoingTcpSocket(); | 318 CreateOutgoingTcpSocket(); |
318 } else { | 319 } else { |
319 // Incoming connections should match the network address. | 320 // Incoming connections should match the network address. |
320 LOG_J(LS_VERBOSE, this) | 321 LOG_J(LS_VERBOSE, this) |
321 << "socket ipaddr: " << socket_->GetLocalAddress().ToString() | 322 << "socket ipaddr: " << socket_->GetLocalAddress().ToString() |
322 << ",port() ip:" << port->ip().ToString(); | 323 << ",port() ip:" << port->ip().ToString(); |
323 ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip()); | 324 RTC_DCHECK(socket_->GetLocalAddress().ipaddr() == port->ip()); |
324 ConnectSocketSignals(socket); | 325 ConnectSocketSignals(socket); |
325 } | 326 } |
326 } | 327 } |
327 | 328 |
328 TCPConnection::~TCPConnection() { | 329 TCPConnection::~TCPConnection() { |
329 } | 330 } |
330 | 331 |
331 int TCPConnection::Send(const void* data, size_t size, | 332 int TCPConnection::Send(const void* data, size_t size, |
332 const rtc::PacketOptions& options) { | 333 const rtc::PacketOptions& options) { |
333 if (!socket_) { | 334 if (!socket_) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 // Process the STUN response before we inform upper layer ready to send. | 372 // Process the STUN response before we inform upper layer ready to send. |
372 Connection::OnConnectionRequestResponse(req, response); | 373 Connection::OnConnectionRequestResponse(req, response); |
373 | 374 |
374 // If we're in the state of pretending to be writeable, we should inform the | 375 // If we're in the state of pretending to be writeable, we should inform the |
375 // upper layer it's ready to send again as previous EWOULDLBLOCK from socket | 376 // upper layer it's ready to send again as previous EWOULDLBLOCK from socket |
376 // would have stopped the outgoing stream. | 377 // would have stopped the outgoing stream. |
377 if (pretending_to_be_writable_) { | 378 if (pretending_to_be_writable_) { |
378 Connection::OnReadyToSend(); | 379 Connection::OnReadyToSend(); |
379 } | 380 } |
380 pretending_to_be_writable_ = false; | 381 pretending_to_be_writable_ = false; |
381 ASSERT(write_state() == STATE_WRITABLE); | 382 RTC_DCHECK(write_state() == STATE_WRITABLE); |
382 } | 383 } |
383 | 384 |
384 void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { | 385 void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { |
385 ASSERT(socket == socket_.get()); | 386 RTC_DCHECK(socket == socket_.get()); |
386 // Do not use this connection if the socket bound to a different address than | 387 // Do not use this connection if the socket bound to a different address than |
387 // the one we asked for. This is seen in Chrome, where TCP sockets cannot be | 388 // the one we asked for. This is seen in Chrome, where TCP sockets cannot be |
388 // given a binding address, and the platform is expected to pick the | 389 // given a binding address, and the platform is expected to pick the |
389 // correct local address. | 390 // correct local address. |
390 const rtc::SocketAddress& socket_addr = socket->GetLocalAddress(); | 391 const rtc::SocketAddress& socket_addr = socket->GetLocalAddress(); |
391 if (socket_addr.ipaddr() == port()->ip()) { | 392 if (socket_addr.ipaddr() == port()->ip()) { |
392 LOG_J(LS_VERBOSE, this) << "Connection established to " | 393 LOG_J(LS_VERBOSE, this) << "Connection established to " |
393 << socket->GetRemoteAddress().ToSensitiveString(); | 394 << socket->GetRemoteAddress().ToSensitiveString(); |
394 } else if (IPIsAny(port()->ip())) { | 395 } else if (IPIsAny(port()->ip())) { |
395 LOG(LS_WARNING) << "Socket is bound to a different address:" | 396 LOG(LS_WARNING) << "Socket is bound to a different address:" |
(...skipping 16 matching lines...) Expand all Loading... |
412 OnClose(socket, 0); | 413 OnClose(socket, 0); |
413 return; | 414 return; |
414 } | 415 } |
415 | 416 |
416 // Connection is established successfully. | 417 // Connection is established successfully. |
417 set_connected(true); | 418 set_connected(true); |
418 connection_pending_ = false; | 419 connection_pending_ = false; |
419 } | 420 } |
420 | 421 |
421 void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) { | 422 void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) { |
422 ASSERT(socket == socket_.get()); | 423 RTC_DCHECK(socket == socket_.get()); |
423 LOG_J(LS_INFO, this) << "Connection closed with error " << error; | 424 LOG_J(LS_INFO, this) << "Connection closed with error " << error; |
424 | 425 |
425 // Guard against the condition where IPC socket will call OnClose for every | 426 // Guard against the condition where IPC socket will call OnClose for every |
426 // packet it can't send. | 427 // packet it can't send. |
427 if (connected()) { | 428 if (connected()) { |
428 set_connected(false); | 429 set_connected(false); |
429 | 430 |
430 // Prevent the connection from being destroyed by redundant SignalClose | 431 // Prevent the connection from being destroyed by redundant SignalClose |
431 // events. | 432 // events. |
432 pretending_to_be_writable_ = true; | 433 pretending_to_be_writable_ = true; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 << "trying to reconnect"; | 472 << "trying to reconnect"; |
472 | 473 |
473 CreateOutgoingTcpSocket(); | 474 CreateOutgoingTcpSocket(); |
474 error_ = EPIPE; | 475 error_ = EPIPE; |
475 } | 476 } |
476 | 477 |
477 void TCPConnection::OnReadPacket( | 478 void TCPConnection::OnReadPacket( |
478 rtc::AsyncPacketSocket* socket, const char* data, size_t size, | 479 rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
479 const rtc::SocketAddress& remote_addr, | 480 const rtc::SocketAddress& remote_addr, |
480 const rtc::PacketTime& packet_time) { | 481 const rtc::PacketTime& packet_time) { |
481 ASSERT(socket == socket_.get()); | 482 RTC_DCHECK(socket == socket_.get()); |
482 Connection::OnReadPacket(data, size, packet_time); | 483 Connection::OnReadPacket(data, size, packet_time); |
483 } | 484 } |
484 | 485 |
485 void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { | 486 void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
486 ASSERT(socket == socket_.get()); | 487 RTC_DCHECK(socket == socket_.get()); |
487 Connection::OnReadyToSend(); | 488 Connection::OnReadyToSend(); |
488 } | 489 } |
489 | 490 |
490 void TCPConnection::CreateOutgoingTcpSocket() { | 491 void TCPConnection::CreateOutgoingTcpSocket() { |
491 ASSERT(outgoing_); | 492 RTC_DCHECK(outgoing_); |
492 // TODO(guoweis): Handle failures here (unlikely since TCP). | 493 // TODO(guoweis): Handle failures here (unlikely since TCP). |
493 int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME) | 494 int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME) |
494 ? rtc::PacketSocketFactory::OPT_TLS_FAKE | 495 ? rtc::PacketSocketFactory::OPT_TLS_FAKE |
495 : 0; | 496 : 0; |
496 socket_.reset(port()->socket_factory()->CreateClientTcpSocket( | 497 socket_.reset(port()->socket_factory()->CreateClientTcpSocket( |
497 rtc::SocketAddress(port()->ip(), 0), remote_candidate().address(), | 498 rtc::SocketAddress(port()->ip(), 0), remote_candidate().address(), |
498 port()->proxy(), port()->user_agent(), opts)); | 499 port()->proxy(), port()->user_agent(), opts)); |
499 if (socket_) { | 500 if (socket_) { |
500 LOG_J(LS_VERBOSE, this) | 501 LOG_J(LS_VERBOSE, this) |
501 << "Connecting from " << socket_->GetLocalAddress().ToSensitiveString() | 502 << "Connecting from " << socket_->GetLocalAddress().ToSensitiveString() |
(...skipping 10 matching lines...) Expand all Loading... |
512 void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) { | 513 void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) { |
513 if (outgoing_) { | 514 if (outgoing_) { |
514 socket->SignalConnect.connect(this, &TCPConnection::OnConnect); | 515 socket->SignalConnect.connect(this, &TCPConnection::OnConnect); |
515 } | 516 } |
516 socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); | 517 socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); |
517 socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); | 518 socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); |
518 socket->SignalClose.connect(this, &TCPConnection::OnClose); | 519 socket->SignalClose.connect(this, &TCPConnection::OnClose); |
519 } | 520 } |
520 | 521 |
521 } // namespace cricket | 522 } // namespace cricket |
OLD | NEW |