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

Unified 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: ready for review now. 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 side-by-side diff with in-line comments
Download patch
Index: base/sync_socket_posix.cc
diff --git a/base/sync_socket_posix.cc b/base/sync_socket_posix.cc
index c5dca752b2de81da10f475cf30a3e74bc020513c..3657e32e2678e693bee964babc64cb4d922f8ea4 100644
--- a/base/sync_socket_posix.cc
+++ b/base/sync_socket_posix.cc
@@ -6,10 +6,11 @@
#include <errno.h>
#include <limits.h>
+#include <fcntl.h>
#include <stdio.h>
-#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <sys/types.h>
#if defined(OS_SOLARIS)
#include <sys/filio.h>
@@ -93,13 +94,28 @@ bool SyncSocket::Close() {
size_t SyncSocket::Send(const void* buffer, size_t length) {
DCHECK_LE(length, kMaxMessageLength);
+ 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.
+ flags = fcntl(handle_, F_GETFL, NULL);
+ 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.
+ // Set the socket to non-blocking mode if it is blocking.
+ fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
+ }
const char* charbuffer = static_cast<const char*>(buffer);
int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
+ 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.
+ return 0;
+
return static_cast<size_t>(len);
}
size_t SyncSocket::Receive(void* buffer, size_t length) {
DCHECK_LE(length, kMaxMessageLength);
+ 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.
+ flags = fcntl(handle_, F_GETFL, NULL);
+ 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.
+ // Set the socket to blocking mode if it is non-blocking.
+ fcntl(handle_, F_SETFL, flags & ~O_NONBLOCK);
+ }
char* charbuffer = static_cast<char*>(buffer);
if (file_util::ReadFromFD(handle_, charbuffer, length))
return length;

Powered by Google App Engine
This is Rietveld 408576698