OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <sys/socket.h> | 5 #include <sys/socket.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/posix/eintr_wrapper.h" | 10 #include "base/posix/eintr_wrapper.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "ipc/unix_domain_socket_util.h" | 14 #include "ipc/unix_domain_socket_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class SocketAcceptor : public base::MessageLoopForIO::Watcher { | 19 class SocketAcceptor : public base::MessageLoopForIO::Watcher { |
20 public: | 20 public: |
21 SocketAcceptor(int fd, base::MessageLoopProxy* target_thread) | 21 SocketAcceptor(int fd, base::MessageLoopProxy* target_thread) |
22 : server_fd_(-1), | 22 : server_fd_(-1), |
23 target_thread_(target_thread), | 23 target_thread_(target_thread), |
24 started_watching_event_(false, false), | 24 started_watching_event_(false, false), |
25 accepted_event_(false, false) { | 25 accepted_event_(false, false) { |
26 target_thread->PostTask(FROM_HERE, | 26 target_thread->PostTask(FROM_HERE, |
27 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); | 27 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); |
28 } | 28 } |
29 | 29 |
30 ~SocketAcceptor() override { Close(); } | 30 virtual ~SocketAcceptor() { |
| 31 Close(); |
| 32 } |
31 | 33 |
32 int server_fd() const { return server_fd_; } | 34 int server_fd() const { return server_fd_; } |
33 | 35 |
34 void WaitUntilReady() { | 36 void WaitUntilReady() { |
35 started_watching_event_.Wait(); | 37 started_watching_event_.Wait(); |
36 } | 38 } |
37 | 39 |
38 void WaitForAccept() { | 40 void WaitForAccept() { |
39 accepted_event_.Wait(); | 41 accepted_event_.Wait(); |
40 } | 42 } |
(...skipping 10 matching lines...) Expand all Loading... |
51 void StartWatching(int fd) { | 53 void StartWatching(int fd) { |
52 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); | 54 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); |
53 base::MessageLoopForIO::current()->WatchFileDescriptor( | 55 base::MessageLoopForIO::current()->WatchFileDescriptor( |
54 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this); | 56 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this); |
55 started_watching_event_.Signal(); | 57 started_watching_event_.Signal(); |
56 } | 58 } |
57 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) { | 59 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) { |
58 watcher->StopWatchingFileDescriptor(); | 60 watcher->StopWatchingFileDescriptor(); |
59 delete watcher; | 61 delete watcher; |
60 } | 62 } |
61 void OnFileCanReadWithoutBlocking(int fd) override { | 63 virtual void OnFileCanReadWithoutBlocking(int fd) override { |
62 ASSERT_EQ(-1, server_fd_); | 64 ASSERT_EQ(-1, server_fd_); |
63 IPC::ServerAcceptConnection(fd, &server_fd_); | 65 IPC::ServerAcceptConnection(fd, &server_fd_); |
64 watcher_->StopWatchingFileDescriptor(); | 66 watcher_->StopWatchingFileDescriptor(); |
65 accepted_event_.Signal(); | 67 accepted_event_.Signal(); |
66 } | 68 } |
67 void OnFileCanWriteWithoutBlocking(int fd) override {} | 69 virtual void OnFileCanWriteWithoutBlocking(int fd) override {} |
68 | 70 |
69 int server_fd_; | 71 int server_fd_; |
70 base::MessageLoopProxy* target_thread_; | 72 base::MessageLoopProxy* target_thread_; |
71 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; | 73 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; |
72 base::WaitableEvent started_watching_event_; | 74 base::WaitableEvent started_watching_event_; |
73 base::WaitableEvent accepted_event_; | 75 base::WaitableEvent accepted_event_; |
74 | 76 |
75 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); | 77 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); |
76 }; | 78 }; |
77 | 79 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); | 166 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); |
165 ASSERT_EQ(buf_len, sent_bytes); | 167 ASSERT_EQ(buf_len, sent_bytes); |
166 char recv_buf[sizeof(buffer)]; | 168 char recv_buf[sizeof(buffer)]; |
167 size_t received_bytes = | 169 size_t received_bytes = |
168 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); | 170 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); |
169 ASSERT_EQ(buf_len, received_bytes); | 171 ASSERT_EQ(buf_len, received_bytes); |
170 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); | 172 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); |
171 } | 173 } |
172 | 174 |
173 } // namespace | 175 } // namespace |
OLD | NEW |