From acf7d70d241c6afc1f3f13fb7b678882f1ec3ea5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 19 Mar 2014 08:20:09 -0700 Subject: Integrate IPC stuff into NodeConfig. --- node/IpcConnection.cpp | 65 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'node/IpcConnection.cpp') diff --git a/node/IpcConnection.cpp b/node/IpcConnection.cpp index 784c651d..c6937004 100644 --- a/node/IpcConnection.cpp +++ b/node/IpcConnection.cpp @@ -46,11 +46,13 @@ namespace ZeroTier { -IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,const SharedPtr &,const char *),void *arg) : +IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : _handler(commandHandler), _arg(arg), _sock(0) { +#ifdef __WINDOWS__ +#else struct sockaddr_un unaddr; unaddr.sun_family = AF_UNIX; strncpy(unaddr.sun_path,endpoint,sizeof(unaddr.sun_path)); @@ -64,11 +66,12 @@ IpcConnection::IpcConnection(const char *endpoint,void (*commandHandler)(void *, ::close(_sock); throw std::runtime_error("IPC endpoint unreachable"); } +#endif Thread::start(this); } -IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,const SharedPtr &,const char *),void *arg) : +IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,IpcConnection *,IpcConnection::EventType,const char *),void *arg) : _handler(commandHandler), _arg(arg), _sock(s) @@ -78,7 +81,17 @@ IpcConnection::IpcConnection(int s,void (*commandHandler)(void *,const SharedPtr IpcConnection::~IpcConnection() { - this->close(); +#ifdef __WINDOWS__ +#else + _writeLock.lock(); + int s = _sock; + _sock = 0; + if (s > 0) { + ::shutdown(s,SHUT_RDWR); + ::close(s); + } + _writeLock.unlock(); +#endif } void IpcConnection::printf(const char *format,...) @@ -95,44 +108,56 @@ void IpcConnection::printf(const char *format,...) va_start(ap,format); n = (int)::vsnprintf(tmp,sizeof(tmp),format,ap); va_end(ap); + if (n <= 0) + return; +#ifdef __WINDOWS__ +#else ::write(_sock,tmp,n); -} - -void IpcConnection::close() -{ - Mutex::Lock _l(_writeLock); - int s = _sock; - _sock = 0; - if (s > 0) { - ::shutdown(s,SHUT_RDWR); - ::close(s); - } - Thread::join(_thread); +#endif } void IpcConnection::threadMain() throw() { +#ifdef __WINDOWS__ +#else char tmp[65536]; char linebuf[65536]; unsigned int lineptr = 0; + int s,n,i; + char c; - while (_sock) { - int n = (int)::read(_sock,tmp,sizeof(tmp)); + for(;;) { + s = _sock; + if (s <= 0) + break; + n = (int)::read(s,tmp,sizeof(tmp)); if (n <= 0) break; - for(int i=0;i(this),linebuf); + _handler(_arg,this,IPC_EVENT_COMMAND,linebuf); lineptr = 0; } } else ++lineptr; } } + + { + _writeLock.lock(); + int s = _sock; + _sock = 0; + if (s > 0) + ::close(s); + _writeLock.unlock(); + } + + _handler(_arg,this,IPC_EVENT_CONNECTION_CLOSING,(const char *)0); +#endif } } // namespace ZeroTier -- cgit v1.2.3