Merge branch 'master' into windows
[ardour.git] / libs / midi++2 / midi++ / ipmidi_port.h
1 /*
2     Copyright (C) 1998-2010 Paul Barton-Davis 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 */
18
19 #ifndef  __libmidi_ipmidi_port_h__
20 #define  __libmidi_ipmidi_port_h__
21
22 #include <string>
23 #include <iostream>
24 #if defined(PLATFORM_WINDOWS)
25 #include <winsock.h>
26 #elif defined(__FREE_BSD__)
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <sys/_sockaddr_storage.h>
30 #include <arpa/inet.h>
31 #include <netinet/ip_carp.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #include <net/route.h>
37 #else
38 #include <arpa/inet.h>
39 #include <net/if.h>
40 #endif
41
42 #include <glibmm/threads.h>
43
44 #include <jack/types.h>
45
46 #include "pbd/xml++.h"
47 #include "pbd/crossthread.h"
48 #include "pbd/signals.h"
49 #include "pbd/ringbuffer.h"
50
51 #include "midi++/types.h"
52 #include "midi++/parser.h"
53 #include "midi++/port.h"
54
55 namespace MIDI {
56
57 class IPMIDIPort : public Port {
58   public:
59     IPMIDIPort (int base_port = lowest_ipmidi_port_default, const std::string& ifname = std::string());
60     IPMIDIPort (const XMLNode&);
61     ~IPMIDIPort ();
62     
63     XMLNode& get_state () const;
64     void set_state (const XMLNode&);
65     
66     int write (const byte *msg, size_t msglen, timestamp_t timestamp);
67     int read (byte *buf, size_t bufsize);
68     void parse (framecnt_t timestamp);
69     int selectable () const;
70
71     static const int lowest_ipmidi_port_default = 21928;
72
73 private:        
74     int sockin;
75     int sockout;
76     struct sockaddr_in addrout;
77     Glib::Threads::Mutex write_lock;    
78
79     bool open_sockets (int base_port, const std::string& ifname);
80     void close_sockets ();
81
82     void init (std::string const &, Flags);
83 };
84
85 } // namespace MIDI
86
87 #endif // __libmidi_ipmidi_port_h__