Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / socket / socket_posix.h
blob6ded624579cc2c2c890b0f2c27e2f22c03ad57f3
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.
5 #ifndef NET_SOCKET_SOCKET_POSIX_H_
6 #define NET_SOCKET_SOCKET_POSIX_H_
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"
19 namespace net {
21 class IOBuffer;
22 class IPEndPoint;
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 SocketPosix : public base::MessageLoopForIO::Watcher {
27 public:
28 SocketPosix();
29 ~SocketPosix() override;
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 // Releases ownership of |socket_fd_| to caller.
38 SocketDescriptor ReleaseConnectedSocket();
40 int Bind(const SockaddrStorage& address);
42 int Listen(int backlog);
43 int Accept(scoped_ptr<SocketPosix>* socket,
44 const CompletionCallback& callback);
46 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net
47 // error code. On ERR_IO_PENDING, |callback| is called with a net error code,
48 // not errno, though errno is set if connect event happens with error.
49 // TODO(byungchul): Need more robust way to pass system errno.
50 int Connect(const SockaddrStorage& address,
51 const CompletionCallback& callback);
52 bool IsConnected() const;
53 bool IsConnectedAndIdle() const;
55 // Multiple outstanding requests of the same type are not supported.
56 // Full duplex mode (reading and writing at the same time) is supported.
57 // On error which is not ERR_IO_PENDING, sets errno and returns a net error
58 // code. On ERR_IO_PENDING, |callback| is called with a net error code, not
59 // errno, though errno is set if read or write events happen with error.
60 // TODO(byungchul): Need more robust way to pass system errno.
61 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
62 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
64 // Waits for next write event. This is called by TCPSocketPosix for TCP
65 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts
66 // waiting for write event successfully. Otherwise, returns a net error code.
67 // It must not be called after Write() because Write() calls it internally.
68 int WaitForWrite(IOBuffer* buf, int buf_len,
69 const CompletionCallback& callback);
71 int GetLocalAddress(SockaddrStorage* address) const;
72 int GetPeerAddress(SockaddrStorage* address) const;
73 void SetPeerAddress(const SockaddrStorage& address);
74 // Returns true if peer address has been set regardless of socket state.
75 bool HasPeerAddress() const;
77 void Close();
79 SocketDescriptor socket_fd() const { return socket_fd_; }
81 private:
82 // base::MessageLoopForIO::Watcher methods.
83 void OnFileCanReadWithoutBlocking(int fd) override;
84 void OnFileCanWriteWithoutBlocking(int fd) override;
86 int DoAccept(scoped_ptr<SocketPosix>* socket);
87 void AcceptCompleted();
89 int DoConnect();
90 void ConnectCompleted();
92 int DoRead(IOBuffer* buf, int buf_len);
93 void ReadCompleted();
95 int DoWrite(IOBuffer* buf, int buf_len);
96 void WriteCompleted();
98 void StopWatchingAndCleanUp();
100 SocketDescriptor socket_fd_;
102 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_;
103 scoped_ptr<SocketPosix>* accept_socket_;
104 CompletionCallback accept_callback_;
106 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
107 scoped_refptr<IOBuffer> read_buf_;
108 int read_buf_len_;
109 // External callback; called when read is complete.
110 CompletionCallback read_callback_;
112 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
113 scoped_refptr<IOBuffer> write_buf_;
114 int write_buf_len_;
115 // External callback; called when write or connect is complete.
116 CompletionCallback write_callback_;
118 // A connect operation is pending. In this case, |write_callback_| needs to be
119 // called when connect is complete.
120 bool waiting_connect_;
122 scoped_ptr<SockaddrStorage> peer_address_;
124 base::ThreadChecker thread_checker_;
126 DISALLOW_COPY_AND_ASSIGN(SocketPosix);
129 } // namespace net
131 #endif // NET_SOCKET_SOCKET_POSIX_H_