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> |
11 #include <sys/ioctl.h> | 11 #include <sys/ioctl.h> |
12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 | 14 |
15 #if defined(OS_SOLARIS) | 15 #if defined(OS_SOLARIS) |
16 #include <sys/filio.h> | 16 #include <sys/filio.h> |
17 #endif | 17 #endif |
18 | 18 |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 | 21 |
22 | |
23 namespace base { | 22 namespace base { |
24 | 23 |
25 namespace { | 24 namespace { |
26 // To avoid users sending negative message lengths to Send/Receive | 25 // To avoid users sending negative message lengths to Send/Receive |
27 // we clamp message lengths, which are size_t, to no more than INT_MAX. | 26 // we clamp message lengths, which are size_t, to no more than INT_MAX. |
28 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); | 27 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
29 | 28 |
30 } // namespace | 29 } // namespace |
31 | 30 |
32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; | 31 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 return false; | 85 return false; |
87 } | 86 } |
88 int retval = HANDLE_EINTR(close(handle_)); | 87 int retval = HANDLE_EINTR(close(handle_)); |
89 if (retval < 0) | 88 if (retval < 0) |
90 DPLOG(ERROR) << "close"; | 89 DPLOG(ERROR) << "close"; |
91 handle_ = kInvalidHandle; | 90 handle_ = kInvalidHandle; |
92 return (retval == 0); | 91 return (retval == 0); |
93 } | 92 } |
94 | 93 |
95 size_t SyncSocket::Send(const void* buffer, size_t length) { | 94 size_t SyncSocket::Send(const void* buffer, size_t length) { |
95 DCHECK_GT(length, 0u); | |
96 DCHECK_LE(length, kMaxMessageLength); | 96 DCHECK_LE(length, kMaxMessageLength); |
97 const char* charbuffer = static_cast<const char*>(buffer); | 97 const char* charbuffer = static_cast<const char*>(buffer); |
98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); | 98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
99 | 99 |
100 return (len == -1) ? 0 : static_cast<size_t>(len); | 100 return (len == -1) ? 0 : static_cast<size_t>(len); |
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_GT(length, 0u); | |
104 DCHECK_LE(length, kMaxMessageLength); | 105 DCHECK_LE(length, kMaxMessageLength); |
105 char* charbuffer = static_cast<char*>(buffer); | 106 char* charbuffer = static_cast<char*>(buffer); |
106 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 107 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
107 return length; | 108 return length; |
108 return 0; | 109 return 0; |
109 } | 110 } |
110 | 111 |
112 size_t SyncSocket::ReceiveWithTimeout(void* buffer, | |
113 size_t length, | |
114 TimeDelta timeout) { | |
willchan no longer on Chromium
2013/10/04 21:01:35
Please use ThreadRestrictions::AssertIOAllowed();
DaleCurtis
2013/10/04 21:08:39
I'll check if that can be done.
DaleCurtis
2013/10/04 23:11:32
Done.
| |
115 DCHECK_GT(length, 0u); | |
116 DCHECK_LE(length, kMaxMessageLength); | |
117 | |
118 // Only timeouts greater than zero and less than one second are allowed. | |
119 DCHECK_GT(timeout.InMicroseconds(), 0); | |
120 DCHECK(timeout.InMicroseconds() < Time::kMicrosecondsPerSecond); | |
121 | |
122 // Track the start time so we can reduce the timeout as data is read. | |
123 TimeTicks start_time = base::TimeTicks::Now(); | |
willchan no longer on Chromium
2013/10/04 21:01:35
drop the base::
DaleCurtis
2013/10/04 23:11:32
Done.
| |
124 | |
125 fd_set rfds; | |
willchan no longer on Chromium
2013/10/04 21:01:35
Please choose a more descriptive name.
DaleCurtis
2013/10/04 23:11:32
Done.
| |
126 size_t bytes_read_total = 0; | |
127 char* charbuffer = static_cast<char*>(buffer); | |
128 do { | |
129 FD_ZERO(&rfds); | |
130 FD_SET(handle_, &rfds); | |
Nico
2013/10/04 05:35:12
You need to check that handle_ is < FD_SETSIZE, el
DaleCurtis
2013/10/04 23:11:32
Done.
| |
131 | |
132 // Wait for data to become available. | |
133 struct timeval timeout_struct = { 0, timeout.InMicroseconds() }; | |
134 const int select_result = HANDLE_EINTR( | |
willchan no longer on Chromium
2013/10/04 21:01:35
I think there's a bug here, right? HANDLE_EINTR()
DaleCurtis
2013/10/04 21:08:39
select() will update the timeout struct on EINTR:
willchan no longer on Chromium
2013/10/09 20:06:52
How about the non-linux posix platforms? What happ
DaleCurtis
2013/10/09 21:55:40
Man page is sadly conflicting, saying both that it
| |
135 select(handle_ + 1, &rfds, NULL, NULL, &timeout_struct)); | |
136 if (select_result <= 0) | |
137 return bytes_read_total; | |
138 | |
139 // select() only tells us that data is ready for reading, not how much. We | |
140 // must Peek() for the amount ready for reading to avoid blocking. | |
willchan no longer on Chromium
2013/10/04 21:01:35
Why don't you just use read() instead and do the l
DaleCurtis
2013/10/04 21:08:39
Code reuse? What's the benefit of reimplementing t
willchan no longer on Chromium
2013/10/09 20:06:52
Sorry, I was hung up on disliking the API that alw
| |
141 DCHECK(FD_ISSET(handle_, &rfds)); | |
142 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); | |
143 const size_t bytes_received = | |
144 Receive(charbuffer + bytes_read_total, bytes_to_read); | |
145 bytes_read_total += bytes_received; | |
146 if (bytes_received != bytes_to_read) | |
147 return bytes_read_total; | |
148 } while (bytes_read_total < length && | |
149 (timeout -= (base::TimeTicks::Now() - start_time)) > | |
willchan no longer on Chromium
2013/10/04 21:01:35
drop the base::
DaleCurtis
2013/10/04 23:11:32
Done.
| |
150 base::TimeDelta()); | |
151 return bytes_read_total; | |
152 } | |
153 | |
111 size_t SyncSocket::Peek() { | 154 size_t SyncSocket::Peek() { |
112 int number_chars; | 155 int number_chars; |
113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 156 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { |
114 // If there is an error in ioctl, signal that the channel would block. | 157 // If there is an error in ioctl, signal that the channel would block. |
115 return 0; | 158 return 0; |
116 } | 159 } |
117 return (size_t) number_chars; | 160 DCHECK_GE(number_chars, 0); |
161 return number_chars; | |
118 } | 162 } |
119 | 163 |
120 CancelableSyncSocket::CancelableSyncSocket() {} | 164 CancelableSyncSocket::CancelableSyncSocket() {} |
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) | 165 CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
122 : SyncSocket(handle) { | 166 : SyncSocket(handle) { |
123 } | 167 } |
124 | 168 |
125 bool CancelableSyncSocket::Shutdown() { | 169 bool CancelableSyncSocket::Shutdown() { |
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; | 170 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
127 } | 171 } |
(...skipping 17 matching lines...) Expand all Loading... | |
145 return len; | 189 return len; |
146 } | 190 } |
147 | 191 |
148 // static | 192 // static |
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 193 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
150 CancelableSyncSocket* socket_b) { | 194 CancelableSyncSocket* socket_b) { |
151 return SyncSocket::CreatePair(socket_a, socket_b); | 195 return SyncSocket::CreatePair(socket_a, socket_b); |
152 } | 196 } |
153 | 197 |
154 } // namespace base | 198 } // namespace base |
OLD | NEW |