Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 } | 101 } |
| 102 | 102 |
| 103 size_t SyncSocket::Receive(void* buffer, size_t length) { | 103 size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 104 DCHECK_LE(length, kMaxMessageLength); | 104 DCHECK_LE(length, kMaxMessageLength); |
| 105 char* charbuffer = static_cast<char*>(buffer); | 105 char* charbuffer = static_cast<char*>(buffer); |
| 106 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 106 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
| 107 return length; | 107 return length; |
| 108 return 0; | 108 return 0; |
| 109 } | 109 } |
| 110 | 110 |
| 111 size_t SyncSocket::ReceiveWithTimeout(void* buffer, | |
| 112 size_t length, | |
| 113 TimeDelta timeout) { | |
| 114 fd_set rfds; | |
| 115 FD_ZERO(&rfds); | |
| 116 FD_SET(handle_, &rfds); | |
| 117 | |
| 118 DCHECK(timeout.InMicroseconds() < Time::kMicrosecondsPerSecond); | |
|
awong
2013/09/17 18:54:12
nit: DCHECK_LT?
DaleCurtis
2013/09/17 19:00:25
Doesn't work for some reason, ends up with a link
awong
2013/09/17 19:57:11
Oh right. It's cause there isn't a definition for
| |
| 119 struct timeval timeout_struct = { 0, timeout.InMicroseconds() }; | |
| 120 | |
| 121 int select_result = HANDLE_EINTR( | |
| 122 select(handle_ + 1, &rfds, NULL, NULL, &timeout_struct)); | |
| 123 | |
| 124 if (select_result > 0) { | |
| 125 DCHECK(FD_ISSET(handle_, &rfds)); | |
| 126 DCHECK_GE(Peek(), length); | |
|
awong
2013/09/17 18:54:12
Why do we think Peek() can't be less than length?
DaleCurtis
2013/09/17 19:00:25
Correct, I'll remove the condition. For its inten
awong
2013/09/17 19:57:11
Do you want to read the amount from Peek() or the
DaleCurtis
2013/09/17 20:13:44
To be perfect this should be a while loop which ru
| |
| 127 return Receive(buffer, length); | |
| 128 } | |
| 129 | |
| 130 return 0; | |
| 131 } | |
| 132 | |
| 111 size_t SyncSocket::Peek() { | 133 size_t SyncSocket::Peek() { |
| 112 int number_chars; | 134 int number_chars; |
| 113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 135 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { |
| 114 // If there is an error in ioctl, signal that the channel would block. | 136 // If there is an error in ioctl, signal that the channel would block. |
| 115 return 0; | 137 return 0; |
| 116 } | 138 } |
| 117 return (size_t) number_chars; | 139 return (size_t) number_chars; |
| 118 } | 140 } |
| 119 | 141 |
| 120 CancelableSyncSocket::CancelableSyncSocket() {} | 142 CancelableSyncSocket::CancelableSyncSocket() {} |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 145 return len; | 167 return len; |
| 146 } | 168 } |
| 147 | 169 |
| 148 // static | 170 // static |
| 149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 171 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 150 CancelableSyncSocket* socket_b) { | 172 CancelableSyncSocket* socket_b) { |
| 151 return SyncSocket::CreatePair(socket_a, socket_b); | 173 return SyncSocket::CreatePair(socket_a, socket_b); |
| 152 } | 174 } |
| 153 | 175 |
| 154 } // namespace base | 176 } // namespace base |
| OLD | NEW |