OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/tcp_client_socket_libevent.h" | 5 #include "net/socket/tcp_client_socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
11 #include <netinet/tcp.h> | 11 #include <netinet/tcp.h> |
12 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
13 #include <netinet/in.h> | 13 #include <netinet/in.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include "base/eintr_wrapper.h" | 16 #include "base/eintr_wrapper.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 19 #include "base/stats_counters.h" |
19 #include "base/string_util.h" | 20 #include "base/string_util.h" |
20 #include "net/base/address_list_net_log_param.h" | 21 #include "net/base/address_list_net_log_param.h" |
21 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
23 #include "net/base/net_log.h" | 24 #include "net/base/net_log.h" |
24 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
25 #if defined(USE_SYSTEM_LIBEVENT) | 26 #if defined(USE_SYSTEM_LIBEVENT) |
26 #include <event.h> | 27 #include <event.h> |
27 #else | 28 #else |
28 #include "third_party/libevent/event.h" | 29 #include "third_party/libevent/event.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 119 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); |
119 } | 120 } |
120 | 121 |
121 int TCPClientSocketLibevent::Connect(CompletionCallback* callback) { | 122 int TCPClientSocketLibevent::Connect(CompletionCallback* callback) { |
122 DCHECK(CalledOnValidThread()); | 123 DCHECK(CalledOnValidThread()); |
123 | 124 |
124 // If already connected, then just return OK. | 125 // If already connected, then just return OK. |
125 if (socket_ != kInvalidSocket) | 126 if (socket_ != kInvalidSocket) |
126 return OK; | 127 return OK; |
127 | 128 |
| 129 static StatsCounter connects("tcp.connect"); |
| 130 connects.Increment(); |
| 131 |
128 DCHECK(!waiting_connect()); | 132 DCHECK(!waiting_connect()); |
129 | 133 |
130 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, | 134 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, |
131 new AddressListNetLogParam(addresses_)); | 135 new AddressListNetLogParam(addresses_)); |
132 | 136 |
133 // We will try to connect to each address in addresses_. Start with the | 137 // We will try to connect to each address in addresses_. Start with the |
134 // first one in the list. | 138 // first one in the list. |
135 next_connect_state_ = CONNECT_STATE_CONNECT; | 139 next_connect_state_ = CONNECT_STATE_CONNECT; |
136 current_ai_ = addresses_.head(); | 140 current_ai_ = addresses_.head(); |
137 | 141 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 DCHECK(CalledOnValidThread()); | 306 DCHECK(CalledOnValidThread()); |
303 DCHECK_NE(kInvalidSocket, socket_); | 307 DCHECK_NE(kInvalidSocket, socket_); |
304 DCHECK(!waiting_connect()); | 308 DCHECK(!waiting_connect()); |
305 DCHECK(!read_callback_); | 309 DCHECK(!read_callback_); |
306 // Synchronous operation not supported | 310 // Synchronous operation not supported |
307 DCHECK(callback); | 311 DCHECK(callback); |
308 DCHECK_GT(buf_len, 0); | 312 DCHECK_GT(buf_len, 0); |
309 | 313 |
310 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 314 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
311 if (nread >= 0) { | 315 if (nread >= 0) { |
| 316 static StatsCounter read_bytes("tcp.read_bytes"); |
| 317 read_bytes.Add(nread); |
| 318 |
312 net_log_.AddEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, | 319 net_log_.AddEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, |
313 new NetLogIntegerParameter("num_bytes", nread)); | 320 new NetLogIntegerParameter("num_bytes", nread)); |
314 return nread; | 321 return nread; |
315 } | 322 } |
316 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 323 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
317 DLOG(INFO) << "read failed, errno " << errno; | 324 DLOG(INFO) << "read failed, errno " << errno; |
318 return MapPosixError(errno); | 325 return MapPosixError(errno); |
319 } | 326 } |
320 | 327 |
321 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 328 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
(...skipping 15 matching lines...) Expand all Loading... |
337 DCHECK(CalledOnValidThread()); | 344 DCHECK(CalledOnValidThread()); |
338 DCHECK_NE(kInvalidSocket, socket_); | 345 DCHECK_NE(kInvalidSocket, socket_); |
339 DCHECK(!waiting_connect()); | 346 DCHECK(!waiting_connect()); |
340 DCHECK(!write_callback_); | 347 DCHECK(!write_callback_); |
341 // Synchronous operation not supported | 348 // Synchronous operation not supported |
342 DCHECK(callback); | 349 DCHECK(callback); |
343 DCHECK_GT(buf_len, 0); | 350 DCHECK_GT(buf_len, 0); |
344 | 351 |
345 int nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len)); | 352 int nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len)); |
346 if (nwrite >= 0) { | 353 if (nwrite >= 0) { |
| 354 static StatsCounter write_bytes("tcp.write_bytes"); |
| 355 write_bytes.Add(nwrite); |
347 net_log_.AddEvent(NetLog::TYPE_SOCKET_BYTES_SENT, | 356 net_log_.AddEvent(NetLog::TYPE_SOCKET_BYTES_SENT, |
348 new NetLogIntegerParameter("num_bytes", nwrite)); | 357 new NetLogIntegerParameter("num_bytes", nwrite)); |
349 return nwrite; | 358 return nwrite; |
350 } | 359 } |
351 if (errno != EAGAIN && errno != EWOULDBLOCK) | 360 if (errno != EAGAIN && errno != EWOULDBLOCK) |
352 return MapPosixError(errno); | 361 return MapPosixError(errno); |
353 | 362 |
354 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 363 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
355 socket_, true, MessageLoopForIO::WATCH_WRITE, | 364 socket_, true, MessageLoopForIO::WATCH_WRITE, |
356 &write_socket_watcher_, &write_watcher_)) { | 365 &write_socket_watcher_, &write_watcher_)) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { | 509 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { |
501 DCHECK(CalledOnValidThread()); | 510 DCHECK(CalledOnValidThread()); |
502 DCHECK(address); | 511 DCHECK(address); |
503 if (!current_ai_) | 512 if (!current_ai_) |
504 return ERR_UNEXPECTED; | 513 return ERR_UNEXPECTED; |
505 address->Copy(current_ai_, false); | 514 address->Copy(current_ai_, false); |
506 return OK; | 515 return OK; |
507 } | 516 } |
508 | 517 |
509 } // namespace net | 518 } // namespace net |
OLD | NEW |