36 char * strdup_ignore_leak(
const char * p )
38 return ::strdup( p ) ;
48 if( bind_name.empty() )
50 G_ASSERT( fd() == -1 ) ;
54 Type type = parse( bind_name ) ;
59 if( !e.empty() )
throw std::runtime_error( e ) ;
63 m_net->bind( location.
address() ) ;
75 m_local->bind( type.path ) ;
78 G::Cleanup::add( CommandSocket::cleanup , strdup_ignore_leak(type.path.c_str()) ) ;
83 void Gv::CommandSocket::cleanup(
G::SignalSafe signal_safe ,
const char * name )
92 Type type = parse( connect_name ) ;
97 if( !e.empty() )
throw std::runtime_error( e ) ;
101 m_net->connect( location.
address() ) ;
109 m_local->connect( type.path ) ;
111 m_local->nonblock() ;
119 connect( connect_name ) ;
120 return std::string() ;
122 catch( std::exception & e )
143 size_t pos = name.rfind(
':') ;
148 if( type.port && name.find(
"udp://") == 0U )
150 type.path = name.substr(6U) ;
152 else if( type.port && pos > 3U && name.find(
"udp:") == 0U )
154 type.path = name.substr(4U) ;
156 else if( name.find(
"unix:") == 0U )
159 type.path = name.substr(5U) ;
161 else if( type.port == 0U || name.find(
'/') != std::string::npos )
170 if( m_local.get() )
return m_local->fd() ;
171 if( m_net.get() )
return m_net->fd() ;
178 m_buffer.resize( G::limits::pipe_buffer ) ;
179 ssize_t n = ::read( fd() , &m_buffer[0] , m_buffer.size() ) ;
182 result.assign( &m_buffer[0] , n ) ;
200 virtual void readEvent()
override ;
201 virtual void onException( std::exception & )
override ;
209 Gv::CommandSocketMixinImp::CommandSocketMixinImp(
CommandSocketMixin & outer ,
const std::string & bind_name ) :
211 m_command_socket(bind_name) ,
212 m_fd(m_command_socket.fd())
218 Gv::CommandSocketMixinImp::~CommandSocketMixinImp()
224 void Gv::CommandSocketMixinImp::readEvent()
226 m_outer.onCommandSocketData( m_command_socket.read() ) ;
229 void Gv::CommandSocketMixinImp::onException( std::exception & )
Describes a Gv::CommandSocket as a UDP address or unix-domain path.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
A derivation of GNet::Socket for a datagram socket.
Overload descriminator for Gv::CommandSocket.
A class which acquires the process's special privileges on construction and releases them on destruct...
static Identity start(SignalSafe)
A signal-safe alternative to construction.
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
A mixin base class that contains a bound Gv::CommandSocket object integrated with the GNet::EventLoop...
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
CommandSocket()
Constructor for a sending socket.
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
static Type parse(const std::string &bind_name)
Parses a filesystem path or transport address.
void connect(const std::string &connect_name)
Creates an association with the remote socket, taking a local filesystem path or a transport address...
int fd() const
Returns the file descriptor.
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
Address address() const
Returns the remote address.
virtual ~CommandSocketMixin()
Destructor.
and accept()ing should be performed directly on the file descriptor.
int domain() const
Returns the address 'domain', eg. PF_INET.
A base class for classes that handle asynchronous events from the event loop.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
An overload discriminator class for File methods.
std::string read()
Reads the socket. Returns the empty string on error.
static std::string resolve(Location &)
Does syncronous name resolution.
A non-blocking datagram socket that is used for sending and receiving process control commands...
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
void close()
Closes the sending socket, if open.
static std::string ws()
A convenience function returning standard whitespace characters.
static void add(void(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
CommandSocketMixin(const std::string &bind_name)
Constructor.
A pimple-pattern implementation class for Gv::CommandSocketMixin.