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

Unified Diff: base/sync_socket_posix.cc

Issue 8965053: Implement support for a cancelable SyncSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and fix merges Created 8 years, 11 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
===================================================================
--- base/sync_socket_posix.cc (revision 118668)
+++ base/sync_socket_posix.cc (working copy)
@@ -30,25 +30,26 @@
const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
-bool SyncSocket::CreatePair(SyncSocket* pair[2]) {
- Handle handles[2] = { kInvalidHandle, kInvalidHandle };
- SyncSocket* tmp_sockets[2] = { NULL, NULL };
+SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
+
+SyncSocket::~SyncSocket() {
+ Close();
+}
+
+// static
+bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
+ DCHECK(socket_a != socket_b);
+ DCHECK(socket_a->handle_ == kInvalidHandle);
+ DCHECK(socket_b->handle_ == kInvalidHandle);
+
#if defined(OS_MACOSX)
int nosigpipe = 1;
#endif // defined(OS_MACOSX)
- // Create the two SyncSocket objects first to avoid ugly cleanup issues.
- tmp_sockets[0] = new SyncSocket(kInvalidHandle);
- if (tmp_sockets[0] == NULL) {
+ Handle handles[2] = { kInvalidHandle, kInvalidHandle };
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0)
goto cleanup;
- }
- tmp_sockets[1] = new SyncSocket(kInvalidHandle);
- if (tmp_sockets[1] == NULL) {
- goto cleanup;
- }
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
- goto cleanup;
- }
+
#if defined(OS_MACOSX)
// On OSX an attempt to read or write to a closed socket may generate a
// SIGPIPE rather than returning -1. setsockopt will shut this off.
@@ -59,11 +60,11 @@
goto cleanup;
}
#endif
+
// Copy the handles out for successful return.
- tmp_sockets[0]->handle_ = handles[0];
- pair[0] = tmp_sockets[0];
- tmp_sockets[1]->handle_ = handles[1];
- pair[1] = tmp_sockets[1];
+ socket_a->handle_ = handles[0];
+ socket_b->handle_ = handles[1];
+
return true;
cleanup:
@@ -75,8 +76,7 @@
if (HANDLE_EINTR(close(handles[1])) < 0)
DPLOG(ERROR) << "close";
}
- delete tmp_sockets[0];
- delete tmp_sockets[1];
+
return false;
}
@@ -117,4 +117,13 @@
return (size_t) number_chars;
}
+CancelableSyncSocket::CancelableSyncSocket() {}
+CancelableSyncSocket::CancelableSyncSocket(Handle handle)
+ : SyncSocket(handle) {
+}
+
+bool CancelableSyncSocket::Shutdown() {
+ return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
+}
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698