41 #include <sys/socket.h> 42 #include <sys/eventfd.h> 52 iSignalEvent_ = eventfd(0,0);
66 reinterpret_cast<void*>(&iOption),
69 throw makeException<SocketException>(
"setsockopt SO_REUSEADDR: %s",
76 throw makeException<SocketException>(
"bind: %s",
81 if(listen(iSock_,10) < 0)
83 throw makeException<SocketException>(
"listen: %s",
87 thread_ = std::thread(&Service::process,
this);
92 if(thread_.joinable())
100 eventfd_write(iSignalEvent_,1);
106 void EMANE::ControlPort::Service::process()
108 std::map<int,std::unique_ptr<Session>> sessionMap;
112 int nfds{iSignalEvent_};
117 FD_SET(iSignalEvent_,&rfds);
118 FD_SET(iSock_,&rfds);
120 nfds = std::max(iSock_,nfds);
122 for(
const auto & entry : sessionMap)
124 FD_SET(entry.first,&rfds);
125 nfds = std::max(entry.first,nfds);
128 auto result = select(nfds+1,&rfds,
nullptr,
nullptr,
nullptr);
142 if(FD_ISSET(iSignalEvent_,&rfds))
147 if(FD_ISSET(iSock_,&rfds))
151 if((iNewFd = accept(iSock_,
nullptr,
nullptr)) > 0)
153 sessionMap.insert(std::make_pair(iNewFd,std::unique_ptr<Session>{
new Session{}}));
157 auto iter = sessionMap.begin();
159 while(iter != sessionMap.end())
161 if(FD_ISSET(iter->first,&rfds))
164 if(iter->second->process(iter->first))
167 sessionMap.erase(iter++);
181 for(
const auto & entry : sessionMap)
socklen_t getAddrLength() const
void open(const INETAddr &endpoint)
sockaddr * getSockAddr() const