/*
- Copyright (C) 2012 Paul Davie
-
+ Copyright (C) 2012 Paul Davis
+
+ Using code from Rui Nuno Capela's qmidinet as inspiration.
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+#ifdef COMPILER_MSVC
+#undef O_NONBLOCK
+#define O_NONBLOCK 0
+#endif
+#if defined(PLATFORM_WINDOWS)
+#include <winsock2.h>
+#else
#include <netdb.h>
+#endif
-#if defined(WIN32)
+#if defined(PLATFORM_WINDOWS)
static WSADATA g_wsaData;
typedef int socklen_t;
#else
inline void closesocket(int s) { ::close(s); }
#endif
-#include <jack/jack.h>
-#include <jack/midiport.h>
-
#include "pbd/xml++.h"
#include "pbd/error.h"
#include "pbd/failed_constructor.h"
int
IPMIDIPort::selectable () const
-{
- return sockin;
+{
+ return sockin;
}
XMLNode&
::closesocket (sockin);
sockin = -1;
}
-
+
if (sockout >= 0) {
::closesocket (sockout);
sockout = -1;
}
}
-static bool
+static bool
get_address (int sock, struct in_addr *inaddr, const string& ifname )
{
// Get interface address from supplied name.
-#if !defined(WIN32)
+#if !defined(PLATFORM_WINDOWS)
struct ifreq ifr;
::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
return false;
-#endif // !WIN32
+#endif // !PLATFORM_WINDOWS'
}
bool
IPMIDIPort::open_sockets (int base_port, const string& ifname)
{
+#if !defined(PLATFORM_WINDOWS)
int protonum = 0;
struct protoent *proto = ::getprotobyname("IP");
addrin.sin_family = AF_INET;
addrin.sin_addr.s_addr = htonl(INADDR_ANY);
addrin.sin_port = htons(base_port);
-
+
if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
::perror("bind");
return false;
}
-
+
// Will Hall, 2007
// INADDR_ANY will bind to default interface,
// specify alternate interface nameon which to bind...
} else {
if_addr_in.s_addr = htonl (INADDR_ANY);
}
-
+
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
mreq.imr_interface.s_addr = if_addr_in.s_addr;
::perror("socket(out)");
return false;
}
-
+
// Will Hall, Oct 2007
if (!ifname.empty()) {
struct in_addr if_addr_out;
return false;
}
}
-
+
::memset(&addrout, 0, sizeof(struct sockaddr_in));
addrout.sin_family = AF_INET;
addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
addrout.sin_port = htons (base_port);
-
+
// Turn off loopback...
int loop = 0;
if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
return false;
}
-
+
return true;
+#else
+ return false;
+#endif // !PLATFORM_WINDOWS'
}
int
-IPMIDIPort::write (byte* msg, size_t msglen, timestamp_t /* ignored */) {
+IPMIDIPort::write (const MIDI::byte* msg, size_t msglen, timestamp_t /* ignored */) {
if (sockout) {
- if (::sendto (sockout, (char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
+ Glib::Threads::Mutex::Lock lm (write_lock);
+ if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
::perror("sendto");
return -1;
}
}
int
-IPMIDIPort::read (byte* buf, size_t bufsize)
+IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
{
/* nothing to do here - all handled by parse() */
return 0;
* parser. This will emit appropriate signals that will be handled
* by anyone who cares.
*/
-
+
unsigned char buf[1024];
struct sockaddr_in sender;
socklen_t slen = sizeof(sender);
if (r >= 0) {
_parser->set_timestamp (timestamp);
-
+
for (int i = 0; i < r; ++i) {
_parser->scanner (buf[i]);
}