Chromium Code Reviews| 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 <fcntl.h> |
| 9 #include <limits.h> | 9 #include <limits.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 DCHECK_NE(socket_a, socket_b); | 55 DCHECK_NE(socket_a, socket_b); |
| 56 DCHECK_EQ(socket_a->handle_, kInvalidHandle); | 56 DCHECK_EQ(socket_a->handle_, kInvalidHandle); |
| 57 DCHECK_EQ(socket_b->handle_, kInvalidHandle); | 57 DCHECK_EQ(socket_b->handle_, kInvalidHandle); |
| 58 | 58 |
| 59 #if defined(OS_MACOSX) | 59 #if defined(OS_MACOSX) |
| 60 int nosigpipe = 1; | 60 int nosigpipe = 1; |
| 61 #endif // defined(OS_MACOSX) | 61 #endif // defined(OS_MACOSX) |
| 62 | 62 |
| 63 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; | 63 Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 64 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) | 64 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) |
| 65 goto cleanup; | 65 goto cleanup; |
|
jar (doing other things)
2013/11/01 00:00:13
nit: If goto's bug you as much as they bother me..
DaleCurtis
2013/11/01 00:15:59
Better: moved into anonymous function.
jar (doing other things)
2013/11/01 00:22:28
Perfect ;-)
| |
| 66 | 66 |
| 67 #if defined(OS_MACOSX) | 67 #if defined(OS_MACOSX) |
| 68 // On OSX an attempt to read or write to a closed socket may generate a | 68 // On OSX an attempt to read or write to a closed socket may generate a |
| 69 // SIGPIPE rather than returning -1. setsockopt will shut this off. | 69 // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 70 if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, | 70 if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, |
| 71 &nosigpipe, sizeof nosigpipe) || | 71 &nosigpipe, sizeof nosigpipe) || |
| 72 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, | 72 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, |
| 73 &nosigpipe, sizeof nosigpipe)) { | 73 &nosigpipe, sizeof nosigpipe)) { |
| 74 goto cleanup; | 74 goto cleanup; |
| 75 } | 75 } |
| 76 #endif | 76 #endif |
| 77 | 77 |
| 78 // Copy the handles out for successful return. | 78 // Copy the handles out for successful return. |
| 79 socket_a->handle_ = handles[0]; | 79 socket_a->handle_ = handles[0]; |
| 80 socket_b->handle_ = handles[1]; | 80 socket_b->handle_ = handles[1]; |
|
jar (doing other things)
2013/11/01 00:00:13
Wouldn't it be more interesting to test where the
DaleCurtis
2013/11/01 00:15:59
I considered this, but the handle only needs to be
| |
| 81 | 81 |
| 82 return true; | 82 return true; |
| 83 | 83 |
| 84 cleanup: | 84 cleanup: |
| 85 if (handles[0] != kInvalidHandle) { | 85 if (handles[0] != kInvalidHandle) { |
| 86 if (HANDLE_EINTR(close(handles[0])) < 0) | 86 if (HANDLE_EINTR(close(handles[0])) < 0) |
| 87 DPLOG(ERROR) << "close"; | 87 DPLOG(ERROR) << "close"; |
| 88 } | 88 } |
| 89 if (handles[1] != kInvalidHandle) { | 89 if (handles[1] != kInvalidHandle) { |
| 90 if (HANDLE_EINTR(close(handles[1])) < 0) | 90 if (HANDLE_EINTR(close(handles[1])) < 0) |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 119 return 0; | 119 return 0; |
| 120 } | 120 } |
| 121 | 121 |
| 122 size_t SyncSocket::ReceiveWithTimeout(void* buffer, | 122 size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 123 size_t length, | 123 size_t length, |
| 124 TimeDelta timeout) { | 124 TimeDelta timeout) { |
| 125 ThreadRestrictions::AssertIOAllowed(); | 125 ThreadRestrictions::AssertIOAllowed(); |
| 126 DCHECK_GT(length, 0u); | 126 DCHECK_GT(length, 0u); |
| 127 DCHECK_LE(length, kMaxMessageLength); | 127 DCHECK_LE(length, kMaxMessageLength); |
| 128 DCHECK_NE(handle_, kInvalidHandle); | 128 DCHECK_NE(handle_, kInvalidHandle); |
| 129 DCHECK_LT(handle_, FD_SETSIZE); | 129 CHECK_LT(handle_, FD_SETSIZE); |
| 130 | 130 |
| 131 // Only timeouts greater than zero and less than one second are allowed. | 131 // Only timeouts greater than zero and less than one second are allowed. |
| 132 DCHECK_GT(timeout.InMicroseconds(), 0); | 132 DCHECK_GT(timeout.InMicroseconds(), 0); |
| 133 DCHECK_LT(timeout.InMicroseconds(), | 133 DCHECK_LT(timeout.InMicroseconds(), |
| 134 base::TimeDelta::FromSeconds(1).InMicroseconds()); | 134 base::TimeDelta::FromSeconds(1).InMicroseconds()); |
| 135 | 135 |
| 136 // Track the start time so we can reduce the timeout as data is read. | 136 // Track the start time so we can reduce the timeout as data is read. |
| 137 TimeTicks start_time = TimeTicks::Now(); | 137 TimeTicks start_time = TimeTicks::Now(); |
| 138 const TimeTicks finish_time = start_time + timeout; | 138 const TimeTicks finish_time = start_time + timeout; |
| 139 | 139 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 return len; | 218 return len; |
| 219 } | 219 } |
| 220 | 220 |
| 221 // static | 221 // static |
| 222 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 222 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 223 CancelableSyncSocket* socket_b) { | 223 CancelableSyncSocket* socket_b) { |
| 224 return SyncSocket::CreatePair(socket_a, socket_b); | 224 return SyncSocket::CreatePair(socket_a, socket_b); |
| 225 } | 225 } |
| 226 | 226 |
| 227 } // namespace base | 227 } // namespace base |
| OLD | NEW |