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 NET_SOCKET_SOCKET_LIBEVENT_H_ | |
6 #define NET_SOCKET_SOCKET_LIBEVENT_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "net/base/completion_callback.h" | |
14 #include "net/base/net_util.h" | |
15 #include "net/socket/socket_descriptor.h" | |
16 | |
17 namespace net { | |
18 | |
19 class IOBuffer; | |
20 class IPEndPoint; | |
21 | |
22 // Socket class to provide asynchronous read/write operations on top of posix | |
mmenke
2014/06/26 15:36:11
nit: "on top of the posix"
byungchul
2014/06/26 18:09:23
Done.
| |
23 // socket api. It support AF_INET, AF_INET6, and AF_UNIX address. Though it | |
mmenke
2014/06/26 15:36:11
nit: "It supports", "addresses"
byungchul
2014/06/26 18:09:23
Done.
| |
24 // opens a steam socket by default, a datagram socket fd could be adopted by | |
25 // AdoptConnectedSocket() if it is connected already by posix connect() api. | |
mmenke
2014/06/26 15:36:11
I don't think we should mention datagram sockets h
byungchul
2014/06/26 18:09:23
Removed.
| |
26 class SocketLibevent : public base::MessageLoopForIO::Watcher{ | |
27 public: | |
28 SocketLibevent(); | |
29 virtual ~SocketLibevent(); | |
30 | |
31 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6 | |
32 // or AF_UNIX. Otherwise, return an net error. | |
mmenke
2014/06/26 15:36:11
nit: "a net"
byungchul
2014/06/26 18:09:23
Done.
| |
33 int Open(int address_family); | |
34 // Takes ownership of |socket|. | |
35 int AdoptConnectedSocket(SocketDescriptor socket, | |
36 const SockaddrStorage& peer_address); | |
37 | |
38 int Bind(const SockaddrStorage& address); | |
39 | |
40 int Listen(int backlog); | |
41 int Accept(scoped_ptr<SocketLibevent>* socket, | |
42 const CompletionCallback& callback); | |
43 | |
44 int Connect(const SockaddrStorage& address, | |
45 const CompletionCallback& callback); | |
46 bool IsConnected() const; | |
47 bool IsConnectedAndIdle() const; | |
48 | |
49 // Multiple outstanding requests are not supported. | |
50 // Full duplex mode (reading and writing at the same time) is supported. | |
51 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
52 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
53 | |
54 // Waits for next write event. This os called by TCPsocketLibevent for TCP | |
55 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts | |
56 // waiting for write event successfully. Otherwise, returns net_error. | |
57 int WaitForWrite(IOBuffer* buf, int buf_len, | |
58 const CompletionCallback& callback); | |
59 | |
60 int GetLocalAddress(SockaddrStorage* address) const; | |
61 int GetPeerAddress(SockaddrStorage* address) const; | |
62 void SetPeerAddress(const SockaddrStorage& address); | |
63 // Returns true if peer address has been set regardless of socket state. | |
64 bool HasPeerAddress() const; | |
65 | |
66 void Close(); | |
67 | |
68 SocketDescriptor socket_fd() const { return socket_fd_; } | |
69 | |
70 private: | |
71 // base::MessageLoopForIO::Watcher methods. | |
72 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
73 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
74 | |
75 int DoAccept(scoped_ptr<SocketLibevent>* socket); | |
76 void AcceptCompleted(); | |
77 | |
78 int DoConnect(); | |
79 void ConnectCompleted(); | |
80 | |
81 int DoRead(IOBuffer* buf, int buf_len); | |
82 void ReadCompleted(); | |
83 | |
84 int DoWrite(IOBuffer* buf, int buf_len); | |
85 void WriteCompleted(); | |
86 | |
87 SocketDescriptor socket_fd_; | |
88 | |
89 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; | |
90 scoped_ptr<SocketLibevent>* accept_socket_; | |
91 CompletionCallback accept_callback_; | |
92 | |
93 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | |
94 IOBuffer* read_buf_; | |
mmenke
2014/06/26 15:36:11
Why isn't this a scoped_refptr?
byungchul
2014/06/26 18:09:23
Done.
| |
95 int read_buf_len_; | |
96 // External callback; called when read is complete. | |
97 CompletionCallback read_callback_; | |
98 | |
99 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | |
100 IOBuffer* write_buf_; | |
mmenke
2014/06/26 15:36:11
Why isn't this a scoped_refptr?
byungchul
2014/06/26 18:09:23
Done.
| |
101 int write_buf_len_; | |
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_; | |
mmenke
2014/06/26 15:36:11
Why use ThreadChecker instead of inheriting from N
byungchul
2014/06/26 18:09:23
According to thread_checker.h, it is preferable to
| |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(SocketLibevent); | |
114 }; | |
115 | |
116 } // namespace net | |
117 | |
118 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_ | |
OLD | NEW |