Use a Server object instead of just calling setup_server

This commit is contained in:
Maxime Coste 2012-10-29 13:44:53 +01:00
parent ba244a70a3
commit 6b664052b8

View File

@ -543,20 +543,22 @@ Client create_local_client(const String& file)
std::vector<Client> clients; std::vector<Client> clients;
void setup_server() struct Server
{ {
auto filename = "/tmp/kak-" + int_to_str(getpid()); Server()
{
m_filename = "/tmp/kak-" + int_to_str(getpid());
int listen_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); m_listen_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
sockaddr_un addr; sockaddr_un addr;
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, m_filename.c_str(), sizeof(addr.sun_path) - 1);
if (bind(listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1) if (bind(m_listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1)
throw runtime_error("unable to bind listen socket " + filename); throw runtime_error("unable to bind listen socket " + m_filename);
if (listen(listen_sock, 4) == -1) if (listen(m_listen_sock, 4) == -1)
throw runtime_error("unable to listen on socket " + filename); throw runtime_error("unable to listen on socket " + m_filename);
auto accepter = [=](int socket) { auto accepter = [=](int socket) {
sockaddr_un client_addr; sockaddr_un client_addr;
@ -582,9 +584,20 @@ void setup_server()
}); });
clients.push_back(std::move(client)); clients.push_back(std::move(client));
}; };
EventManager::instance().watch(listen_sock, accepter); EventManager::instance().watch(m_listen_sock, accepter);
} }
~Server()
{
unlink(m_filename.c_str());
close(m_listen_sock);
}
private:
int m_listen_sock;
String m_filename;
};
RemoteClient* connect_to(const String& pid) RemoteClient* connect_to(const String& pid)
{ {
auto filename = "/tmp/kak-" + pid; auto filename = "/tmp/kak-" + pid;
@ -660,7 +673,7 @@ int main(int argc, char* argv[])
write_debug("pid: " + int_to_str(getpid()) + "\n"); write_debug("pid: " + int_to_str(getpid()) + "\n");
write_debug("utf-8 test: é á ï"); write_debug("utf-8 test: é á ï");
setup_server(); Server server;
Client local_client; Client local_client;
try try