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