32 m_u.specific.sun_family = AF_LOCAL ;
33 ::memset( m_u.specific.sun_path , 0 ,
sizeof(m_u.specific.sun_path) ) ;
34 std::strncpy( m_u.specific.sun_path , path ,
sizeof(m_u.specific.sun_path)-1U ) ;
39 if( path.length() >=
sizeof(m_u.specific.sun_path) )
40 throw std::runtime_error(
"socket path too long" ) ;
42 m_u.specific.sun_family = AF_LOCAL ;
43 ::memset( m_u.specific.sun_path , 0 ,
sizeof(m_u.specific.sun_path) ) ;
44 std::strncpy( m_u.specific.sun_path , path.c_str() ,
sizeof(m_u.specific.sun_path)-1U ) ;
59 return sizeof(m_u.specific) ;
67 m_fd = ::socket( AF_LOCAL , datagram ? SOCK_DGRAM : SOCK_STREAM , 0 ) ;
68 int e = G::Process::errno_() ;
70 throw Error(
"failed to create local socket" , G::Process::strerror(e) ) ;
75 Address address( path.c_str() ) ;
76 int rc = ::connect( m_fd , address.p() , address.size() ) ;
77 int e = G::Process::errno_() ;
78 if( rc != 0 && !no_throw )
79 throw Error(
"failed to connect local socket" , path , G::Process::strerror(e) ) ;
80 m_connected = rc == 0 ;
86 G_ASSERT( !path.empty() ) ;
87 Address address( path.c_str() ) ;
88 int rc = ::bind( m_fd , address.p() , address.size() ) ;
92 throw Error(
"failed to bind path to local socket" , G::Process::strerror(e) , path ) ;
113 int flags = ::fcntl( m_fd , F_GETFL ) ;
115 ::fcntl( m_fd , F_SETFL , flags | O_NONBLOCK ) ;
LocalSocketAddress(const std::string &path)
Constructor.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
int fd() const
Returns the socket's file descriptor.
void nonblock()
Makes the socket non-blocking.
LocalSocket(bool datagram)
Constructor for a datagram socket or a stream socket.
bool connect(const std::string &address_path, bool no_throw=false)
Connects to the given address.
~LocalSocket()
Destructor.
struct sockaddr * p()
Returns the sockaddr pointer.
bool connected() const
Returns true if the socket has been successfully connect()ed.
An address class for G::LocalSocket that works with file system paths.
size_t size() const
Returns the sockaddr size.
void bind(const std::string &address_path)
Binds the given address to the socket. Throws Error on error.