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 <fcntl.h> | |
8 #include <limits.h> | 9 #include <limits.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 #include "base/threading/thread_restrictions.h" |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 | 24 |
25 namespace { | 25 namespace { |
26 // To avoid users sending negative message lengths to Send/Receive | 26 // 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. | 27 // 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); | 28 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; | 32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
33 | 33 |
34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} | 34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
35 | 35 |
36 SyncSocket::~SyncSocket() { | 36 SyncSocket::~SyncSocket() { |
37 Close(); | 37 Close(); |
38 } | 38 } |
39 | 39 |
40 // static | 40 // static |
41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { | 41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
42 DCHECK(socket_a != socket_b); | 42 DCHECK_NE(socket_a, socket_b); |
43 DCHECK(socket_a->handle_ == kInvalidHandle); | 43 DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
44 DCHECK(socket_b->handle_ == kInvalidHandle); | 44 DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
45 | 45 |
46 #if defined(OS_MACOSX) | 46 #if defined(OS_MACOSX) |
47 int nosigpipe = 1; | 47 int nosigpipe = 1; |
48 #endif // defined(OS_MACOSX) | 48 #endif // defined(OS_MACOSX) |
49 | 49 |
50 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; | 50 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) | 51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) |
52 goto cleanup; | 52 goto cleanup; |
53 | 53 |
54 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
(...skipping 20 matching lines...) Expand all Loading... | |
75 } | 75 } |
76 if (handles[1] != kInvalidHandle) { | 76 if (handles[1] != kInvalidHandle) { |
77 if (HANDLE_EINTR(close(handles[1])) < 0) | 77 if (HANDLE_EINTR(close(handles[1])) < 0) |
78 DPLOG(ERROR) << "close"; | 78 DPLOG(ERROR) << "close"; |
79 } | 79 } |
80 | 80 |
81 return false; | 81 return false; |
82 } | 82 } |
83 | 83 |
84 bool SyncSocket::Close() { | 84 bool SyncSocket::Close() { |
85 if (handle_ == kInvalidHandle) { | 85 if (handle_ == kInvalidHandle) |
86 return false; | 86 return true; |
jar (doing other things)
2013/10/10 02:01:28
Why has this changed to false?
DaleCurtis
2013/10/10 17:37:34
CancelableSyncSocket::Shutdown() will result in th
| |
87 } | 87 const int retval = HANDLE_EINTR(close(handle_)); |
88 int retval = HANDLE_EINTR(close(handle_)); | 88 DPLOG_IF(ERROR, retval < 0) << "close"; |
89 if (retval < 0) | |
90 DPLOG(ERROR) << "close"; | |
91 handle_ = kInvalidHandle; | 89 handle_ = kInvalidHandle; |
92 return (retval == 0); | 90 return retval == 0; |
93 } | 91 } |
94 | 92 |
95 size_t SyncSocket::Send(const void* buffer, size_t length) { | 93 size_t SyncSocket::Send(const void* buffer, size_t length) { |
94 ThreadRestrictions::AssertIOAllowed(); | |
95 DCHECK_GT(length, 0u); | |
96 DCHECK_LE(length, kMaxMessageLength); | 96 DCHECK_LE(length, kMaxMessageLength); |
97 DCHECK_NE(handle_, kInvalidHandle); | |
97 const char* charbuffer = static_cast<const char*>(buffer); | 98 const char* charbuffer = static_cast<const char*>(buffer); |
98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); | 99 const int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
99 | 100 return len < 0 ? 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 ThreadRestrictions::AssertIOAllowed(); | |
105 DCHECK_GT(length, 0u); | |
104 DCHECK_LE(length, kMaxMessageLength); | 106 DCHECK_LE(length, kMaxMessageLength); |
107 DCHECK_NE(handle_, kInvalidHandle); | |
105 char* charbuffer = static_cast<char*>(buffer); | 108 char* charbuffer = static_cast<char*>(buffer); |
106 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 109 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
107 return length; | 110 return length; |
108 return 0; | 111 return 0; |
109 } | 112 } |
110 | 113 |
114 size_t SyncSocket::ReceiveWithTimeout(void* buffer, | |
115 size_t length, | |
116 TimeDelta timeout) { | |
117 ThreadRestrictions::AssertIOAllowed(); | |
118 DCHECK_GT(length, 0u); | |
119 DCHECK_LE(length, kMaxMessageLength); | |
120 DCHECK_NE(handle_, kInvalidHandle); | |
121 DCHECK_LT(handle_, FD_SETSIZE); | |
DaleCurtis
2013/10/11 22:48:24
I made this a DCHECK(), but I'm wondering if it sh
| |
122 | |
123 // Only timeouts greater than zero and less than one second are allowed. | |
124 DCHECK_GT(timeout.InMicroseconds(), 0); | |
125 DCHECK(timeout.InMicroseconds() < Time::kMicrosecondsPerSecond); | |
jar (doing other things)
2013/10/10 02:01:28
nit: Much easier to read is something like:
timeo
DaleCurtis
2013/10/11 22:48:24
I like the second one, but it requires adding an o
| |
126 | |
127 // Track the start time so we can reduce the timeout as data is read. | |
128 TimeTicks start_time = TimeTicks::Now(); | |
129 | |
130 fd_set read_fds; | |
131 size_t bytes_read_total = 0; | |
132 char* charbuffer = static_cast<char*>(buffer); | |
133 do { | |
134 FD_ZERO(&read_fds); | |
135 FD_SET(handle_, &read_fds); | |
136 | |
137 // Wait for data to become available. | |
138 struct timeval timeout_struct = { 0, timeout.InMicroseconds() }; | |
139 const int select_result = | |
140 select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct); | |
141 // Handle EINTR manually since we need to update the timeout value. | |
142 if (select_result == -1 && errno == EINTR) | |
143 continue; | |
144 if (select_result <= 0) | |
145 return bytes_read_total; | |
146 | |
147 // select() only tells us that data is ready for reading, not how much. We | |
148 // must Peek() for the amount ready for reading to avoid blocking. | |
149 DCHECK(FD_ISSET(handle_, &read_fds)); | |
150 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total); | |
151 | |
152 // There may be zero bytes to read if the socket at the other end closed. | |
153 if (!bytes_to_read) | |
154 return bytes_read_total; | |
155 | |
156 const size_t bytes_received = | |
157 Receive(charbuffer + bytes_read_total, bytes_to_read); | |
DaleCurtis
2013/10/11 22:48:24
I just realized that Receive might hit and handle
| |
158 bytes_read_total += bytes_received; | |
159 if (bytes_received != bytes_to_read) | |
160 return bytes_read_total; | |
161 } while (bytes_read_total < length && | |
162 (timeout -= (TimeTicks::Now() - start_time)) > TimeDelta()); | |
jar (doing other things)
2013/10/10 02:01:28
This looks wrong.
It appears that you're decrem
DaleCurtis
2013/10/10 17:37:34
Can you elaborate? We need to update the timeout
jar (doing other things)
2013/10/11 02:36:12
The following is an explanation of how your code a
DaleCurtis
2013/10/11 22:48:24
Ack, I shouldn't have missed that. :( Thanks for y
| |
163 return bytes_read_total; | |
164 } | |
165 | |
111 size_t SyncSocket::Peek() { | 166 size_t SyncSocket::Peek() { |
112 int number_chars; | 167 DCHECK_NE(handle_, kInvalidHandle); |
113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 168 int number_chars = 0; |
169 if (ioctl(handle_, FIONREAD, &number_chars) == -1) { | |
114 // If there is an error in ioctl, signal that the channel would block. | 170 // If there is an error in ioctl, signal that the channel would block. |
115 return 0; | 171 return 0; |
116 } | 172 } |
117 return (size_t) number_chars; | 173 DCHECK_GE(number_chars, 0); |
174 return number_chars; | |
118 } | 175 } |
119 | 176 |
120 CancelableSyncSocket::CancelableSyncSocket() {} | 177 CancelableSyncSocket::CancelableSyncSocket() {} |
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) | 178 CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
122 : SyncSocket(handle) { | 179 : SyncSocket(handle) { |
123 } | 180 } |
124 | 181 |
125 bool CancelableSyncSocket::Shutdown() { | 182 bool CancelableSyncSocket::Shutdown() { |
183 DCHECK_NE(handle_, kInvalidHandle); | |
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; | 184 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
127 } | 185 } |
128 | 186 |
129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | 187 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
188 DCHECK_GT(length, 0u); | |
189 DCHECK_LE(length, kMaxMessageLength); | |
190 DCHECK_NE(handle_, kInvalidHandle); | |
191 | |
130 long flags = 0; | 192 long flags = 0; |
131 flags = fcntl(handle_, F_GETFL, NULL); | 193 flags = fcntl(handle_, F_GETFL, NULL); |
132 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | 194 if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
133 // Set the socket to non-blocking mode for sending if its original mode | 195 // Set the socket to non-blocking mode for sending if its original mode |
134 // is blocking. | 196 // is blocking. |
135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); | 197 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); |
136 } | 198 } |
137 | 199 |
138 size_t len = SyncSocket::Send(buffer, length); | 200 size_t len = SyncSocket::Send(buffer, length); |
139 | 201 |
140 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | 202 if (flags != -1 && (flags & O_NONBLOCK) == 0) { |
141 // Restore the original flags. | 203 // Restore the original flags. |
142 fcntl(handle_, F_SETFL, flags); | 204 fcntl(handle_, F_SETFL, flags); |
143 } | 205 } |
144 | 206 |
145 return len; | 207 return len; |
146 } | 208 } |
147 | 209 |
148 // static | 210 // static |
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 211 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
150 CancelableSyncSocket* socket_b) { | 212 CancelableSyncSocket* socket_b) { |
151 return SyncSocket::CreatePair(socket_a, socket_b); | 213 return SyncSocket::CreatePair(socket_a, socket_b); |
152 } | 214 } |
153 | 215 |
154 } // namespace base | 216 } // namespace base |
OLD | NEW |