OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef NET_SOCKET_SOCKET_LIBEVENT_H_ | 5 #ifndef NET_SOCKET_SOCKET_POSIX_H_ |
6 #define NET_SOCKET_SOCKET_LIBEVENT_H_ | 6 #define NET_SOCKET_SOCKET_POSIX_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
17 #include "net/socket/socket_descriptor.h" | 17 #include "net/socket/socket_descriptor.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 class IOBuffer; | 21 class IOBuffer; |
22 class IPEndPoint; | 22 class IPEndPoint; |
23 | 23 |
24 // Socket class to provide asynchronous read/write operations on top of the | 24 // Socket class to provide asynchronous read/write operations on top of the |
25 // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses. | 25 // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses. |
26 class NET_EXPORT_PRIVATE SocketLibevent | 26 class NET_EXPORT_PRIVATE SocketPosix : public base::MessageLoopForIO::Watcher { |
27 : public base::MessageLoopForIO::Watcher { | |
28 public: | 27 public: |
29 SocketLibevent(); | 28 SocketPosix(); |
30 ~SocketLibevent() override; | 29 ~SocketPosix() override; |
31 | 30 |
32 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6 | 31 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6 |
33 // or AF_UNIX. Otherwise, it does DCHECK() and returns a net error. | 32 // or AF_UNIX. Otherwise, it does DCHECK() and returns a net error. |
34 int Open(int address_family); | 33 int Open(int address_family); |
35 // Takes ownership of |socket|. | 34 // Takes ownership of |socket|. |
36 int AdoptConnectedSocket(SocketDescriptor socket, | 35 int AdoptConnectedSocket(SocketDescriptor socket, |
37 const SockaddrStorage& peer_address); | 36 const SockaddrStorage& peer_address); |
38 // Releases ownership of |socket_fd_| to caller. | 37 // Releases ownership of |socket_fd_| to caller. |
39 SocketDescriptor ReleaseConnectedSocket(); | 38 SocketDescriptor ReleaseConnectedSocket(); |
40 | 39 |
41 int Bind(const SockaddrStorage& address); | 40 int Bind(const SockaddrStorage& address); |
42 | 41 |
43 int Listen(int backlog); | 42 int Listen(int backlog); |
44 int Accept(scoped_ptr<SocketLibevent>* socket, | 43 int Accept(scoped_ptr<SocketPosix>* socket, |
45 const CompletionCallback& callback); | 44 const CompletionCallback& callback); |
46 | 45 |
47 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net | 46 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net |
48 // error code. On ERR_IO_PENDING, |callback| is called with a net error code, | 47 // error code. On ERR_IO_PENDING, |callback| is called with a net error code, |
49 // not errno, though errno is set if connect event happens with error. | 48 // not errno, though errno is set if connect event happens with error. |
50 // TODO(byungchul): Need more robust way to pass system errno. | 49 // TODO(byungchul): Need more robust way to pass system errno. |
51 int Connect(const SockaddrStorage& address, | 50 int Connect(const SockaddrStorage& address, |
52 const CompletionCallback& callback); | 51 const CompletionCallback& callback); |
53 bool IsConnected() const; | 52 bool IsConnected() const; |
54 bool IsConnectedAndIdle() const; | 53 bool IsConnectedAndIdle() const; |
55 | 54 |
56 // Multiple outstanding requests of the same type are not supported. | 55 // Multiple outstanding requests of the same type are not supported. |
57 // Full duplex mode (reading and writing at the same time) is supported. | 56 // Full duplex mode (reading and writing at the same time) is supported. |
58 // On error which is not ERR_IO_PENDING, sets errno and returns a net error | 57 // On error which is not ERR_IO_PENDING, sets errno and returns a net error |
59 // code. On ERR_IO_PENDING, |callback| is called with a net error code, not | 58 // code. On ERR_IO_PENDING, |callback| is called with a net error code, not |
60 // errno, though errno is set if read or write events happen with error. | 59 // errno, though errno is set if read or write events happen with error. |
61 // TODO(byungchul): Need more robust way to pass system errno. | 60 // TODO(byungchul): Need more robust way to pass system errno. |
62 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 61 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
63 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 62 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
64 | 63 |
65 // Waits for next write event. This is called by TCPsocketLibevent for TCP | 64 // Waits for next write event. This is called by TCPSocketPosix for TCP |
66 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts | 65 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts |
67 // waiting for write event successfully. Otherwise, returns a net error code. | 66 // waiting for write event successfully. Otherwise, returns a net error code. |
68 // It must not be called after Write() because Write() calls it internally. | 67 // It must not be called after Write() because Write() calls it internally. |
69 int WaitForWrite(IOBuffer* buf, int buf_len, | 68 int WaitForWrite(IOBuffer* buf, int buf_len, |
70 const CompletionCallback& callback); | 69 const CompletionCallback& callback); |
71 | 70 |
72 int GetLocalAddress(SockaddrStorage* address) const; | 71 int GetLocalAddress(SockaddrStorage* address) const; |
73 int GetPeerAddress(SockaddrStorage* address) const; | 72 int GetPeerAddress(SockaddrStorage* address) const; |
74 void SetPeerAddress(const SockaddrStorage& address); | 73 void SetPeerAddress(const SockaddrStorage& address); |
75 // Returns true if peer address has been set regardless of socket state. | 74 // Returns true if peer address has been set regardless of socket state. |
76 bool HasPeerAddress() const; | 75 bool HasPeerAddress() const; |
77 | 76 |
78 void Close(); | 77 void Close(); |
79 | 78 |
80 SocketDescriptor socket_fd() const { return socket_fd_; } | 79 SocketDescriptor socket_fd() const { return socket_fd_; } |
81 | 80 |
82 private: | 81 private: |
83 // base::MessageLoopForIO::Watcher methods. | 82 // base::MessageLoopForIO::Watcher methods. |
84 void OnFileCanReadWithoutBlocking(int fd) override; | 83 void OnFileCanReadWithoutBlocking(int fd) override; |
85 void OnFileCanWriteWithoutBlocking(int fd) override; | 84 void OnFileCanWriteWithoutBlocking(int fd) override; |
86 | 85 |
87 int DoAccept(scoped_ptr<SocketLibevent>* socket); | 86 int DoAccept(scoped_ptr<SocketPosix>* socket); |
88 void AcceptCompleted(); | 87 void AcceptCompleted(); |
89 | 88 |
90 int DoConnect(); | 89 int DoConnect(); |
91 void ConnectCompleted(); | 90 void ConnectCompleted(); |
92 | 91 |
93 int DoRead(IOBuffer* buf, int buf_len); | 92 int DoRead(IOBuffer* buf, int buf_len); |
94 void ReadCompleted(); | 93 void ReadCompleted(); |
95 | 94 |
96 int DoWrite(IOBuffer* buf, int buf_len); | 95 int DoWrite(IOBuffer* buf, int buf_len); |
97 void WriteCompleted(); | 96 void WriteCompleted(); |
98 | 97 |
99 void StopWatchingAndCleanUp(); | 98 void StopWatchingAndCleanUp(); |
100 | 99 |
101 SocketDescriptor socket_fd_; | 100 SocketDescriptor socket_fd_; |
102 | 101 |
103 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; | 102 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; |
104 scoped_ptr<SocketLibevent>* accept_socket_; | 103 scoped_ptr<SocketPosix>* accept_socket_; |
105 CompletionCallback accept_callback_; | 104 CompletionCallback accept_callback_; |
106 | 105 |
107 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 106 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
108 scoped_refptr<IOBuffer> read_buf_; | 107 scoped_refptr<IOBuffer> read_buf_; |
109 int read_buf_len_; | 108 int read_buf_len_; |
110 // External callback; called when read is complete. | 109 // External callback; called when read is complete. |
111 CompletionCallback read_callback_; | 110 CompletionCallback read_callback_; |
112 | 111 |
113 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | 112 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
114 scoped_refptr<IOBuffer> write_buf_; | 113 scoped_refptr<IOBuffer> write_buf_; |
115 int write_buf_len_; | 114 int write_buf_len_; |
116 // External callback; called when write or connect is complete. | 115 // External callback; called when write or connect is complete. |
117 CompletionCallback write_callback_; | 116 CompletionCallback write_callback_; |
118 | 117 |
119 // A connect operation is pending. In this case, |write_callback_| needs to be | 118 // A connect operation is pending. In this case, |write_callback_| needs to be |
120 // called when connect is complete. | 119 // called when connect is complete. |
121 bool waiting_connect_; | 120 bool waiting_connect_; |
122 | 121 |
123 scoped_ptr<SockaddrStorage> peer_address_; | 122 scoped_ptr<SockaddrStorage> peer_address_; |
124 | 123 |
125 base::ThreadChecker thread_checker_; | 124 base::ThreadChecker thread_checker_; |
126 | 125 |
127 DISALLOW_COPY_AND_ASSIGN(SocketLibevent); | 126 DISALLOW_COPY_AND_ASSIGN(SocketPosix); |
128 }; | 127 }; |
129 | 128 |
130 } // namespace net | 129 } // namespace net |
131 | 130 |
132 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_ | 131 #endif // NET_SOCKET_SOCKET_POSIX_H_ |
OLD | NEW |