--- a/sc2src/sc2mpd.cpp
+++ b/sc2src/sc2mpd.cpp
@@ -100,10 +100,17 @@
#endif
+int g_sockfd = -1;
bool g_quitrequest = false;
void sigcatcher(int)
{
g_quitrequest = true;
+
+ // As recvfrom() is a blocking operation we need to shut the corresponding
+ // socket down so that recvfrom() returns with 0.
+ if (g_sockfd > 0) {
+ shutdown(g_sockfd, SHUT_RD);
+ }
}
using namespace OpenHome;
@@ -471,24 +478,17 @@
void HandleUserCmd(OhmReceiver* receiver, AudioEater* audio, const Brx& aUri)
{
struct sockaddr_un server_addr, client_addr;
- struct timeval read_timeout;
socklen_t len;
ssize_t num_bytes;
char buf[BUF_SIZE];
- int sockfd = -1;
-
- read_timeout.tv_sec = 1;
- read_timeout.tv_usec = 0;
LOGDEB("OhmReceiver: Running user command handler\n");
- sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
- if (sockfd == -1) {
+ g_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
+ if (g_sockfd == -1) {
LOGERR("Error: Cannot create socket: " << strerror(errno) << endl);
goto _leave;
}
- setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout,
- sizeof read_timeout);
if (remove(SOCK_PATH) == -1 && errno != ENOENT) {
LOGERR("Error: Cannot remove socket path: " << strerror(errno) << endl);
@@ -499,7 +499,7 @@
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, SOCK_PATH, sizeof(server_addr.sun_path) - 1);
- if (bind(sockfd, (struct sockaddr *) &server_addr,
+ if (bind(g_sockfd, (struct sockaddr *) &server_addr,
sizeof(struct sockaddr_un)) == -1) {
LOGERR("Error: Cannot bind to socket: " << strerror(errno) << endl);
goto _close_socket;
@@ -510,7 +510,7 @@
// recvfrom() is a blocking call and will wait for messages to arrive.
// If no data has been received within `read_timeout` then -1 is
// returned by `recvfrom()` with errno set to EAGAIN or EWOULDBLOCK.
- num_bytes = recvfrom(sockfd, buf, BUF_SIZE, 0,
+ num_bytes = recvfrom(g_sockfd, buf, BUF_SIZE, 0,
(struct sockaddr *) &client_addr, &len);
if (num_bytes == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
@@ -544,7 +544,7 @@
}
}
}
- ssize_t bytes_sent = sendto(sockfd, (const void *)response.c_str(),
+ ssize_t bytes_sent = sendto(g_sockfd, (const void *)response.c_str(),
response.length(), 0,
(struct sockaddr *)&client_addr, len);
if (bytes_sent == -1) {
@@ -555,8 +555,8 @@
}
_close_socket:
- if (sockfd > 0)
- close(sockfd);
+ if (g_sockfd > 0)
+ close(g_sockfd);
_leave:
LOGDEB("OhmReceiver: Leaving user command handler\n");
}