henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 11 | /* |
| 12 | * This is a diagram of how TCP reconnect works for the active side. The |
| 13 | * passive side just waits for an incoming connection. |
| 14 | * |
| 15 | * - Connected: Indicate whether the TCP socket is connected. |
| 16 | * |
| 17 | * - Writable: Whether the stun binding is completed. Sending a data packet |
| 18 | * before stun binding completed will trigger IPC socket layer to shutdown |
| 19 | * the connection. |
| 20 | * |
| 21 | * - PendingTCP: |connection_pending_| indicates whether there is an |
| 22 | * outstanding TCP connection in progress. |
| 23 | * |
| 24 | * - PretendWri: Tracked by |pretending_to_be_writable_|. Marking connection as |
| 25 | * WRITE_TIMEOUT will cause the connection be deleted. Instead, we're |
| 26 | * "pretending" we're still writable for a period of time such that reconnect |
| 27 | * could work. |
| 28 | * |
| 29 | * Data could only be sent in state 3. Sening data during state 2 & 6 will get |
| 30 | * EWOULDBLOCK, 4 & 5 EPIPE. |
| 31 | * |
| 32 | * 7 -------------+ |
| 33 | * |Connected: N | |
| 34 | * Timeout |Writable: N | Timeout |
| 35 | * +------------------->|Connection is |<----------------+ |
| 36 | * | |Dead | | |
| 37 | * | +--------------+ | |
| 38 | * | ^ | |
| 39 | * | OnClose | | |
| 40 | * | +-----------------------+ | | |
| 41 | * | | | |Timeout | |
| 42 | * | v | | | |
| 43 | * 4 +----------+ 5 -----+--+--+ 6 -----+-----+ |
| 44 | * |Connected: N|Send() or |Connected: N| |Connected: Y| |
| 45 | * |Writable: Y|Ping() |Writable: Y|OnConnect |Writable: Y| |
| 46 | * |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N| |
| 47 | * |PretendWri:Y| |PretendWri:Y| |PretendWri:Y| |
| 48 | * +-----+------+ +------------+ +---+--+-----+ |
| 49 | * ^ ^ | | |
| 50 | * | | OnClose | | |
| 51 | * | +----------------------------------------------+ | |
| 52 | * | | |
| 53 | * | Stun Binding Completed | |
| 54 | * | | |
| 55 | * | OnClose | |
| 56 | * +------------------------------------------------+ | |
| 57 | * | v |
| 58 | * 1 -----------+ 2 -----------+Stun 3 -----------+ |
| 59 | * |Connected: N| |Connected: Y|Binding |Connected: Y| |
| 60 | * |Writable: N|OnConnect |Writable: N|Completed |Writable: Y| |
| 61 | * |PendingTCP:Y+---------> |PendingTCP:N+--------> |PendingTCP:N| |
| 62 | * |PretendWri:N| |PretendWri:N| |PretendWri:N| |
| 63 | * +------------+ +------------+ +------------+ |
| 64 | * |
| 65 | */ |
| 66 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | #include "webrtc/p2p/base/tcpport.h" |
| 68 | |
| 69 | #include "webrtc/p2p/base/common.h" |
| 70 | #include "webrtc/base/common.h" |
| 71 | #include "webrtc/base/logging.h" |
| 72 | |
| 73 | namespace cricket { |
| 74 | |
| 75 | TCPPort::TCPPort(rtc::Thread* thread, |
| 76 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 77 | rtc::Network* network, |
| 78 | const rtc::IPAddress& ip, |
| 79 | uint16 min_port, |
| 80 | uint16 max_port, |
| 81 | const std::string& username, |
| 82 | const std::string& password, |
| 83 | bool allow_listen) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 84 | : Port(thread, LOCAL_PORT_TYPE, factory, network, ip, min_port, max_port, |
| 85 | username, password), |
| 86 | incoming_only_(false), |
| 87 | allow_listen_(allow_listen), |
| 88 | socket_(NULL), |
| 89 | error_(0) { |
| 90 | // TODO(mallinath) - Set preference value as per RFC 6544. |
| 91 | // http://b/issue?id=7141794 |
| 92 | } |
| 93 | |
| 94 | bool TCPPort::Init() { |
| 95 | if (allow_listen_) { |
| 96 | // Treat failure to create or bind a TCP socket as fatal. This |
| 97 | // should never happen. |
| 98 | socket_ = socket_factory()->CreateServerTcpSocket( |
| 99 | rtc::SocketAddress(ip(), 0), min_port(), max_port(), |
| 100 | false /* ssl */); |
| 101 | if (!socket_) { |
| 102 | LOG_J(LS_ERROR, this) << "TCP socket creation failed."; |
| 103 | return false; |
| 104 | } |
| 105 | socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection); |
| 106 | socket_->SignalAddressReady.connect(this, &TCPPort::OnAddressReady); |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | TCPPort::~TCPPort() { |
| 112 | delete socket_; |
| 113 | std::list<Incoming>::iterator it; |
| 114 | for (it = incoming_.begin(); it != incoming_.end(); ++it) |
| 115 | delete it->socket; |
| 116 | incoming_.clear(); |
| 117 | } |
| 118 | |
| 119 | Connection* TCPPort::CreateConnection(const Candidate& address, |
| 120 | CandidateOrigin origin) { |
| 121 | // We only support TCP protocols |
| 122 | if ((address.protocol() != TCP_PROTOCOL_NAME) && |
| 123 | (address.protocol() != SSLTCP_PROTOCOL_NAME)) { |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | if (address.tcptype() == TCPTYPE_ACTIVE_STR || |
| 128 | (address.tcptype().empty() && address.address().port() == 0)) { |
| 129 | // It's active only candidate, we should not try to create connections |
| 130 | // for these candidates. |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | // We can't accept TCP connections incoming on other ports |
| 135 | if (origin == ORIGIN_OTHER_PORT) |
| 136 | return NULL; |
| 137 | |
| 138 | // Check if we are allowed to make outgoing TCP connections |
| 139 | if (incoming_only_ && (origin == ORIGIN_MESSAGE)) |
| 140 | return NULL; |
| 141 | |
| 142 | // We don't know how to act as an ssl server yet |
| 143 | if ((address.protocol() == SSLTCP_PROTOCOL_NAME) && |
| 144 | (origin == ORIGIN_THIS_PORT)) { |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | if (!IsCompatibleAddress(address.address())) { |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | TCPConnection* conn = NULL; |
| 153 | if (rtc::AsyncPacketSocket* socket = |
| 154 | GetIncoming(address.address(), true)) { |
| 155 | socket->SignalReadPacket.disconnect(this); |
| 156 | conn = new TCPConnection(this, address, socket); |
| 157 | } else { |
| 158 | conn = new TCPConnection(this, address); |
| 159 | } |
| 160 | AddConnection(conn); |
| 161 | return conn; |
| 162 | } |
| 163 | |
| 164 | void TCPPort::PrepareAddress() { |
| 165 | if (socket_) { |
| 166 | // If socket isn't bound yet the address will be added in |
| 167 | // OnAddressReady(). Socket may be in the CLOSED state if Listen() |
| 168 | // failed, we still want to add the socket address. |
| 169 | LOG(LS_VERBOSE) << "Preparing TCP address, current state: " |
| 170 | << socket_->GetState(); |
| 171 | if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND || |
| 172 | socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED) |
| 173 | AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(), |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame^] | 174 | rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", |
| 175 | TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | ICE_TYPE_PREFERENCE_HOST_TCP, 0, true); |
| 177 | } else { |
| 178 | LOG_J(LS_INFO, this) << "Not listening due to firewall restrictions."; |
| 179 | // Note: We still add the address, since otherwise the remote side won't |
| 180 | // recognize our incoming TCP connections. |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame^] | 181 | AddAddress(rtc::SocketAddress(ip(), 0), rtc::SocketAddress(ip(), 0), |
| 182 | rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", TCPTYPE_ACTIVE_STR, |
| 183 | LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, 0, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | int TCPPort::SendTo(const void* data, size_t size, |
| 188 | const rtc::SocketAddress& addr, |
| 189 | const rtc::PacketOptions& options, |
| 190 | bool payload) { |
| 191 | rtc::AsyncPacketSocket * socket = NULL; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 192 | TCPConnection* conn = static_cast<TCPConnection*>(GetConnection(addr)); |
| 193 | |
| 194 | // For Connection, this is the code path used by Ping() to establish |
| 195 | // WRITABLE. It has to send through the socket directly as TCPConnection::Send |
| 196 | // checks writability. |
| 197 | if (conn) { |
| 198 | if (!conn->connected()) { |
| 199 | conn->MaybeReconnect(); |
| 200 | return SOCKET_ERROR; |
| 201 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 202 | socket = conn->socket(); |
| 203 | } else { |
| 204 | socket = GetIncoming(addr); |
| 205 | } |
| 206 | if (!socket) { |
| 207 | LOG_J(LS_ERROR, this) << "Attempted to send to an unknown destination, " |
| 208 | << addr.ToSensitiveString(); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 209 | return SOCKET_ERROR; // TODO(tbd): Set error_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int sent = socket->Send(data, size, options); |
| 213 | if (sent < 0) { |
| 214 | error_ = socket->GetError(); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 215 | // Error from this code path for a Connection (instead of from a bare |
| 216 | // socket) will not trigger reconnecting. In theory, this shouldn't matter |
| 217 | // as OnClose should always be called and set connected to false. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 218 | LOG_J(LS_ERROR, this) << "TCP send of " << size |
| 219 | << " bytes failed with error " << error_; |
| 220 | } |
| 221 | return sent; |
| 222 | } |
| 223 | |
| 224 | int TCPPort::GetOption(rtc::Socket::Option opt, int* value) { |
| 225 | if (socket_) { |
| 226 | return socket_->GetOption(opt, value); |
| 227 | } else { |
| 228 | return SOCKET_ERROR; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int TCPPort::SetOption(rtc::Socket::Option opt, int value) { |
| 233 | if (socket_) { |
| 234 | return socket_->SetOption(opt, value); |
| 235 | } else { |
| 236 | return SOCKET_ERROR; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | int TCPPort::GetError() { |
| 241 | return error_; |
| 242 | } |
| 243 | |
| 244 | void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 245 | rtc::AsyncPacketSocket* new_socket) { |
| 246 | ASSERT(socket == socket_); |
| 247 | |
| 248 | Incoming incoming; |
| 249 | incoming.addr = new_socket->GetRemoteAddress(); |
| 250 | incoming.socket = new_socket; |
| 251 | incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); |
| 252 | incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); |
| 253 | |
| 254 | LOG_J(LS_VERBOSE, this) << "Accepted connection from " |
| 255 | << incoming.addr.ToSensitiveString(); |
| 256 | incoming_.push_back(incoming); |
| 257 | } |
| 258 | |
| 259 | rtc::AsyncPacketSocket* TCPPort::GetIncoming( |
| 260 | const rtc::SocketAddress& addr, bool remove) { |
| 261 | rtc::AsyncPacketSocket* socket = NULL; |
| 262 | for (std::list<Incoming>::iterator it = incoming_.begin(); |
| 263 | it != incoming_.end(); ++it) { |
| 264 | if (it->addr == addr) { |
| 265 | socket = it->socket; |
| 266 | if (remove) |
| 267 | incoming_.erase(it); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | return socket; |
| 272 | } |
| 273 | |
| 274 | void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 275 | const char* data, size_t size, |
| 276 | const rtc::SocketAddress& remote_addr, |
| 277 | const rtc::PacketTime& packet_time) { |
| 278 | Port::OnReadPacket(data, size, remote_addr, PROTO_TCP); |
| 279 | } |
| 280 | |
| 281 | void TCPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
| 282 | Port::OnReadyToSend(); |
| 283 | } |
| 284 | |
| 285 | void TCPPort::OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 286 | const rtc::SocketAddress& address) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame^] | 287 | AddAddress(address, address, rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", |
| 288 | TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST_TCP, |
| 289 | 0, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 292 | TCPConnection::TCPConnection(TCPPort* port, |
| 293 | const Candidate& candidate, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 294 | rtc::AsyncPacketSocket* socket) |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 295 | : Connection(port, 0, candidate), |
| 296 | socket_(socket), |
| 297 | error_(0), |
| 298 | outgoing_(socket == NULL), |
| 299 | connection_pending_(false), |
| 300 | pretending_to_be_writable_(false), |
| 301 | reconnection_timeout_(cricket::CONNECTION_WRITE_CONNECT_TIMEOUT) { |
| 302 | if (outgoing_) { |
| 303 | CreateOutgoingTcpSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 304 | } else { |
| 305 | // Incoming connections should match the network address. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 306 | LOG_J(LS_VERBOSE, this) |
| 307 | << "socket ipaddr: " << socket_->GetLocalAddress().ToString() |
| 308 | << ",port() ip:" << port->ip().ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 309 | ASSERT(socket_->GetLocalAddress().ipaddr() == port->ip()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 310 | ConnectSocketSignals(socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | TCPConnection::~TCPConnection() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int TCPConnection::Send(const void* data, size_t size, |
| 318 | const rtc::PacketOptions& options) { |
| 319 | if (!socket_) { |
| 320 | error_ = ENOTCONN; |
| 321 | return SOCKET_ERROR; |
| 322 | } |
| 323 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 324 | // Sending after OnClose on active side will trigger a reconnect for a |
| 325 | // outgoing connection. Note that the write state is still WRITABLE as we want |
| 326 | // to spend a few seconds attempting a reconnect before saying we're |
| 327 | // unwritable. |
| 328 | if (!connected()) { |
| 329 | MaybeReconnect(); |
| 330 | return SOCKET_ERROR; |
| 331 | } |
| 332 | |
| 333 | // Note that this is important to put this after the previous check to give |
| 334 | // the connection a chance to reconnect. |
| 335 | if (pretending_to_be_writable_ || write_state() != STATE_WRITABLE) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 336 | // TODO: Should STATE_WRITE_TIMEOUT return a non-blocking error? |
| 337 | error_ = EWOULDBLOCK; |
| 338 | return SOCKET_ERROR; |
| 339 | } |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 340 | sent_packets_total_++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | int sent = socket_->Send(data, size, options); |
| 342 | if (sent < 0) { |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 343 | sent_packets_discarded_++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 344 | error_ = socket_->GetError(); |
| 345 | } else { |
| 346 | send_rate_tracker_.Update(sent); |
| 347 | } |
| 348 | return sent; |
| 349 | } |
| 350 | |
| 351 | int TCPConnection::GetError() { |
| 352 | return error_; |
| 353 | } |
| 354 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 355 | void TCPConnection::OnConnectionRequestResponse(ConnectionRequest* req, |
| 356 | StunMessage* response) { |
| 357 | // Once we receive a binding response, we are really writable, and not just |
| 358 | // pretending to be writable. |
| 359 | pretending_to_be_writable_ = false; |
| 360 | Connection::OnConnectionRequestResponse(req, response); |
| 361 | ASSERT(write_state() == STATE_WRITABLE); |
| 362 | } |
| 363 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 364 | void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { |
| 365 | ASSERT(socket == socket_); |
| 366 | // Do not use this connection if the socket bound to a different address than |
| 367 | // the one we asked for. This is seen in Chrome, where TCP sockets cannot be |
| 368 | // given a binding address, and the platform is expected to pick the |
| 369 | // correct local address. |
henrike@webrtc.org | 43e033e | 2014-11-10 19:40:29 +0000 | [diff] [blame] | 370 | const rtc::IPAddress& socket_ip = socket->GetLocalAddress().ipaddr(); |
| 371 | if (socket_ip == port()->ip()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 372 | LOG_J(LS_VERBOSE, this) << "Connection established to " |
| 373 | << socket->GetRemoteAddress().ToSensitiveString(); |
| 374 | set_connected(true); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 375 | connection_pending_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 376 | } else { |
henrike@webrtc.org | 43e033e | 2014-11-10 19:40:29 +0000 | [diff] [blame] | 377 | LOG_J(LS_WARNING, this) << "Dropping connection as TCP socket bound to IP " |
| 378 | << socket_ip.ToSensitiveString() |
| 379 | << ", different from the local candidate IP " |
| 380 | << port()->ip().ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 381 | socket_->Close(); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) { |
| 386 | ASSERT(socket == socket_); |
henrike@webrtc.org | 43e033e | 2014-11-10 19:40:29 +0000 | [diff] [blame] | 387 | LOG_J(LS_INFO, this) << "Connection closed with error " << error; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 388 | |
| 389 | // Guard against the condition where IPC socket will call OnClose for every |
| 390 | // packet it can't send. |
| 391 | if (connected()) { |
| 392 | set_connected(false); |
| 393 | pretending_to_be_writable_ = true; |
| 394 | |
| 395 | // We don't attempt reconnect right here. This is to avoid a case where the |
| 396 | // shutdown is intentional and reconnect is not necessary. We only reconnect |
| 397 | // when the connection is used to Send() or Ping(). |
| 398 | port()->thread()->PostDelayed(reconnection_timeout(), this, |
| 399 | MSG_TCPCONNECTION_DELAYED_ONCLOSE); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void TCPConnection::OnMessage(rtc::Message* pmsg) { |
| 404 | switch (pmsg->message_id) { |
| 405 | case MSG_TCPCONNECTION_DELAYED_ONCLOSE: |
| 406 | // If this connection can't become connected and writable again in 5 |
| 407 | // seconds, it's time to tear this down. This is the case for the original |
| 408 | // TCP connection on passive side during a reconnect. |
| 409 | if (pretending_to_be_writable_) { |
| 410 | set_write_state(STATE_WRITE_TIMEOUT); |
| 411 | } |
| 412 | break; |
| 413 | default: |
| 414 | Connection::OnMessage(pmsg); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void TCPConnection::MaybeReconnect() { |
| 419 | // Only reconnect for an outgoing TCPConnection when OnClose was signaled and |
| 420 | // no outstanding reconnect is pending. |
| 421 | if (connected() || connection_pending_ || !outgoing_) { |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | LOG_J(LS_INFO, this) << "TCP Connection with remote is closed, " |
| 426 | << "trying to reconnect"; |
| 427 | |
| 428 | CreateOutgoingTcpSocket(); |
| 429 | error_ = EPIPE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void TCPConnection::OnReadPacket( |
| 433 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 434 | const rtc::SocketAddress& remote_addr, |
| 435 | const rtc::PacketTime& packet_time) { |
| 436 | ASSERT(socket == socket_); |
| 437 | Connection::OnReadPacket(data, size, packet_time); |
| 438 | } |
| 439 | |
| 440 | void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
| 441 | ASSERT(socket == socket_); |
| 442 | Connection::OnReadyToSend(); |
| 443 | } |
| 444 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 445 | void TCPConnection::CreateOutgoingTcpSocket() { |
| 446 | ASSERT(outgoing_); |
| 447 | // TODO(guoweis): Handle failures here (unlikely since TCP). |
| 448 | int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME) |
| 449 | ? rtc::PacketSocketFactory::OPT_SSLTCP |
| 450 | : 0; |
| 451 | socket_.reset(port()->socket_factory()->CreateClientTcpSocket( |
| 452 | rtc::SocketAddress(port()->ip(), 0), remote_candidate().address(), |
| 453 | port()->proxy(), port()->user_agent(), opts)); |
| 454 | if (socket_) { |
| 455 | LOG_J(LS_VERBOSE, this) |
| 456 | << "Connecting from " << socket_->GetLocalAddress().ToSensitiveString() |
| 457 | << " to " << remote_candidate().address().ToSensitiveString(); |
| 458 | set_connected(false); |
| 459 | connection_pending_ = true; |
| 460 | ConnectSocketSignals(socket_.get()); |
| 461 | } else { |
| 462 | LOG_J(LS_WARNING, this) << "Failed to create connection to " |
| 463 | << remote_candidate().address().ToSensitiveString(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) { |
| 468 | if (outgoing_) { |
| 469 | socket->SignalConnect.connect(this, &TCPConnection::OnConnect); |
| 470 | } |
| 471 | socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); |
| 472 | socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); |
| 473 | socket->SignalClose.connect(this, &TCPConnection::OnClose); |
| 474 | } |
| 475 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 476 | } // namespace cricket |