| 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> | |
| 10 #include <stdio.h> | 9 #include <stdio.h> |
| 10 #include <sys/types.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> | |
| 14 | 13 |
| 15 #if defined(OS_SOLARIS) | 14 #if defined(OS_SOLARIS) |
| 16 #include <sys/filio.h> | 15 #include <sys/filio.h> |
| 17 #endif | 16 #endif |
| 18 | 17 |
| 19 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 20 #include "base/logging.h" | 19 #include "base/logging.h" |
| 21 | 20 |
| 22 | 21 |
| 23 namespace base { | 22 namespace base { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 if (retval < 0) | 88 if (retval < 0) |
| 90 DPLOG(ERROR) << "close"; | 89 DPLOG(ERROR) << "close"; |
| 91 handle_ = kInvalidHandle; | 90 handle_ = kInvalidHandle; |
| 92 return (retval == 0); | 91 return (retval == 0); |
| 93 } | 92 } |
| 94 | 93 |
| 95 size_t SyncSocket::Send(const void* buffer, size_t length) { | 94 size_t SyncSocket::Send(const void* buffer, size_t length) { |
| 96 DCHECK_LE(length, kMaxMessageLength); | 95 DCHECK_LE(length, kMaxMessageLength); |
| 97 const char* charbuffer = static_cast<const char*>(buffer); | 96 const char* charbuffer = static_cast<const char*>(buffer); |
| 98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); | 97 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
| 99 | 98 return static_cast<size_t>(len); |
| 100 return (len == -1) ? 0 : static_cast<size_t>(len); | |
| 101 } | 99 } |
| 102 | 100 |
| 103 size_t SyncSocket::Receive(void* buffer, size_t length) { | 101 size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 104 DCHECK_LE(length, kMaxMessageLength); | 102 DCHECK_LE(length, kMaxMessageLength); |
| 105 char* charbuffer = static_cast<char*>(buffer); | 103 char* charbuffer = static_cast<char*>(buffer); |
| 106 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 104 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
| 107 return length; | 105 return length; |
| 108 return 0; | 106 return 0; |
| 109 } | 107 } |
| 110 | 108 |
| 111 size_t SyncSocket::Peek() { | 109 size_t SyncSocket::Peek() { |
| 112 int number_chars; | 110 int number_chars; |
| 113 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 111 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { |
| 114 // If there is an error in ioctl, signal that the channel would block. | 112 // If there is an error in ioctl, signal that the channel would block. |
| 115 return 0; | 113 return 0; |
| 116 } | 114 } |
| 117 return (size_t) number_chars; | 115 return (size_t) number_chars; |
| 118 } | 116 } |
| 119 | 117 |
| 120 CancelableSyncSocket::CancelableSyncSocket() {} | 118 CancelableSyncSocket::CancelableSyncSocket() {} |
| 121 CancelableSyncSocket::CancelableSyncSocket(Handle handle) | 119 CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 122 : SyncSocket(handle) { | 120 : SyncSocket(handle) { |
| 123 } | 121 } |
| 124 | 122 |
| 125 bool CancelableSyncSocket::Shutdown() { | 123 bool CancelableSyncSocket::Shutdown() { |
| 126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; | 124 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
| 127 } | 125 } |
| 128 | 126 |
| 129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | |
| 130 long flags = 0; | |
| 131 flags = fcntl(handle_, F_GETFL, NULL); | |
| 132 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | |
| 133 // Set the socket to non-blocking mode for sending if its original mode | |
| 134 // is blocking. | |
| 135 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); | |
| 136 } | |
| 137 | |
| 138 size_t len = SyncSocket::Send(buffer, length); | |
| 139 | |
| 140 if (flags != -1 && (flags & O_NONBLOCK) == 0) { | |
| 141 // Restore the original flags. | |
| 142 fcntl(handle_, F_SETFL, flags); | |
| 143 } | |
| 144 | |
| 145 return len; | |
| 146 } | |
| 147 | |
| 148 // static | 127 // static |
| 149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 128 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 150 CancelableSyncSocket* socket_b) { | 129 CancelableSyncSocket* socket_b) { |
| 151 return SyncSocket::CreatePair(socket_a, socket_b); | 130 return SyncSocket::CreatePair(socket_a, socket_b); |
| 152 } | 131 } |
| 153 | 132 |
| 154 } // namespace base | 133 } // namespace base |
| OLD | NEW |