11/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
22#if !defined INC_PUBNUB_INTERNAL
3- #define INC_PUBNUB_INTERNAL
3+ #define INC_PUBNUB_INTERNAL
44
55
66#include <unistd.h>
@@ -24,12 +24,14 @@ typedef int pb_socket_t;
2424#define socket_send (socket , buf , len ) send((socket), (buf), (len), 0)
2525#endif
2626
27- #define socket_recv (socket , buf , len , flags ) recv((socket), (buf), (len), (flags))
27+ #define socket_recv (socket , buf , len , flags ) \
28+ recv((socket), (buf), (len), (flags))
2829
29- /* Treating `EINPROGRESS` the same as `EWOULDBLOCK` isn't
30+ /* Treating `EINPROGRESS` the same as `EWOULDBLOCK` isn't
3031 the greatest solution, but it is good for now.
3132*/
32- #define socket_would_block () ((errno == EAGAIN) ||(errno == EWOULDBLOCK) || (errno == EINPROGRESS))
33+ #define socket_would_block () \
34+ ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINPROGRESS))
3335
3436#define socket_timed_out () (errno == ETIMEDOUT)
3537
@@ -38,16 +40,35 @@ typedef int pb_socket_t;
3840#define SOCKET_INVALID -1
3941#define SOCKET_ERROR -1
4042
41- /* Maybe we could use `getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &error, &len)`,
42- but, its utility is questionable, so probably test extensively to see if it
43- really works for us.
43+ /* Maybe we could use `getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &error,
44+ &len)`, but, its utility is questionable, so probably test extensively to see
45+ if it really works for us.
4446 */
4547#define socket_is_connected (socket ) true
4648
47- #define socket_set_rcv_timeout (socket , milliseconds ) do { \
48- struct timeval M_tm = { (milliseconds)/1000, ((milliseconds)%1000) * 1000 }; \
49- setsockopt((socket), SOL_SOCKET, SO_RCVTIMEO, (char*)&M_tm, sizeof M_tm); \
50- } while(0)
49+ #define socket_set_rcv_timeout (socket , milliseconds ) \
50+ do { \
51+ struct timeval M_tm = { (milliseconds) / 1000, \
52+ ((milliseconds) % 1000) * 1000 }; \
53+ setsockopt((socket), SOL_SOCKET, SO_RCVTIMEO, (char*)&M_tm, sizeof M_tm); \
54+ } while (0)
55+
56+
57+ #if __APPLE__
58+ /* Actually, BSD in general provides SO_NOSIGPIPE, but we don't know
59+ what's the status of this support across various BSDs... So, for
60+ now, we only do this for MacOS.
61+ */
62+ #define socket_disable_SIGPIPE (socket ) \
63+ do { \
64+ int on = 1; \
65+ if (setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on)) == -1) { \
66+ PUBNUB_LOG_WARNING("Failed to set SO_NOSIGPIPE, errno=%d\n", errno); \
67+ } \
68+ } while (0)
69+ #else
70+ #define socket_disable_SIGPIPE (socket )
71+ #endif
5172
5273
5374/** The Pubnub POSIX context */
@@ -66,5 +87,4 @@ struct pubnub_pal {
6687#include "core/pubnub_internal_common.h"
6788
6889
69-
7090#endif /* !defined INC_PUBNUB_INTERNAL */
0 commit comments