OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_SHELL_DOMAIN_SOCKET_SOCKET_LIBEVENT_H_ | |
6 #define MOJO_SHELL_DOMAIN_SOCKET_SOCKET_LIBEVENT_H_ | |
7 | |
8 #include <sys/types.h> | |
9 #include <sys/socket.h> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "mojo/shell/domain_socket/completion_callback.h" | |
19 #include "mojo/shell/domain_socket/socket_descriptor.h" | |
20 | |
21 namespace mojo { | |
22 namespace shell { | |
23 | |
24 // Convenience struct for when you need a |struct sockaddr|. | |
25 struct SockaddrStorage { | |
26 SockaddrStorage() | |
27 : addr_len(sizeof(addr_storage)), | |
28 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {} | |
29 SockaddrStorage(const SockaddrStorage& other); | |
30 void operator=(const SockaddrStorage& other); | |
31 | |
32 struct sockaddr_storage addr_storage; | |
33 socklen_t addr_len; | |
34 struct sockaddr* const addr; | |
35 }; | |
36 | |
37 // Socket class to provide asynchronous read/write operations on top of the | |
38 // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses. | |
39 class SocketLibevent : public base::MessageLoopForIO::Watcher { | |
40 public: | |
41 SocketLibevent(); | |
42 ~SocketLibevent() override; | |
43 | |
44 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6 | |
45 // or AF_UNIX. Otherwise, it does DCHECK() and returns a net error. | |
46 int Open(int address_family); | |
47 // Takes ownership of |socket|. | |
48 int AdoptConnectedSocket(SocketDescriptor socket, | |
49 const SockaddrStorage& peer_address); | |
50 // Releases ownership of |socket_fd_| to caller. | |
51 SocketDescriptor ReleaseConnectedSocket(); | |
52 | |
53 int Bind(const SockaddrStorage& address); | |
54 | |
55 int Listen(int backlog); | |
56 int Accept(scoped_ptr<SocketLibevent>* socket, | |
57 const CompletionCallback& callback); | |
58 | |
59 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net | |
60 // error code. On ERR_IO_PENDING, |callback| is called with a net error code, | |
61 // not errno, though errno is set if connect event happens with error. | |
62 // TODO(byungchul): Need more robust way to pass system errno. | |
63 int Connect(const SockaddrStorage& address, | |
64 const CompletionCallback& callback); | |
65 bool IsConnected() const; | |
66 bool IsConnectedAndIdle() const; | |
67 | |
68 int GetLocalAddress(SockaddrStorage* address) const; | |
69 int GetPeerAddress(SockaddrStorage* address) const; | |
70 void SetPeerAddress(const SockaddrStorage& address); | |
71 // Returns true if peer address has been set regardless of socket state. | |
72 bool HasPeerAddress() const; | |
73 | |
74 void Close(); | |
75 | |
76 SocketDescriptor socket_fd() const { return socket_fd_; } | |
77 | |
78 private: | |
79 // base::MessageLoopForIO::Watcher methods. | |
80 void OnFileCanReadWithoutBlocking(int fd) override; | |
81 void OnFileCanWriteWithoutBlocking(int fd) override; | |
82 | |
83 int DoAccept(scoped_ptr<SocketLibevent>* socket); | |
84 void AcceptCompleted(); | |
85 | |
86 int DoConnect(); | |
87 void ConnectCompleted(); | |
88 | |
89 void StopWatchingAndCleanUp(); | |
90 | |
91 SocketDescriptor socket_fd_; | |
92 | |
93 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; | |
94 scoped_ptr<SocketLibevent>* accept_socket_; | |
95 CompletionCallback accept_callback_; | |
96 | |
97 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | |
98 // External callback; called when read is complete. | |
99 CompletionCallback read_callback_; | |
100 | |
101 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | |
102 // External callback; called when write or connect is complete. | |
103 CompletionCallback write_callback_; | |
104 | |
105 // A connect operation is pending. In this case, |write_callback_| needs to be | |
106 // called when connect is complete. | |
107 bool waiting_connect_; | |
108 | |
109 scoped_ptr<SockaddrStorage> peer_address_; | |
110 | |
111 base::ThreadChecker thread_checker_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(SocketLibevent); | |
114 }; | |
115 | |
116 } // namespace shell | |
117 } // namespace mojo | |
118 | |
119 #endif // MOJO_SHELL_DOMAIN_SOCKET_SOCKET_LIBEVENT_H_ | |
OLD | NEW |