Index: net/socket/socket_libevent.h |
diff --git a/net/socket/socket_libevent.h b/net/socket/socket_libevent.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0ad0bffc8c9175ec1d43224903595857cf7ab363 |
--- /dev/null |
+++ b/net/socket/socket_libevent.h |
@@ -0,0 +1,118 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef NET_SOCKET_SOCKET_LIBEVENT_H_ |
+#define NET_SOCKET_SOCKET_LIBEVENT_H_ |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/threading/thread_checker.h" |
+#include "net/base/completion_callback.h" |
+#include "net/base/net_util.h" |
+#include "net/socket/socket_descriptor.h" |
+ |
+namespace net { |
+ |
+class IOBuffer; |
+class IPEndPoint; |
+ |
+// 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.
|
+// 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.
|
+// opens a steam socket by default, a datagram socket fd could be adopted by |
+// 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.
|
+class SocketLibevent : public base::MessageLoopForIO::Watcher{ |
+ public: |
+ SocketLibevent(); |
+ virtual ~SocketLibevent(); |
+ |
+ // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6 |
+ // 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.
|
+ int Open(int address_family); |
+ // Takes ownership of |socket|. |
+ int AdoptConnectedSocket(SocketDescriptor socket, |
+ const SockaddrStorage& peer_address); |
+ |
+ int Bind(const SockaddrStorage& address); |
+ |
+ int Listen(int backlog); |
+ int Accept(scoped_ptr<SocketLibevent>* socket, |
+ const CompletionCallback& callback); |
+ |
+ int Connect(const SockaddrStorage& address, |
+ const CompletionCallback& callback); |
+ bool IsConnected() const; |
+ bool IsConnectedAndIdle() const; |
+ |
+ // Multiple outstanding requests are not supported. |
+ // Full duplex mode (reading and writing at the same time) is supported. |
+ int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
+ int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
+ |
+ // Waits for next write event. This os called by TCPsocketLibevent for TCP |
+ // fastopen after sending first data. Returns ERR_IO_PENDING if it starts |
+ // waiting for write event successfully. Otherwise, returns net_error. |
+ int WaitForWrite(IOBuffer* buf, int buf_len, |
+ const CompletionCallback& callback); |
+ |
+ int GetLocalAddress(SockaddrStorage* address) const; |
+ int GetPeerAddress(SockaddrStorage* address) const; |
+ void SetPeerAddress(const SockaddrStorage& address); |
+ // Returns true if peer address has been set regardless of socket state. |
+ bool HasPeerAddress() const; |
+ |
+ void Close(); |
+ |
+ SocketDescriptor socket_fd() const { return socket_fd_; } |
+ |
+ private: |
+ // base::MessageLoopForIO::Watcher methods. |
+ virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
+ virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
+ |
+ int DoAccept(scoped_ptr<SocketLibevent>* socket); |
+ void AcceptCompleted(); |
+ |
+ int DoConnect(); |
+ void ConnectCompleted(); |
+ |
+ int DoRead(IOBuffer* buf, int buf_len); |
+ void ReadCompleted(); |
+ |
+ int DoWrite(IOBuffer* buf, int buf_len); |
+ void WriteCompleted(); |
+ |
+ SocketDescriptor socket_fd_; |
+ |
+ base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; |
+ scoped_ptr<SocketLibevent>* accept_socket_; |
+ CompletionCallback accept_callback_; |
+ |
+ base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
+ 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.
|
+ int read_buf_len_; |
+ // External callback; called when read is complete. |
+ CompletionCallback read_callback_; |
+ |
+ base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
+ 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.
|
+ int write_buf_len_; |
+ // External callback; called when write or connect is complete. |
+ CompletionCallback write_callback_; |
+ |
+ // A connect operation is pending. In this case, |write_callback_| needs to be |
+ // called when connect is complete. |
+ bool waiting_connect_; |
+ |
+ scoped_ptr<SockaddrStorage> peer_address_; |
+ |
+ 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
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(SocketLibevent); |
+}; |
+ |
+} // namespace net |
+ |
+#endif // NET_SOCKET_SOCKET_LIBEVENT_H_ |