MCP: add debug tracing for timeouts
[ardour.git] / libs / surfaces / mackie / surface_port.cc
index a6f10f92433bd2b007e37bda5310915157c43cad..5a336447c812c18dd6c84ce3724ac1135a4cfa27 100644 (file)
@@ -20,8 +20,9 @@
 #include "mackie_control_exception.h"
 #include "controls.h"
 
-#include <midi++/types.h>
-#include <midi++/port.h>
+#include "midi++/types.h"
+#include "midi++/port.h"
+#include "midi++/manager.h"
 #include <sigc++/sigc++.h>
 #include <boost/shared_array.hpp>
 
 
 #include <sstream>
 
+#include <cstring>
+#include <cerrno>
+
 using namespace std;
 using namespace Mackie;
 
-SurfacePort::SurfacePort( MIDI::Port & port, int number )
-: _port( port ), _number( number ), _active( false )
+SurfacePort::SurfacePort()
+       : _input_port (0), _output_port (0), _number (0), _active (false)
 {
 }
 
+/** @param input_port Input MIDI::Port; this object takes responsibility for removing it from
+ *  the MIDI::Manager and destroying it.
+ *  @param output_port Output MIDI::Port; responsibility similarly taken.
+ */
+SurfacePort::SurfacePort (MIDI::Port & input_port, MIDI::Port & output_port, int number)
+       : _input_port (&input_port), _output_port (&output_port), _number (number), _active (false)
+{
+}
+
+SurfacePort::~SurfacePort()
+{
+#ifdef PORT_DEBUG
+       cout << "~SurfacePort::SurfacePort()" << endl;
+#endif
+       // make sure another thread isn't reading or writing as we close the port
+       Glib::RecMutex::Lock lock (_rwlock);
+       _active = false;
+
+       MIDI::Manager* mm = MIDI::Manager::instance ();
+       
+       if (_input_port) {
+               mm->remove_port (_input_port);
+               delete _input_port;
+       }
+
+       if (_output_port) {
+               mm->remove_port (_output_port);
+               delete _output_port;
+       }
+       
+#ifdef PORT_DEBUG
+       cout << "~SurfacePort::SurfacePort() finished" << endl;
+#endif
+}
+
+// wrapper for one day when strerror_r is working properly
+string fetch_errmsg (int error_number)
+{
+       char * msg = strerror (error_number);
+       return msg;
+}
+       
 MidiByteArray SurfacePort::read()
 {
        const int max_buf_size = 512;
        MIDI::byte buf[max_buf_size];
        MidiByteArray retval;
 
-       int nread = port().read( buf, sizeof (buf) );
+       // check active. Mainly so that the destructor
+       // doesn't destroy the mutex while it's still locked
+       if  (!active()) {
+               return retval;
+       }
+       
+       // return nothing read if the lock isn't acquired
+#if 0
+       Glib::RecMutex::Lock lock (_rwlock, Glib::TRY_LOCK);
+               
+       if  (!lock.locked()) {
+               cout << "SurfacePort::read not locked" << endl;
+               return retval;
+       }
+       
+       // check active again - destructor sequence
+       if  (!active()) return retval;
+#endif
+       
+       // read port and copy to return value
+       int nread = input_port().read (buf, sizeof (buf));
 
        if (nread >= 0) {
-               retval.copy( nread, buf );
-               if ((size_t) nread == sizeof (buf))
-               {
+               retval.copy (nread, buf);
+               if ((size_t) nread == sizeof (buf)) {
+#ifdef PORT_DEBUG
+                       cout << "SurfacePort::read recursive" << endl;
+#endif
                        retval << read();
                }
+       } else {
+               if  (errno != EAGAIN) {
+                       ostringstream os;
+                       os << "Surface: error reading from port: " << input_port().name();
+                       os << ": " << errno << fetch_errmsg (errno);
+
+                       cout << os.str() << endl;
+                       inactive_event();
+                       throw MackieControlException (os.str());
+               }
        }
-       else
-       {
-               ostringstream os;
-               os << "error reading from port: " << port().name() << " nread: " << nread;
-               cout << os.str() << endl;
-               inactive_event();
-               throw MackieControlException( os.str() );
-       }
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::read: " << retval << endl;
+#endif
        return retval;
 }
 
-void SurfacePort::write( const MidiByteArray & mba )
+void SurfacePort::write (const MidiByteArray & mba)
 {
-       if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
-       //cout << "SurfacePort::write: " << mba << endl;
-       int count = port().write( mba.bytes().get(), mba.size() );
-       if ( count != (int)mba.size() )
-       {
-               inactive_event();
-               ostringstream os;
-               os << _("Surface: couldn't write to port ") << port().name();
-               throw MackieControlException( os.str() );
+       if (mba.empty()) {
+               return;
        }
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
+
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::write: " << mba << " to " << output_port().name() << endl;
+#endif
+       
+       // check active before and after lock - to make sure
+       // that the destructor doesn't destroy the mutex while
+       // it's still in use
+       if (!active()) return;
+       Glib::RecMutex::Lock lock (_rwlock);
+       if (!active()) return;
+
+       int count = output_port().write (mba.bytes().get(), mba.size(), 0);
+       if  (count != (int)mba.size()) {
+               if  (errno == 0) {
+                       cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
+               } else if  (errno != EAGAIN) {
+                       ostringstream os;
+                       os << "Surface: couldn't write to port " << output_port().name();
+                       os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
+                       
+                       cout << os.str() << endl;
+                       inactive_event();
+               }
+       }
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::wrote " << count << endl;
+#endif
 }
 
-void SurfacePort::write_sysex( const MidiByteArray & mba )
+void SurfacePort::write_sysex (const MidiByteArray & mba)
 {
+       if (mba.empty()) {
+               return;
+       }
+
        MidiByteArray buf;
        buf << sysex_hdr() << mba << MIDI::eox;
-       write( buf );
+       write (buf);
 }
 
-void SurfacePort::write_sysex( MIDI::byte msg )
+void SurfacePort::write_sysex (MIDI::byte msg)
 {
        MidiByteArray buf;
        buf << sysex_hdr() << msg << MIDI::eox;
-       write( buf );
-}
-
-// This should be moved to midi++ at some point
-ostream & operator << ( ostream & os, const MIDI::Port & port )
-{
-       os << "device: " << port.device();
-       os << "; ";
-       os << "name: " << port.name();
-       os << "; ";
-       os << "type: " << port.type();
-       os << "; ";
-       os << "mode: " << port.mode();
-       os << "; ";
-       os << "ok: " << port.ok();
-       os << "; ";
-       os << "number: " << port.number();
-       os << "; ";
-       return os;
+       write (buf);
 }
 
-ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
+ostream & Mackie::operator <<  (ostream & os, const SurfacePort & port)
 {
        os << "{ ";
-       os << "device: " << port.port().device();
+       os << "name: " << port.input_port().name() << " " << port.output_port().name();
        os << "; ";
-       os << "name: " << port.port().name();
-       os << "; ";
-       os << "number: " << port.number();
        os << " }";
        return os;
 }
+