summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/sync_socket.h6
-rw-r--r--base/sync_socket_posix.cc6
-rw-r--r--base/sync_socket_win.cc6
-rw-r--r--ipc/sync_socket_unittest.cc5
4 files changed, 23 insertions, 0 deletions
diff --git a/base/sync_socket.h b/base/sync_socket.h
index ad181ff..a44c445 100644
--- a/base/sync_socket.h
+++ b/base/sync_socket.h
@@ -51,6 +51,12 @@ class SyncSocket {
// Returns the number of bytes received, or 0 upon failure.
size_t Receive(void* buffer, size_t length);
+ // Returns the number of bytes available. If non-zero, Receive() will not
+ // not block when called. NOTE: Some implementations cannot reliably
+ // determine the number of bytes available so avoid using the returned
+ // size as a promise and simply test against zero.
+ size_t Peek();
+
// Extracts the contained handle. Used for transferring between
// processes.
Handle handle() const { return handle_; }
diff --git a/base/sync_socket_posix.cc b/base/sync_socket_posix.cc
index 35d53b2..5390c7d 100644
--- a/base/sync_socket_posix.cc
+++ b/base/sync_socket_posix.cc
@@ -98,4 +98,10 @@ size_t SyncSocket::Receive(void* buffer, size_t length) {
}
}
+// TODO(port). Some kind of select?
+size_t SyncSocket::Peek() {
+ NOTIMPLEMENTED();
+ return 0;
+}
+
} // namespace base
diff --git a/base/sync_socket_win.cc b/base/sync_socket_win.cc
index b591bb0..4e3c35f 100644
--- a/base/sync_socket_win.cc
+++ b/base/sync_socket_win.cc
@@ -141,4 +141,10 @@ size_t SyncSocket::Receive(void* buffer, size_t length) {
return count;
}
+size_t SyncSocket::Peek() {
+ DWORD available = 0;
+ PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL);
+ return available;
+}
+
} // namespace base
diff --git a/ipc/sync_socket_unittest.cc b/ipc/sync_socket_unittest.cc
index 9820009..7710c09 100644
--- a/ipc/sync_socket_unittest.cc
+++ b/ipc/sync_socket_unittest.cc
@@ -189,6 +189,11 @@ class SyncSocketClientListener : public IPC::Channel::Listener {
// string as was written on the SyncSocket. These are compared
// and a shutdown message is sent back to the server.
void OnMsgClassResponse(const std::string& str) {
+#if defined(OS_WIN)
+ // We rely on the order of sync_socket.Send() and chan_->Send() in
+ // the SyncSocketServerListener object.
+ EXPECT_EQ(kHelloStringLength, socket_->Peek());
+#endif
char buf[kHelloStringLength];
socket_->Receive(static_cast<void*>(buf), kHelloStringLength);
EXPECT_EQ(strcmp(str.c_str(), buf), 0);