Fix log-scale rangesteps and log-control numeric spinboxes
[ardour.git] / libs / midi++2 / ipmidi_port.cc
index ace51eae7d7d7b89fd7d20673086dc0536584a65..26bc0a324e85ba78956371653630bb47c90d459e 100644 (file)
 #endif
 #if defined(PLATFORM_WINDOWS)
 #include <winsock2.h>
+#include <ws2tcpip.h>
 #else
 #include <netdb.h>
 #endif
 
 #if defined(PLATFORM_WINDOWS)
-static WSADATA g_wsaData;
 typedef int socklen_t;
 #else
 #include <unistd.h>
@@ -112,12 +112,12 @@ IPMIDIPort::close_sockets ()
        }
 }
 
+#ifndef PLATFORM_WINDOWS
 static bool
 get_address (int sock, struct in_addr *inaddr, const string& ifname )
 {
        // Get interface address from supplied name.
 
-#if !defined(PLATFORM_WINDOWS)
        struct ifreq ifr;
        ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
 
@@ -141,18 +141,12 @@ get_address (int sock, struct in_addr *inaddr, const string& ifname )
        inaddr->s_addr = sa.sin_addr.s_addr;
 
        return true;
-
-#else
-
-       return false;
-
-#endif // !PLATFORM_WINDOWS'
 }
+#endif
 
 bool
 IPMIDIPort::open_sockets (int base_port, const string& ifname)
 {
-#if !defined(PLATFORM_WINDOWS)
        int protonum = 0;
        struct protoent *proto = ::getprotobyname("IP");
 
@@ -181,6 +175,7 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
        // INADDR_ANY will bind to default interface,
        // specify alternate interface nameon which to bind...
        struct in_addr if_addr_in;
+#ifndef PLATFORM_WINDOWS
        if (!ifname.empty()) {
                if (!get_address(sockin, &if_addr_in, ifname)) {
                        error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
@@ -194,9 +189,12 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
        } else {
                if_addr_in.s_addr = htonl (INADDR_ANY);
        }
+#else
+       if_addr_in.s_addr = htonl (INADDR_ANY);
+#endif
 
        struct ip_mreq mreq;
-       mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
+       mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37"); /* ipMIDI group multicast address */
        mreq.imr_interface.s_addr = if_addr_in.s_addr;
        if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
                ::perror("setsockopt(IP_ADD_MEMBERSHIP)");
@@ -214,6 +212,7 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
        }
 
        // Will Hall, Oct 2007
+#ifndef PLATFORM_WINDOWS
        if (!ifname.empty()) {
                struct in_addr if_addr_out;
                if (!get_address(sockout, &if_addr_out, ifname)) {
@@ -225,6 +224,7 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
                        return false;
                }
        }
+#endif
 
        ::memset(&addrout, 0, sizeof(struct sockaddr_in));
        addrout.sin_family = AF_INET;
@@ -233,11 +233,43 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
 
        // Turn off loopback...
        int loop = 0;
-       if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
+
+#ifdef PLATFORM_WINDOWS
+
+       /* https://msdn.microsoft.com/en-us/library/windows/desktop/ms739161%28v=vs.85%29.aspx
+        *
+        * ------------------------------------------------------------------------------
+        * Note The Winsock version of the IP_MULTICAST_LOOP option is
+        * semantically different than the UNIX version of the
+        * IP_MULTICAST_LOOP option:
+        *
+        * In Winsock, the IP_MULTICAST_LOOP option applies only to the receive path.
+        * In the UNIX version, the IP_MULTICAST_LOOP option applies to the send path.
+        *
+        * For example, applications ON and OFF (which are easier to track than
+        * X and Y) join the same group on the same interface; application ON
+        * sets the IP_MULTICAST_LOOP option on, application OFF sets the
+        * IP_MULTICAST_LOOP option off. If ON and OFF are Winsock
+        * applications, OFF can send to ON, but ON cannot sent to OFF. In
+        * contrast, if ON and OFF are UNIX applications, ON can send to OFF,
+        * but OFF cannot send to ON.
+        * ------------------------------------------------------------------------------
+        *
+        * Alles klar? Gut! 
+        */
+
+       const int target_sock = sockin;
+#else
+       const int target_sock = sockout;
+#endif
+
+       if (::setsockopt (target_sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
                ::perror("setsockopt(IP_MULTICAST_LOOP)");
                return false;
        }
 
+#ifndef PLATFORM_WINDOWS
+
        if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
                error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
                return false;
@@ -248,10 +280,21 @@ IPMIDIPort::open_sockets (int base_port, const string& ifname)
                return false;
        }
 
-       return true;
 #else
-       return false;
-#endif // !PLATFORM_WINDOWS'
+       // If imode !=0, non-blocking mode is enabled.
+       u_long mode=1;
+       if (ioctlsocket(sockin,FIONBIO,&mode)) {
+               error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
+               return false;
+       }
+       mode = 1; /* just in case it was modified in the previous call */
+       if (ioctlsocket(sockout,FIONBIO,&mode)) {
+               error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
+               return false;
+       }
+#endif
+
+       return true;
 }
 
 int
@@ -276,7 +319,7 @@ IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
 }
 
 void
-IPMIDIPort::parse (framecnt_t timestamp)
+IPMIDIPort::parse (samplecnt_t timestamp)
 {
        /* input was detected on the socket, so go get it and hand it to the
         * parser. This will emit appropriate signals that will be handled
@@ -299,4 +342,3 @@ IPMIDIPort::parse (framecnt_t timestamp)
                ::perror ("failed to recv from socket");
        }
 }
-