socket_posix.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef NET_SOCKET_SOCKET_POSIX_H_
  5. #define NET_SOCKET_SOCKET_POSIX_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/message_loop/message_pump_for_io.h"
  11. #include "base/threading/thread_checker.h"
  12. #include "net/base/completion_once_callback.h"
  13. #include "net/base/net_export.h"
  14. #include "net/socket/socket_descriptor.h"
  15. #include "net/traffic_annotation/network_traffic_annotation.h"
  16. namespace net {
  17. class IOBuffer;
  18. struct SockaddrStorage;
  19. // Socket class to provide asynchronous read/write operations on top of the
  20. // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses.
  21. class NET_EXPORT_PRIVATE SocketPosix
  22. : public base::MessagePumpForIO::FdWatcher {
  23. public:
  24. SocketPosix();
  25. SocketPosix(const SocketPosix&) = delete;
  26. SocketPosix& operator=(const SocketPosix&) = delete;
  27. ~SocketPosix() override;
  28. // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6
  29. // or AF_UNIX. Otherwise, it does DCHECK() and returns a net error.
  30. int Open(int address_family);
  31. // Takes ownership of |socket|, which is known to already be connected to the
  32. // given peer address.
  33. int AdoptConnectedSocket(SocketDescriptor socket,
  34. const SockaddrStorage& peer_address);
  35. // Takes ownership of |socket|, which may or may not be open, bound, or
  36. // listening. The caller must determine the state of the socket based on its
  37. // provenance and act accordingly. The socket may have connections waiting
  38. // to be accepted, but must not be actually connected.
  39. int AdoptUnconnectedSocket(SocketDescriptor socket);
  40. // Releases ownership of |socket_fd_| to caller. There must be no pending
  41. // write.
  42. SocketDescriptor ReleaseConnectedSocket();
  43. int Bind(const SockaddrStorage& address);
  44. int Listen(int backlog);
  45. int Accept(std::unique_ptr<SocketPosix>* socket,
  46. CompletionOnceCallback callback);
  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, CompletionOnceCallback callback);
  52. bool IsConnected() const;
  53. bool IsConnectedAndIdle() const;
  54. // Multiple outstanding requests of the same type are not supported.
  55. // Full duplex mode (reading and writing at the same time) is supported.
  56. // On error which is not ERR_IO_PENDING, sets errno and returns a net error
  57. // code. On ERR_IO_PENDING, |callback| is called with a net error code, not
  58. // errno, though errno is set if read or write events happen with error.
  59. // TODO(byungchul): Need more robust way to pass system errno.
  60. int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
  61. // Reads up to |buf_len| bytes into |buf| without blocking. If read is to
  62. // be retried later, |callback| will be invoked when data is ready for
  63. // reading. This method doesn't hold on to |buf|.
  64. // See socket.h for more information.
  65. int ReadIfReady(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
  66. int CancelReadIfReady();
  67. int Write(IOBuffer* buf,
  68. int buf_len,
  69. CompletionOnceCallback callback,
  70. const NetworkTrafficAnnotationTag& traffic_annotation);
  71. // Waits for next write event. This is called by TCPSocketPosix for TCP
  72. // fastopen after sending first data. Returns ERR_IO_PENDING if it starts
  73. // waiting for write event successfully. Otherwise, returns a net error code.
  74. // It must not be called after Write() because Write() calls it internally.
  75. int WaitForWrite(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
  76. int GetLocalAddress(SockaddrStorage* address) const;
  77. int GetPeerAddress(SockaddrStorage* address) const;
  78. void SetPeerAddress(const SockaddrStorage& address);
  79. // Returns true if peer address has been set regardless of socket state.
  80. bool HasPeerAddress() const;
  81. void Close();
  82. // Detachs from the current thread, to allow the socket to be transferred to
  83. // a new thread. Should only be called when the object is no longer used by
  84. // the old thread.
  85. void DetachFromThread();
  86. SocketDescriptor socket_fd() const { return socket_fd_; }
  87. private:
  88. // base::MessagePumpForIO::FdWatcher methods.
  89. void OnFileCanReadWithoutBlocking(int fd) override;
  90. void OnFileCanWriteWithoutBlocking(int fd) override;
  91. int DoAccept(std::unique_ptr<SocketPosix>* socket);
  92. void AcceptCompleted();
  93. int DoConnect();
  94. void ConnectCompleted();
  95. int DoRead(IOBuffer* buf, int buf_len);
  96. void RetryRead(int rv);
  97. void ReadCompleted();
  98. int DoWrite(IOBuffer* buf, int buf_len);
  99. void WriteCompleted();
  100. // |close_socket| indicates whether the socket should also be closed.
  101. void StopWatchingAndCleanUp(bool close_socket);
  102. SocketDescriptor socket_fd_;
  103. base::MessagePumpForIO::FdWatchController accept_socket_watcher_;
  104. raw_ptr<std::unique_ptr<SocketPosix>> accept_socket_;
  105. CompletionOnceCallback accept_callback_;
  106. base::MessagePumpForIO::FdWatchController read_socket_watcher_;
  107. // Non-null when a Read() is in progress.
  108. scoped_refptr<IOBuffer> read_buf_;
  109. int read_buf_len_ = 0;
  110. CompletionOnceCallback read_callback_;
  111. // Non-null when a ReadIfReady() is in progress.
  112. CompletionOnceCallback read_if_ready_callback_;
  113. base::MessagePumpForIO::FdWatchController write_socket_watcher_;
  114. scoped_refptr<IOBuffer> write_buf_;
  115. int write_buf_len_ = 0;
  116. // External callback; called when write or connect is complete.
  117. CompletionOnceCallback 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_ = false;
  121. std::unique_ptr<SockaddrStorage> peer_address_;
  122. base::ThreadChecker thread_checker_;
  123. };
  124. } // namespace net
  125. #endif // NET_SOCKET_SOCKET_POSIX_H_