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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 } | 87 } |
87 int retval = HANDLE_EINTR(close(handle_)); | 88 int retval = HANDLE_EINTR(close(handle_)); |
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); |
97 long flags = 0; | |
tommi (sloooow) - chröme
2012/04/13 10:28:10
suggest you only change this for CancelableSyncSoc
no longer working on chromium
2012/04/13 11:50:50
Done.
| |
98 flags = fcntl(handle_, F_GETFL, NULL); | |
99 if (flags >= 0 && !(flags & O_NONBLOCK)) { | |
tommi (sloooow) - chröme
2012/04/13 10:28:10
instead of >= 0, use != -1
no longer working on chromium
2012/04/13 11:50:50
Done.
| |
100 // Set the socket to non-blocking mode if it is blocking. | |
101 fcntl(handle_, F_SETFL, flags | O_NONBLOCK); | |
102 } | |
96 const char* charbuffer = static_cast<const char*>(buffer); | 103 const char* charbuffer = static_cast<const char*>(buffer); |
97 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); | 104 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
105 if (-1 == len) | |
tommi (sloooow) - chröme
2012/04/13 10:28:10
good catch on handling -1!
tommi (sloooow) - chröme
2012/04/13 10:28:10
please reset the state of the socket after sending
no longer working on chromium
2012/04/13 11:50:50
Done.
| |
106 return 0; | |
107 | |
98 return static_cast<size_t>(len); | 108 return static_cast<size_t>(len); |
99 } | 109 } |
100 | 110 |
101 size_t SyncSocket::Receive(void* buffer, size_t length) { | 111 size_t SyncSocket::Receive(void* buffer, size_t length) { |
102 DCHECK_LE(length, kMaxMessageLength); | 112 DCHECK_LE(length, kMaxMessageLength); |
113 long flags = 0; | |
tommi (sloooow) - chröme
2012/04/13 10:28:10
imho this should be done after WriteFileDescriptor
no longer working on chromium
2012/04/13 11:50:50
Done.
| |
114 flags = fcntl(handle_, F_GETFL, NULL); | |
115 if (flags >= 0 && (flags & O_NONBLOCK)) { | |
tommi (sloooow) - chröme
2012/04/13 10:28:10
!= -1
no longer working on chromium
2012/04/13 11:50:50
Done.
| |
116 // Set the socket to blocking mode if it is non-blocking. | |
117 fcntl(handle_, F_SETFL, flags & ~O_NONBLOCK); | |
118 } | |
103 char* charbuffer = static_cast<char*>(buffer); | 119 char* charbuffer = static_cast<char*>(buffer); |
104 if (file_util::ReadFromFD(handle_, charbuffer, length)) | 120 if (file_util::ReadFromFD(handle_, charbuffer, length)) |
105 return length; | 121 return length; |
106 return 0; | 122 return 0; |
107 } | 123 } |
108 | 124 |
109 size_t SyncSocket::Peek() { | 125 size_t SyncSocket::Peek() { |
110 int number_chars; | 126 int number_chars; |
111 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { | 127 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { |
112 // If there is an error in ioctl, signal that the channel would block. | 128 // If there is an error in ioctl, signal that the channel would block. |
(...skipping 11 matching lines...) Expand all Loading... | |
124 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; | 140 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
125 } | 141 } |
126 | 142 |
127 // static | 143 // static |
128 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 144 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
129 CancelableSyncSocket* socket_b) { | 145 CancelableSyncSocket* socket_b) { |
130 return SyncSocket::CreatePair(socket_a, socket_b); | 146 return SyncSocket::CreatePair(socket_a, socket_b); |
131 } | 147 } |
132 | 148 |
133 } // namespace base | 149 } // namespace base |
OLD | NEW |