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> |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 DCHECK(!waiting_connect_); | 270 DCHECK(!waiting_connect_); |
271 DCHECK(!read_callback_); | 271 DCHECK(!read_callback_); |
272 // Synchronous operation not supported | 272 // Synchronous operation not supported |
273 DCHECK(callback); | 273 DCHECK(callback); |
274 DCHECK_GT(buf_len, 0); | 274 DCHECK_GT(buf_len, 0); |
275 | 275 |
276 TRACE_EVENT_BEGIN("socket.read", this, ""); | 276 TRACE_EVENT_BEGIN("socket.read", this, ""); |
277 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 277 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
278 if (nread >= 0) { | 278 if (nread >= 0) { |
279 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", nread)); | 279 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", nread)); |
280 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread); | 280 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, |
| 281 "num_bytes", nread); |
281 return nread; | 282 return nread; |
282 } | 283 } |
283 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 284 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
284 DLOG(INFO) << "read failed, errno " << errno; | 285 DLOG(INFO) << "read failed, errno " << errno; |
285 return MapPosixError(errno); | 286 return MapPosixError(errno); |
286 } | 287 } |
287 | 288 |
288 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 289 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
289 socket_, true, MessageLoopForIO::WATCH_READ, | 290 socket_, true, MessageLoopForIO::WATCH_READ, |
290 &read_socket_watcher_, &read_watcher_)) { | 291 &read_socket_watcher_, &read_watcher_)) { |
(...skipping 14 matching lines...) Expand all Loading... |
305 DCHECK(!waiting_connect_); | 306 DCHECK(!waiting_connect_); |
306 DCHECK(!write_callback_); | 307 DCHECK(!write_callback_); |
307 // Synchronous operation not supported | 308 // Synchronous operation not supported |
308 DCHECK(callback); | 309 DCHECK(callback); |
309 DCHECK_GT(buf_len, 0); | 310 DCHECK_GT(buf_len, 0); |
310 | 311 |
311 TRACE_EVENT_BEGIN("socket.write", this, ""); | 312 TRACE_EVENT_BEGIN("socket.write", this, ""); |
312 int nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len)); | 313 int nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len)); |
313 if (nwrite >= 0) { | 314 if (nwrite >= 0) { |
314 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", nwrite)); | 315 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", nwrite)); |
315 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, nwrite); | 316 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, |
| 317 "num_bytes", nwrite); |
316 return nwrite; | 318 return nwrite; |
317 } | 319 } |
318 if (errno != EAGAIN && errno != EWOULDBLOCK) | 320 if (errno != EAGAIN && errno != EWOULDBLOCK) |
319 return MapPosixError(errno); | 321 return MapPosixError(errno); |
320 | 322 |
321 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 323 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
322 socket_, true, MessageLoopForIO::WATCH_WRITE, | 324 socket_, true, MessageLoopForIO::WATCH_WRITE, |
323 &write_socket_watcher_, &write_watcher_)) { | 325 &write_socket_watcher_, &write_watcher_)) { |
324 DLOG(INFO) << "WatchFileDescriptor failed on write, errno " << errno; | 326 DLOG(INFO) << "WatchFileDescriptor failed on write, errno " << errno; |
325 return MapPosixError(errno); | 327 return MapPosixError(errno); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 void TCPClientSocketLibevent::DidCompleteRead() { | 427 void TCPClientSocketLibevent::DidCompleteRead() { |
426 int bytes_transferred; | 428 int bytes_transferred; |
427 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 429 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
428 read_buf_len_)); | 430 read_buf_len_)); |
429 | 431 |
430 int result; | 432 int result; |
431 if (bytes_transferred >= 0) { | 433 if (bytes_transferred >= 0) { |
432 TRACE_EVENT_END("socket.read", this, | 434 TRACE_EVENT_END("socket.read", this, |
433 StringPrintf("%d bytes", bytes_transferred)); | 435 StringPrintf("%d bytes", bytes_transferred)); |
434 result = bytes_transferred; | 436 result = bytes_transferred; |
435 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result); | 437 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, |
| 438 "num_bytes", result); |
436 } else { | 439 } else { |
437 result = MapPosixError(errno); | 440 result = MapPosixError(errno); |
438 } | 441 } |
439 | 442 |
440 if (result != ERR_IO_PENDING) { | 443 if (result != ERR_IO_PENDING) { |
441 read_buf_ = NULL; | 444 read_buf_ = NULL; |
442 read_buf_len_ = 0; | 445 read_buf_len_ = 0; |
443 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); | 446 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); |
444 DCHECK(ok); | 447 DCHECK(ok); |
445 DoReadCallback(result); | 448 DoReadCallback(result); |
446 } | 449 } |
447 } | 450 } |
448 | 451 |
449 void TCPClientSocketLibevent::DidCompleteWrite() { | 452 void TCPClientSocketLibevent::DidCompleteWrite() { |
450 int bytes_transferred; | 453 int bytes_transferred; |
451 bytes_transferred = HANDLE_EINTR(write(socket_, write_buf_->data(), | 454 bytes_transferred = HANDLE_EINTR(write(socket_, write_buf_->data(), |
452 write_buf_len_)); | 455 write_buf_len_)); |
453 | 456 |
454 int result; | 457 int result; |
455 if (bytes_transferred >= 0) { | 458 if (bytes_transferred >= 0) { |
456 result = bytes_transferred; | 459 result = bytes_transferred; |
457 TRACE_EVENT_END("socket.write", this, | 460 TRACE_EVENT_END("socket.write", this, |
458 StringPrintf("%d bytes", bytes_transferred)); | 461 StringPrintf("%d bytes", bytes_transferred)); |
459 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, result); | 462 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, |
| 463 "num_bytes", result); |
460 } else { | 464 } else { |
461 result = MapPosixError(errno); | 465 result = MapPosixError(errno); |
462 } | 466 } |
463 | 467 |
464 if (result != ERR_IO_PENDING) { | 468 if (result != ERR_IO_PENDING) { |
465 write_buf_ = NULL; | 469 write_buf_ = NULL; |
466 write_buf_len_ = 0; | 470 write_buf_len_ = 0; |
467 write_socket_watcher_.StopWatchingFileDescriptor(); | 471 write_socket_watcher_.StopWatchingFileDescriptor(); |
468 DoWriteCallback(result); | 472 DoWriteCallback(result); |
469 } | 473 } |
470 } | 474 } |
471 | 475 |
472 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { | 476 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { |
473 DCHECK(address); | 477 DCHECK(address); |
474 if (!current_ai_) | 478 if (!current_ai_) |
475 return ERR_UNEXPECTED; | 479 return ERR_UNEXPECTED; |
476 address->Copy(current_ai_, false); | 480 address->Copy(current_ai_, false); |
477 return OK; | 481 return OK; |
478 } | 482 } |
479 | 483 |
480 } // namespace net | 484 } // namespace net |
OLD | NEW |