Browse Source

Fixed MSG_NOSIGNAL compatibility with MacOS

Paul-Louis Ageneau 5 years ago
parent
commit
a47ddc5838
1 changed files with 13 additions and 1 deletions
  1. 13 1
      src/tcptransport.cpp

+ 13 - 1
src/tcptransport.cpp

@@ -192,6 +192,13 @@ void TcpTransport::connect(const sockaddr *addr, socklen_t addrlen) {
 		if (::ioctlsocket(mSock, FIONBIO, &b) < 0)
 			throw std::runtime_error("Failed to set socket non-blocking mode");
 
+#ifdef __APPLE__
+		// MacOS lacks MSG_NOSIGNAL and requires SO_NOSIGPIPE instead
+		int opt = 1;
+		if (::setsockopt(mSock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)) < 0)
+			throw std::runtime_error("Failed to disable SIGPIPE for socket");
+#endif
+
 		// Initiate connection
 		int ret = ::connect(mSock, addr, addrlen);
 		if (ret < 0 && errno != EINPROGRESS) {
@@ -264,7 +271,12 @@ bool TcpTransport::trySendMessage(message_ptr &message) {
 	auto data = reinterpret_cast<const char *>(message->data());
 	auto size = message->size();
 	while (size) {
-		int len = ::send(mSock, data, size, MSG_NOSIGNAL);
+#ifdef __APPLE__
+		int flags = 0;
+#else
+		int flags = MSG_NOSIGNAL;
+#endif
+		int len = ::send(mSock, data, size, flags);
 		if (len < 0) {
 			if (errno == EAGAIN || errno == EWOULDBLOCK) {
 				message = make_message(message->end() - size, message->end());