Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: base/sync_socket_posix.cc

Issue 23875019: Add SyncSocket::ReceiveWithTimeout() and SyncSocket unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Only AssertIOAllowed() on blocking Send(). Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/sync_socket_nacl.cc ('k') | base/sync_socket_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Writes |length| of |buffer| into |handle|. Returns the number of bytes
31 // written or zero on error. |length| must be greater than 0.
32 size_t SendHelper(SyncSocket::Handle handle,
33 const void* buffer,
34 size_t length) {
35 DCHECK_GT(length, 0u);
36 DCHECK_LE(length, kMaxMessageLength);
37 DCHECK_NE(handle, SyncSocket::kInvalidHandle);
38 const char* charbuffer = static_cast<const char*>(buffer);
39 const int len = file_util::WriteFileDescriptor(handle, charbuffer, length);
40 return len < 0 ? 0 : static_cast<size_t>(len);
41 }
42
30 } // namespace 43 } // namespace
31 44
32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; 45 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
33 46
34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} 47 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
35 48
36 SyncSocket::~SyncSocket() { 49 SyncSocket::~SyncSocket() {
37 Close(); 50 Close();
38 } 51 }
39 52
40 // static 53 // static
41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { 54 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
42 DCHECK(socket_a != socket_b); 55 DCHECK_NE(socket_a, socket_b);
43 DCHECK(socket_a->handle_ == kInvalidHandle); 56 DCHECK_EQ(socket_a->handle_, kInvalidHandle);
44 DCHECK(socket_b->handle_ == kInvalidHandle); 57 DCHECK_EQ(socket_b->handle_, kInvalidHandle);
45 58
46 #if defined(OS_MACOSX) 59 #if defined(OS_MACOSX)
47 int nosigpipe = 1; 60 int nosigpipe = 1;
48 #endif // defined(OS_MACOSX) 61 #endif // defined(OS_MACOSX)
49 62
50 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; 63 Handle handles[2] = { kInvalidHandle, kInvalidHandle };
51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) 64 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0)
52 goto cleanup; 65 goto cleanup;
53 66
54 #if defined(OS_MACOSX) 67 #if defined(OS_MACOSX)
(...skipping 20 matching lines...) Expand all
75 } 88 }
76 if (handles[1] != kInvalidHandle) { 89 if (handles[1] != kInvalidHandle) {
77 if (HANDLE_EINTR(close(handles[1])) < 0) 90 if (HANDLE_EINTR(close(handles[1])) < 0)
78 DPLOG(ERROR) << "close"; 91 DPLOG(ERROR) << "close";
79 } 92 }
80 93
81 return false; 94 return false;
82 } 95 }
83 96
84 bool SyncSocket::Close() { 97 bool SyncSocket::Close() {
85 if (handle_ == kInvalidHandle) { 98 if (handle_ == kInvalidHandle)
86 return false; 99 return true;
87 } 100 const int retval = HANDLE_EINTR(close(handle_));
88 int retval = HANDLE_EINTR(close(handle_)); 101 DPLOG_IF(ERROR, retval < 0) << "close";
89 if (retval < 0)
90 DPLOG(ERROR) << "close";
91 handle_ = kInvalidHandle; 102 handle_ = kInvalidHandle;
92 return (retval == 0); 103 return retval == 0;
93 } 104 }
94 105
95 size_t SyncSocket::Send(const void* buffer, size_t length) { 106 size_t SyncSocket::Send(const void* buffer, size_t length) {
96 DCHECK_LE(length, kMaxMessageLength); 107 ThreadRestrictions::AssertIOAllowed();
97 const char* charbuffer = static_cast<const char*>(buffer); 108 return SendHelper(handle_, buffer, length);
98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
99
100 return (len == -1) ? 0 : static_cast<size_t>(len);
101 } 109 }
102 110
103 size_t SyncSocket::Receive(void* buffer, size_t length) { 111 size_t SyncSocket::Receive(void* buffer, size_t length) {
112 ThreadRestrictions::AssertIOAllowed();
113 DCHECK_GT(length, 0u);
104 DCHECK_LE(length, kMaxMessageLength); 114 DCHECK_LE(length, kMaxMessageLength);
115 DCHECK_NE(handle_, kInvalidHandle);
105 char* charbuffer = static_cast<char*>(buffer); 116 char* charbuffer = static_cast<char*>(buffer);
106 if (file_util::ReadFromFD(handle_, charbuffer, length)) 117 if (file_util::ReadFromFD(handle_, charbuffer, length))
107 return length; 118 return length;
108 return 0; 119 return 0;
109 } 120 }
110 121
122 size_t SyncSocket::ReceiveWithTimeout(void* buffer,
123 size_t length,
124 TimeDelta timeout) {
125 ThreadRestrictions::AssertIOAllowed();
126 DCHECK_GT(length, 0u);
127 DCHECK_LE(length, kMaxMessageLength);
128 DCHECK_NE(handle_, kInvalidHandle);
129 DCHECK_LT(handle_, FD_SETSIZE);
130
131 // Only timeouts greater than zero and less than one second are allowed.
132 DCHECK_GT(timeout.InMicroseconds(), 0);
133 DCHECK_LT(timeout.InMicroseconds(),
134 base::TimeDelta::FromSeconds(1).InMicroseconds());
135
136 // Track the start time so we can reduce the timeout as data is read.
137 TimeTicks start_time = TimeTicks::Now();
138 const TimeTicks finish_time = start_time + timeout;
139
140 fd_set read_fds;
141 size_t bytes_read_total;
142 for (bytes_read_total = 0;
143 bytes_read_total < length && timeout.InMicroseconds() > 0;
jar (doing other things) 2013/10/18 22:02:52 nit: I don't know if this is time sensitive code (
DaleCurtis 2013/10/18 22:10:50 I don't see where it maps to a divide? https://cod
jar (doing other things) 2013/10/18 22:29:57 <DOH!> Nevermind. I'm so used to looking at ToMi
144 timeout = finish_time - base::TimeTicks::Now()) {
145 FD_ZERO(&read_fds);
146 FD_SET(handle_, &read_fds);
147
148 // Wait for data to become available.
149 struct timeval timeout_struct =
150 { 0, static_cast<suseconds_t>(timeout.InMicroseconds()) };
151 const int select_result =
152 select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct);
153 // Handle EINTR manually since we need to update the timeout value.
154 if (select_result == -1 && errno == EINTR)
155 continue;
156 if (select_result <= 0)
157 return bytes_read_total;
158
159 // select() only tells us that data is ready for reading, not how much. We
160 // must Peek() for the amount ready for reading to avoid blocking.
161 DCHECK(FD_ISSET(handle_, &read_fds));
162 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
163
164 // There may be zero bytes to read if the socket at the other end closed.
165 if (!bytes_to_read)
166 return bytes_read_total;
167
168 const size_t bytes_received =
169 Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
170 bytes_read_total += bytes_received;
171 if (bytes_received != bytes_to_read)
172 return bytes_read_total;
173 }
174
175 return bytes_read_total;
176 }
177
111 size_t SyncSocket::Peek() { 178 size_t SyncSocket::Peek() {
112 int number_chars; 179 DCHECK_NE(handle_, kInvalidHandle);
113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { 180 int number_chars = 0;
181 if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
114 // If there is an error in ioctl, signal that the channel would block. 182 // If there is an error in ioctl, signal that the channel would block.
115 return 0; 183 return 0;
116 } 184 }
117 return (size_t) number_chars; 185 DCHECK_GE(number_chars, 0);
186 return number_chars;
118 } 187 }
119 188
120 CancelableSyncSocket::CancelableSyncSocket() {} 189 CancelableSyncSocket::CancelableSyncSocket() {}
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) 190 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
122 : SyncSocket(handle) { 191 : SyncSocket(handle) {
123 } 192 }
124 193
125 bool CancelableSyncSocket::Shutdown() { 194 bool CancelableSyncSocket::Shutdown() {
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; 195 DCHECK_NE(handle_, kInvalidHandle);
196 return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0;
127 } 197 }
128 198
129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { 199 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
130 long flags = 0; 200 DCHECK_GT(length, 0u);
131 flags = fcntl(handle_, F_GETFL, NULL); 201 DCHECK_LE(length, kMaxMessageLength);
202 DCHECK_NE(handle_, kInvalidHandle);
203
204 const long flags = fcntl(handle_, F_GETFL, NULL);
132 if (flags != -1 && (flags & O_NONBLOCK) == 0) { 205 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
133 // Set the socket to non-blocking mode for sending if its original mode 206 // Set the socket to non-blocking mode for sending if its original mode
134 // is blocking. 207 // is blocking.
135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); 208 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
136 } 209 }
137 210
138 size_t len = SyncSocket::Send(buffer, length); 211 const size_t len = SendHelper(handle_, buffer, length);
139 212
140 if (flags != -1 && (flags & O_NONBLOCK) == 0) { 213 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
141 // Restore the original flags. 214 // Restore the original flags.
142 fcntl(handle_, F_SETFL, flags); 215 fcntl(handle_, F_SETFL, flags);
143 } 216 }
144 217
145 return len; 218 return len;
146 } 219 }
147 220
148 // static 221 // static
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 222 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
150 CancelableSyncSocket* socket_b) { 223 CancelableSyncSocket* socket_b) {
151 return SyncSocket::CreatePair(socket_a, socket_b); 224 return SyncSocket::CreatePair(socket_a, socket_b);
152 } 225 }
153 226
154 } // namespace base 227 } // namespace base
OLDNEW
« no previous file with comments | « base/sync_socket_nacl.cc ('k') | base/sync_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698