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/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_util.h" |
| 17 #include "net/socket/socket_descriptor.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 class IOBuffer; |
| 22 class IPEndPoint; |
| 23 |
| 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. |
| 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, it does DCHECK() and returns a net error. |
| 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 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net |
| 45 // error code. On ERR_IO_PENDING, |callback| is called with a net error code, |
| 46 // not errno, though errno is set if connect event happens with error. |
| 47 // TODO(byungchul): Need more robust way to pass system errno. |
| 48 int Connect(const SockaddrStorage& address, |
| 49 const CompletionCallback& callback); |
| 50 bool IsConnected() const; |
| 51 bool IsConnectedAndIdle() const; |
| 52 |
| 53 // Multiple outstanding requests of the same type are not supported. |
| 54 // Full duplex mode (reading and writing at the same time) is supported. |
| 55 // On error which is not ERR_IO_PENDING, sets errno and returns a net error |
| 56 // code. On ERR_IO_PENDING, |callback| is called with a net error code, not |
| 57 // errno, though errno is set if read or write events happen with error. |
| 58 // TODO(byungchul): Need more robust way to pass system errno. |
| 59 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 60 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| 61 |
| 62 // Waits for next write event. This is called by TCPsocketLibevent for TCP |
| 63 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts |
| 64 // waiting for write event successfully. Otherwise, returns a net error code. |
| 65 // It must not be called after Write() because Write() calls it internally. |
| 66 int WaitForWrite(IOBuffer* buf, int buf_len, |
| 67 const CompletionCallback& callback); |
| 68 |
| 69 int GetLocalAddress(SockaddrStorage* address) const; |
| 70 int GetPeerAddress(SockaddrStorage* address) const; |
| 71 void SetPeerAddress(const SockaddrStorage& address); |
| 72 // Returns true if peer address has been set regardless of socket state. |
| 73 bool HasPeerAddress() const; |
| 74 |
| 75 void Close(); |
| 76 |
| 77 SocketDescriptor socket_fd() const { return socket_fd_; } |
| 78 |
| 79 private: |
| 80 // base::MessageLoopForIO::Watcher methods. |
| 81 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 82 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 83 |
| 84 int DoAccept(scoped_ptr<SocketLibevent>* socket); |
| 85 void AcceptCompleted(); |
| 86 |
| 87 int DoConnect(); |
| 88 void ConnectCompleted(); |
| 89 |
| 90 int DoRead(IOBuffer* buf, int buf_len); |
| 91 void ReadCompleted(); |
| 92 |
| 93 int DoWrite(IOBuffer* buf, int buf_len); |
| 94 void WriteCompleted(); |
| 95 |
| 96 SocketDescriptor socket_fd_; |
| 97 |
| 98 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; |
| 99 scoped_ptr<SocketLibevent>* accept_socket_; |
| 100 CompletionCallback accept_callback_; |
| 101 |
| 102 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 103 scoped_refptr<IOBuffer> read_buf_; |
| 104 int read_buf_len_; |
| 105 // External callback; called when read is complete. |
| 106 CompletionCallback read_callback_; |
| 107 |
| 108 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
| 109 scoped_refptr<IOBuffer> write_buf_; |
| 110 int write_buf_len_; |
| 111 // External callback; called when write or connect is complete. |
| 112 CompletionCallback write_callback_; |
| 113 |
| 114 // A connect operation is pending. In this case, |write_callback_| needs to be |
| 115 // called when connect is complete. |
| 116 bool waiting_connect_; |
| 117 |
| 118 scoped_ptr<SockaddrStorage> peer_address_; |
| 119 |
| 120 base::ThreadChecker thread_checker_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(SocketLibevent); |
| 123 }; |
| 124 |
| 125 } // namespace net |
| 126 |
| 127 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_ |
OLD | NEW |