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

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: Explicitly check microseconds. 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
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 } // 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
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;
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);
122
123 // Only timeouts greater than zero and less than one second are allowed.
124 DCHECK_GT(timeout.InMicroseconds(), 0);
125 DCHECK_LT(timeout.InMicroseconds(),
126 base::TimeDelta::FromSeconds(1).InMicroseconds());
127
128 // Track the start time so we can reduce the timeout as data is read.
129 TimeTicks start_time = TimeTicks::Now();
130 const TimeTicks finish_time = start_time + timeout;
131
132 fd_set read_fds;
133 size_t bytes_read_total;
134 for (bytes_read_total = 0;
135 bytes_read_total < length && timeout.InMicroseconds() > 0;
136 timeout = finish_time - base::TimeTicks::Now()) {
137 FD_ZERO(&read_fds);
138 FD_SET(handle_, &read_fds);
139
140 // Wait for data to become available.
141 struct timeval timeout_struct = { 0, timeout.InMicroseconds() };
142 const int select_result =
143 select(handle_ + 1, &read_fds, NULL, NULL, &timeout_struct);
144 // Handle EINTR manually since we need to update the timeout value.
145 if (select_result == -1 && errno == EINTR)
146 continue;
147 if (select_result <= 0)
148 return bytes_read_total;
149
150 // select() only tells us that data is ready for reading, not how much. We
151 // must Peek() for the amount ready for reading to avoid blocking.
152 DCHECK(FD_ISSET(handle_, &read_fds));
153 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
154
155 // There may be zero bytes to read if the socket at the other end closed.
156 if (!bytes_to_read)
157 return bytes_read_total;
158
159 const size_t bytes_received =
160 Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
161 bytes_read_total += bytes_received;
162 if (bytes_received != bytes_to_read)
163 return bytes_read_total;
164 }
165
166 return bytes_read_total;
167 }
168
111 size_t SyncSocket::Peek() { 169 size_t SyncSocket::Peek() {
112 int number_chars; 170 DCHECK_NE(handle_, kInvalidHandle);
113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { 171 int number_chars = 0;
172 if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
114 // If there is an error in ioctl, signal that the channel would block. 173 // If there is an error in ioctl, signal that the channel would block.
115 return 0; 174 return 0;
116 } 175 }
117 return (size_t) number_chars; 176 DCHECK_GE(number_chars, 0);
177 return number_chars;
118 } 178 }
119 179
120 CancelableSyncSocket::CancelableSyncSocket() {} 180 CancelableSyncSocket::CancelableSyncSocket() {}
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) 181 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
122 : SyncSocket(handle) { 182 : SyncSocket(handle) {
123 } 183 }
124 184
125 bool CancelableSyncSocket::Shutdown() { 185 bool CancelableSyncSocket::Shutdown() {
186 DCHECK_NE(handle_, kInvalidHandle);
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; 187 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
127 } 188 }
128 189
129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { 190 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
191 DCHECK_GT(length, 0u);
192 DCHECK_LE(length, kMaxMessageLength);
193 DCHECK_NE(handle_, kInvalidHandle);
194
130 long flags = 0; 195 long flags = 0;
131 flags = fcntl(handle_, F_GETFL, NULL); 196 flags = fcntl(handle_, F_GETFL, NULL);
132 if (flags != -1 && (flags & O_NONBLOCK) == 0) { 197 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
133 // Set the socket to non-blocking mode for sending if its original mode 198 // Set the socket to non-blocking mode for sending if its original mode
134 // is blocking. 199 // is blocking.
135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); 200 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
136 } 201 }
137 202
138 size_t len = SyncSocket::Send(buffer, length); 203 size_t len = SyncSocket::Send(buffer, length);
139 204
140 if (flags != -1 && (flags & O_NONBLOCK) == 0) { 205 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
141 // Restore the original flags. 206 // Restore the original flags.
142 fcntl(handle_, F_SETFL, flags); 207 fcntl(handle_, F_SETFL, flags);
143 } 208 }
144 209
145 return len; 210 return len;
146 } 211 }
147 212
148 // static 213 // static
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 214 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
150 CancelableSyncSocket* socket_b) { 215 CancelableSyncSocket* socket_b) {
151 return SyncSocket::CreatePair(socket_a, socket_b); 216 return SyncSocket::CreatePair(socket_a, socket_b);
152 } 217 }
153 218
154 } // namespace base 219 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698