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