From: Simon McVittie Date: Thu, 12 Mar 2015 19:54:25 +0000 (+0000) Subject: dbus-sysdeps: add more infrastructure around DBusSocket X-Git-Tag: dbus-1.9.16~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc652d9f0c1f428cf3e71746e75775a061850b36;p=thirdparty%2Fdbus.git dbus-sysdeps: add more infrastructure around DBusSocket This is all trivial right now, but will become significant when we change DBusSocket into a type-safe struct. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89444 Reviewed-by: Ralf Habacker --- diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index f5a9b4e27..4c92928b6 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -133,12 +133,36 @@ typedef unsigned long dbus_gid_t; * */ #ifndef DBUS_WIN + typedef int DBusSocket; # define DBUS_SOCKET_INVALID -1 -#else +# define DBUS_SOCKET_FORMAT "d" +# define DBUS_SOCKET_PRINTABLE(s) (s) +# define DBUS_SOCKET_INIT -1 +# define DBUS_SOCKET_IS_VALID(s) ((s) >= 0) +# define DBUS_SOCKET_INVALIDATE(s) ((s) = -1) +# define DBUS_SOCKET_GET_INT(s) (s) + +#else /* DBUS_WIN */ + typedef SOCKET DBusSocket; # define DBUS_SOCKET_INVALID INVALID_SOCKET -#endif +# define DBUS_SOCKET_FORMAT "Iu" +# define DBUS_SOCKET_PRINTABLE(s) (s) +# define DBUS_SOCKET_INIT INVALID_SOCKET +# define DBUS_SOCKET_IS_VALID(s) ((s) != INVALID_SOCKET) +# define DBUS_SOCKET_INVALIDATE(s) ((s) = INVALID_SOCKET) +# define DBUS_SOCKET_GET_INT(s) ((int) (s)) + +#endif /* DBUS_WIN */ + +static inline DBusSocket +_dbus_socket_get_invalid (void) +{ + DBusSocket s = DBUS_SOCKET_INIT; + + return s; +} DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_close_socket (DBusSocket fd, @@ -325,12 +349,43 @@ dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic); #define _DBUS_POLLNVAL 0x0020 #endif +#ifdef DBUS_WIN + +/* On Windows, you can only poll sockets. We emulate Unix poll() using + * select(), so it doesn't matter what precise type we put in DBusPollFD; + * use DBusSocket so that the compiler can check we are doing it right. + */ +typedef DBusSocket DBusPollable; +# define DBUS_SOCKET_GET_POLLABLE(s) (s) +# define DBUS_POLLABLE_FORMAT "Iu" +# define DBUS_POLLABLE_PRINTABLE(p) (p) +# define DBUS_POLLABLE_IS_VALID(p) (DBUS_SOCKET_IS_VALID (p)) +# define DBUS_POLLABLE_INVALIDATE(p) (DBUS_SOCKET_INVALIDATE (p)) +# define DBUS_POLLABLE_EQUALS(a, b) ((a) == (b)) + +#else /* !DBUS_WIN */ + +/* On Unix, you can poll sockets, pipes, etc., and we must put exactly + * "int" in DBusPollFD because we're relying on its layout exactly matching + * struct pollfd. (This is silly, and one day we should use a better + * abstraction.) + */ +typedef int DBusPollable; +# define DBUS_SOCKET_GET_POLLABLE(s) (s) +# define DBUS_POLLABLE_FORMAT "d" +# define DBUS_POLLABLE_PRINTABLE(p) (p) +# define DBUS_POLLABLE_IS_VALID(p) (p >= 0) +# define DBUS_POLLABLE_INVALIDATE(p) ((p) = -1) +# define DBUS_POLLABLE_EQUALS(a, b) ((a) == (b)) + +#endif /* !DBUS_WIN */ + /** * A portable struct pollfd wrapper. */ typedef struct { - int fd; /**< File descriptor */ + DBusPollable fd; /**< File descriptor */ short events; /**< Events to poll for */ short revents; /**< Events that occurred */ } DBusPollFD;