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 NET_EXPORT_PRIVATE SocketLibevent 27 : public base::MessageLoopForIO::Watcher { 28 public: 29 SocketLibevent(); 30 virtual ~SocketLibevent(); 31 32 // 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. 34 int Open(int address_family); 35 // Takes ownership of |socket|. 36 int AdoptConnectedSocket(SocketDescriptor socket, 37 const SockaddrStorage& peer_address); 38 // Releases ownership of |socket_fd_| to caller. 39 SocketDescriptor ReleaseConnectedSocket(); 40 41 int Bind(const SockaddrStorage& address); 42 43 int Listen(int backlog); 44 int Accept(scoped_ptr<SocketLibevent>* socket, 45 const CompletionCallback& callback); 46 47 // 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, 49 // not errno, though errno is set if connect event happens with error. 50 // TODO(byungchul): Need more robust way to pass system errno. 51 int Connect(const SockaddrStorage& address, 52 const CompletionCallback& callback); 53 bool IsConnected() const; 54 bool IsConnectedAndIdle() const; 55 56 // Multiple outstanding requests of the same type are not supported. 57 // 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 59 // 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. 61 // TODO(byungchul): Need more robust way to pass system errno. 62 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 63 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 64 65 // Waits for next write event. This is called by TCPsocketLibevent for TCP 66 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts 67 // waiting for write event successfully. Otherwise, returns a net error code. 68 // It must not be called after Write() because Write() calls it internally. 69 int WaitForWrite(IOBuffer* buf, int buf_len, 70 const CompletionCallback& callback); 71 72 int GetLocalAddress(SockaddrStorage* address) const; 73 int GetPeerAddress(SockaddrStorage* address) const; 74 void SetPeerAddress(const SockaddrStorage& address); 75 // Returns true if peer address has been set regardless of socket state. 76 bool HasPeerAddress() const; 77 78 void Close(); 79 80 SocketDescriptor socket_fd() const { return socket_fd_; } 81 82 private: 83 // base::MessageLoopForIO::Watcher methods. 84 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; 85 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; 86 87 int DoAccept(scoped_ptr<SocketLibevent>* socket); 88 void AcceptCompleted(); 89 90 int DoConnect(); 91 void ConnectCompleted(); 92 93 int DoRead(IOBuffer* buf, int buf_len); 94 void ReadCompleted(); 95 96 int DoWrite(IOBuffer* buf, int buf_len); 97 void WriteCompleted(); 98 99 void StopWatchingAndCleanUp(); 100 101 SocketDescriptor socket_fd_; 102 103 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; 104 scoped_ptr<SocketLibevent>* accept_socket_; 105 CompletionCallback accept_callback_; 106 107 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; 108 scoped_refptr<IOBuffer> read_buf_; 109 int read_buf_len_; 110 // External callback; called when read is complete. 111 CompletionCallback read_callback_; 112 113 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; 114 scoped_refptr<IOBuffer> write_buf_; 115 int write_buf_len_; 116 // External callback; called when write or connect is complete. 117 CompletionCallback write_callback_; 118 119 // A connect operation is pending. In this case, |write_callback_| needs to be 120 // called when connect is complete. 121 bool waiting_connect_; 122 123 scoped_ptr<SockaddrStorage> peer_address_; 124 125 base::ThreadChecker thread_checker_; 126 127 DISALLOW_COPY_AND_ASSIGN(SocketLibevent); 128 }; 129 130 } // namespace net 131 132 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_ 133