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::Receive(void* buffer, size_t length, | |
112 base::TimeDelta timeout) { | |
113 // TODO(dalecurtis): Build fd_set during construction? | |
tommi (sloooow) - chröme
2013/08/20 10:55:57
would this make a measurable difference?
DaleCurtis
2013/09/11 01:16:03
Probably not. Comment removed.
| |
114 fd_set rfds; | |
115 FD_ZERO(&rfds); | |
116 FD_SET(handle_, &rfds); | |
117 | |
118 // TODO(dalecurtis): Fill out seconds. Can't use Time::ToTimeVal since it | |
119 // expects time relative to 1970. | |
120 struct timeval timeout_struct = { 0, timeout.InMicroseconds() }; | |
miu
2013/08/17 02:39:13
Since you're not populating the "seconds" part, co
DaleCurtis
2013/09/11 01:16:03
Done.
| |
121 int select_result = HANDLE_EINTR( | |
122 select(handle_ + 1, &rfds, NULL, NULL, &timeout_struct)); | |
123 if (select_result <= 0) | |
124 return 0; | |
125 | |
126 DCHECK_LE(length, kMaxMessageLength); | |
127 char* charbuffer = static_cast<char*>(buffer); | |
128 if (file_util::ReadFromFD(handle_, charbuffer, length)) | |
tommi (sloooow) - chröme
2013/08/20 10:55:57
don't you first have to check if handle_ is in rfd
DaleCurtis
2013/08/20 18:27:51
Not if we only have one descriptor since select()
tommi (sloooow) - chröme
2013/08/21 12:33:26
sgtm
| |
129 return length; | |
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 |