awesome-client: handle disconnection

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-09-22 15:11:16 +02:00
parent cd24dcf59d
commit 9a4a1b90bd
1 changed files with 47 additions and 15 deletions

View File

@ -27,6 +27,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <readline/readline.h> #include <readline/readline.h>
@ -43,19 +44,26 @@
struct sockaddr_un *addr; struct sockaddr_un *addr;
int csfd; int csfd;
char *display;
/** Initialize the client and server socket connections. /** Initialize the client and server socket connections.
* If something goes wrong, preserves errno. * If something goes wrong, preserves errno.
* \return 0 if everything worked, 1 otherwise. * \return 0 if everything worked, 1 otherwise.
*/ */
static int static bool
sockets_init(void) sockets_init(void)
{ {
return (csfd = socket_getclient()) < 0 || if((csfd = socket_getclient()) < 0)
!(addr = socket_getaddr(getenv("DISPLAY"))) || return false;
connect(csfd, addr, sizeof(struct sockaddr_un)) == -1;
}
if(!(addr = socket_getaddr(display)))
return false;
if(connect(csfd, addr, sizeof(struct sockaddr_un)) == -1)
return false;
return true;
}
/** Close the client and server socket connections. /** Close the client and server socket connections.
*/ */
@ -66,6 +74,15 @@ sockets_close(void)
p_delete(&addr); p_delete(&addr);
} }
/** Reconnect sockets.
*/
static void
sockets_reconnect(void)
{
warn("connection lost, reconnecting…");
sockets_close();
sockets_init();
}
/** Send a message to awesome. /** Send a message to awesome.
* \param msg The message. * \param msg The message.
@ -82,6 +99,9 @@ send_msg(const char *msg, ssize_t msg_len)
case ENOENT: case ENOENT:
warn("can't write to %s", addr->sun_path); warn("can't write to %s", addr->sun_path);
break; break;
case EPIPE:
sockets_reconnect();
return send_msg(msg, msg_len);
default: default:
warn("error sending packet: %s", strerror(errno)); warn("error sending packet: %s", strerror(errno));
} }
@ -99,13 +119,24 @@ recv_msg(void)
{ {
ssize_t r; ssize_t r;
char buf[1024]; char buf[1024];
int try = 10;
r = recv(csfd, buf, sizeof(buf) - 1, MSG_TRUNC); while(try)
{
r = recv(csfd, buf, sizeof(buf) - 1, MSG_TRUNC | MSG_DONTWAIT);
if (r < 0) if (r < 0)
{ {
warn("error recieving from UNIX domain socket: %s", strerror(errno)); if(errno != EAGAIN)
return; return warn("error recieving from UNIX domain socket: %s", strerror(errno));
try--;
} }
else
break;
usleep(100000);
}
if(!try)
sockets_reconnect();
else if(r > 0) else if(r > 0)
{ {
buf[r] = '\0'; buf[r] = '\0';
@ -150,7 +181,9 @@ main(int argc, char **argv)
else if(argc > 2) else if(argc > 2)
exit_help(EXIT_SUCCESS); exit_help(EXIT_SUCCESS);
if (sockets_init()) display = getenv("DISPLAY");
if (!sockets_init())
{ {
warn("can't connect to UNIX domain socket: %s", strerror(errno)); warn("can't connect to UNIX domain socket: %s", strerror(errno));
return EXIT_FAILURE; return EXIT_FAILURE;
@ -158,7 +191,6 @@ main(int argc, char **argv)
if(isatty(STDIN_FILENO)) if(isatty(STDIN_FILENO))
{ {
char *display = getenv("DISPLAY");
asprintf(&prompt, "awesome@%s%% ", display ? display : "unknown"); asprintf(&prompt, "awesome@%s%% ", display ? display : "unknown");
while((msg = readline(prompt))) while((msg = readline(prompt)))
if((msg_len = a_strlen(msg))) if((msg_len = a_strlen(msg)))
@ -167,7 +199,7 @@ main(int argc, char **argv)
p_realloc(&msg, msg_len + 2); p_realloc(&msg, msg_len + 2);
msg[msg_len] = '\n'; msg[msg_len] = '\n';
msg[msg_len + 1] = '\0'; msg[msg_len + 1] = '\0';
send_msg(msg, msg_len + 2); if(send_msg(msg, msg_len + 2) == EXIT_SUCCESS)
recv_msg(); recv_msg();
p_delete(&msg); p_delete(&msg);
} }
@ -196,7 +228,7 @@ main(int argc, char **argv)
} }
if(msg_len > 1) if(msg_len > 1)
{ {
ret_value = send_msg(msg, msg_len); if((ret_value = send_msg(msg, msg_len)) == EXIT_SUCCESS)
recv_msg(); recv_msg();
} }
p_delete(&msg); p_delete(&msg);