Skip to content

Commit

Permalink
Fix compilation on Mac OS X
Browse files Browse the repository at this point in the history
system_clock and high_resolution_clock are no synonyms on OS X.
The NOSIGNAL option must be set on the socket not during the call to send
  • Loading branch information
MichaelEischer committed May 6, 2015
1 parent af322bd commit c6786fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logger.cc
Expand Up @@ -5,7 +5,7 @@
#include <locale>
#include <sstream>

Logger::Logger(const std::string &filename) : start_time(std::chrono::system_clock::now()) {
Logger::Logger(const std::string &filename) : start_time(std::chrono::high_resolution_clock::now()) {
if (!filename.empty()) {
ofs.exceptions(std::ios_base::badbit | std::ios_base::failbit);
ofs.open(filename, std::ios_base::out | std::ios_base::app);
Expand Down
12 changes: 12 additions & 0 deletions udpbroadcast.cc
Expand Up @@ -55,6 +55,14 @@ bool InterfaceInfo::configure_socket(const Socket &sock, Logger &logger) const {
#ifdef WIN32
return true;
#else
#ifdef __APPLE__
int set = 1;
if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<void *>(&set), sizeof(int)) < 0) {
int rc = errno;
logger.write(Glib::ustring::compose(u8"Cannot set NOSIGPIPE option for interface %1: %2", Glib::locale_to_utf8(name_), Glib::locale_to_utf8(std::strerror(rc))));
return false;
}
#endif
if (family_ == AF_INET) {
ip_mreqn mreq;
mreq.imr_ifindex = ifindex;
Expand Down Expand Up @@ -194,7 +202,11 @@ void UDPBroadcast::send(const void *data, size_t length) {
if (i.configure_socket(sock.second, logger)) {
// The socket was set up to send to this interface.
// Now send data.
#ifdef __APPLE__
ssize_t ssz = ::send(sock.second, data, length, 0);
#else
ssize_t ssz = ::send(sock.second, data, length, MSG_NOSIGNAL);
#endif
if (ssz < 0) {
int rc = errno;
logger.write(Glib::ustring::compose(u8"Failed to send on interface %1 to address %2 and port %3: %4", Glib::locale_to_utf8(i.name()), Glib::locale_to_utf8(sock.first.first), Glib::locale_to_utf8(sock.first.second), Glib::locale_to_utf8(std::strerror(rc))));
Expand Down

0 comments on commit c6786fa

Please sign in to comment.