Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: base/sync_socket_posix.cc

Issue 10000004: Make the CancellableSyncSocket non-blocking on Send, and blocking on Receive (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed Tommi's comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
99 if (-1 == len)
tommi (sloooow) - chröme 2012/04/13 12:56:47 condense this to: return len == -1 ? 0 : static_c
no longer working on chromium 2012/04/16 10:26:45 Done.
100 return 0;
101
98 return static_cast<size_t>(len); 102 return static_cast<size_t>(len);
99 } 103 }
100 104
101 size_t SyncSocket::Receive(void* buffer, size_t length) { 105 size_t SyncSocket::Receive(void* buffer, size_t length) {
102 DCHECK_LE(length, kMaxMessageLength); 106 DCHECK_LE(length, kMaxMessageLength);
103 char* charbuffer = static_cast<char*>(buffer); 107 char* charbuffer = static_cast<char*>(buffer);
104 if (file_util::ReadFromFD(handle_, charbuffer, length)) 108 if (file_util::ReadFromFD(handle_, charbuffer, length))
105 return length; 109 return length;
106 return 0; 110 return 0;
107 } 111 }
108 112
109 size_t SyncSocket::Peek() { 113 size_t SyncSocket::Peek() {
110 int number_chars; 114 int number_chars;
111 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { 115 if (-1 == ioctl(handle_, FIONREAD, &number_chars)) {
112 // If there is an error in ioctl, signal that the channel would block. 116 // If there is an error in ioctl, signal that the channel would block.
113 return 0; 117 return 0;
114 } 118 }
115 return (size_t) number_chars; 119 return (size_t) number_chars;
116 } 120 }
117 121
118 CancelableSyncSocket::CancelableSyncSocket() {} 122 CancelableSyncSocket::CancelableSyncSocket() {}
119 CancelableSyncSocket::CancelableSyncSocket(Handle handle) 123 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
120 : SyncSocket(handle) { 124 : SyncSocket(handle) {
121 } 125 }
122 126
123 bool CancelableSyncSocket::Shutdown() { 127 bool CancelableSyncSocket::Shutdown() {
124 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; 128 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
125 } 129 }
126 130
131 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
132 long flags = 0;
133 flags = fcntl(handle_, F_GETFL, NULL);
134 if (flags != -1) {
tommi (sloooow) - chröme 2012/04/13 12:56:47 nit: if (flags != -1 && (flags & O_NONBLOCK) == 0)
no longer working on chromium 2012/04/16 10:26:45 Done.
135 // The default mode for the socket is blocking, set it to non-blocking mode
136 // for sending.
137 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
138 }
139
140 size_t len = SyncSocket::Send(buffer, length);
141
142 if (flags != -1) {
tommi (sloooow) - chröme 2012/04/13 12:56:47 nit: if (flags != -1 && (flags & O_NONBLOCK) == 0)
no longer working on chromium 2012/04/16 10:26:45 Done.
143 // Set the socket back to blocking mode.
tommi (sloooow) - chröme 2012/04/13 12:56:47 nit: // Restore the original flags.
no longer working on chromium 2012/04/16 10:26:45 Done.
144 fcntl(handle_, F_SETFL, flags);
145 }
146
147 return len;
148 }
149
127 // static 150 // static
128 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 151 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
129 CancelableSyncSocket* socket_b) { 152 CancelableSyncSocket* socket_b) {
130 return SyncSocket::CreatePair(socket_a, socket_b); 153 return SyncSocket::CreatePair(socket_a, socket_b);
131 } 154 }
132 155
133 } // namespace base 156 } // namespace base
OLDNEW
« no previous file with comments | « base/sync_socket.h ('k') | base/sync_socket_win.cc » ('j') | base/sync_socket_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698