unix_domain_client_socket_posix.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_UNIX_DOMAIN_CLIENT_SOCKET_POSIX_H_
  5. #define NET_SOCKET_UNIX_DOMAIN_CLIENT_SOCKET_POSIX_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/net_export.h"
  11. #include "net/log/net_log_with_source.h"
  12. #include "net/socket/socket_descriptor.h"
  13. #include "net/socket/stream_socket.h"
  14. #include "net/traffic_annotation/network_traffic_annotation.h"
  15. namespace net {
  16. class SocketPosix;
  17. // A client socket that uses unix domain socket as the transport layer.
  18. class NET_EXPORT UnixDomainClientSocket : public StreamSocket {
  19. public:
  20. // Builds a client socket with |socket_path|. The caller should call Connect()
  21. // to connect to a server socket.
  22. UnixDomainClientSocket(const std::string& socket_path,
  23. bool use_abstract_namespace);
  24. // Builds a client socket with SocketPosix which is already connected.
  25. // UnixDomainServerSocket uses this after it accepts a connection.
  26. explicit UnixDomainClientSocket(std::unique_ptr<SocketPosix> socket);
  27. UnixDomainClientSocket(const UnixDomainClientSocket&) = delete;
  28. UnixDomainClientSocket& operator=(const UnixDomainClientSocket&) = delete;
  29. ~UnixDomainClientSocket() override;
  30. // StreamSocket implementation.
  31. int Connect(CompletionOnceCallback callback) override;
  32. void Disconnect() override;
  33. bool IsConnected() const override;
  34. bool IsConnectedAndIdle() const override;
  35. int GetPeerAddress(IPEndPoint* address) const override;
  36. int GetLocalAddress(IPEndPoint* address) const override;
  37. const NetLogWithSource& NetLog() const override;
  38. bool WasEverUsed() const override;
  39. bool WasAlpnNegotiated() const override;
  40. NextProto GetNegotiatedProtocol() const override;
  41. bool GetSSLInfo(SSLInfo* ssl_info) override;
  42. int64_t GetTotalReceivedBytes() const override;
  43. void ApplySocketTag(const SocketTag& tag) override;
  44. // Socket implementation.
  45. int Read(IOBuffer* buf,
  46. int buf_len,
  47. CompletionOnceCallback callback) override;
  48. int Write(IOBuffer* buf,
  49. int buf_len,
  50. CompletionOnceCallback callback,
  51. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  52. int SetReceiveBufferSize(int32_t size) override;
  53. int SetSendBufferSize(int32_t size) override;
  54. // Releases ownership of underlying SocketDescriptor to caller.
  55. // Internal state is reset so that this object can be used again.
  56. // Socket must be connected in order to release it.
  57. SocketDescriptor ReleaseConnectedSocket();
  58. private:
  59. const std::string socket_path_;
  60. const bool use_abstract_namespace_;
  61. std::unique_ptr<SocketPosix> socket_;
  62. // This net log is just to comply StreamSocket::NetLog(). It throws away
  63. // everything.
  64. NetLogWithSource net_log_;
  65. };
  66. } // namespace net
  67. #endif // NET_SOCKET_UNIX_DOMAIN_CLIENT_SOCKET_POSIX_H_